I am trying to change the color of the separator bar at the bottom of the action bar programmatically. My strategy is to set the action bar background to a programmatically generated LayerDrawable containing ShapeDrawable rectangles based on this XML :
<?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item> <shape android:shape="rectangle"> <solid android:color="@color/action_bar_line_color" /> </shape> </item> <item android:bottom="2dip"> <shape android:shape="rectangle"> <solid android:color="@color/action_bar_color" /> </shape> </item> </layer-list>
But I got to the checkpoint: I canβt understand how to apply the android:bottom property (as in <item android:bottom="2dip"> ) programmatically. Obviously, android:bottom is a property of the item tag, to which (I think) there is no software equivalent, and I could not find any ShapeDrawable methods / properties that look appropriate.
Code so far:
public LayerDrawable createABBackground(String color) { ShapeDrawable rect = new ShapeDrawable(new RectShape()); rect.getPaint().setColor(Color.parseColor("#000000")); ShapeDrawable rect2 = new ShapeDrawable(new RectShape()); rect2.getPaint().setColor(Color.parseColor(color)); ShapeDrawable[] layers = {rect, rect2}; LayerDrawable background = new LayerDrawable(layers); return background; }
Ideas? If it matters for alternative solutions, I use ActionBarSherlock.
EDIT:
setLayerInset, as suggested by MH, did what I wanted. A modified version of the function used is used here:
public LayerDrawable createABBackground(String color) { ShapeDrawable rect = new ShapeDrawable(new RectShape()); rect.getPaint().setColor(Color.parseColor(color)); ShapeDrawable rect2 = new ShapeDrawable(new RectShape()); rect2.getPaint().setColor(Color.parseColor("#000000")); ShapeDrawable[] layers = {rect, rect2}; LayerDrawable background = new LayerDrawable(layers); background.setLayerInset(0, 0, 3, 0, 0); background.setLayerInset(1, 0, 0, 0, 3); return background; }
source share