Search code examples
androidlayer-list

How to apply stroke to a button specific sides in android


How can I apply stroke to a button's only two sides that is LEFT border and RIGHT border? in android.

I have apply the following code but it's not working

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!-- This is the main color -->
<item>
      <shape>
            <solid android:color="#EEEEEE" />
      </shape>
</item>

<!-- This is the line -->
<item android:right="1dp">
     <shape>
           <solid android:color="#333333" />
     </shape>
</item>

<item android:left="1dp">
    <shape >
        <solid android:color="#333333"/>
    </shape>
</item>
</layer-list>

Solution

  • You were on the right track. This will do the trick:

    <?xml version="1.0" encoding="utf-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    
        <item>
              <shape>
                    <solid android:color="#EEEEEE" />
              </shape>
        </item>
    
        <!-- This is the line -->
        <item android:right="1dp" android:left="1dp">
             <shape>
                   <solid android:color="#333333" />
             </shape>
        </item>
    
    </layer-list>
    

    And use this as the background for your button. Just tested it - works just fine.