0

I've been using the following method to add a border to the top of a view:

<?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="#b7b7b7" /> 
        </shape>
    </item>   
    <item android:top="1px" >  
        <shape android:shape="rectangle"> 
            <solid android:color="#5f5f5f" />
            </shape>
    </item>    
</layer-list>

The above gets specified in its own xml file within the drawable folder, then set as the background on the view where I want the border to appear.

Now, the problem here is that this border is "hard coded" to a specific background color. Whichever view I apply it on, the background color will be changed to #5f5f5f.

I want to be able to set any background color, then apply a border. In other words, I can have a red view, a green view, and a blue view. Suppose I want to place the same border on top of each one. Is there a way to do this without making 3 copies of the above xml file and changing the color in each one?

mmBs
  • 7,728
  • 6
  • 37
  • 43
  • you have to assign an id to your layer and then use http://developer.android.com/reference/android/graphics/drawable/LayerDrawable.html#findDrawableByLayerId(int) to get the Drawable you want to change – pskink Dec 24 '13 at 20:33
  • Is there no way to do it within the XML itself? If I do have to duplicate the above XML a bunch of times, is there a way to do it all within the same XML file? It doesn't seem to allow setting an id on the layer-list itself. – user1464652 Dec 24 '13 at 22:36
  • you either have to change your Drawable at runtime or copy your xml files – pskink Dec 25 '13 at 06:54
  • See these http://myandroidtipsandtricks.blogspot.in/2012/09/drawables-part-ii.html http://stackoverflow.com/questions/8018435/android-how-to-change-a-layer-list-drawable – keshav Dec 24 '13 at 20:39

0 Answers0