Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Way to add a border of different color to top and bottom edge of an android view

I have a TextView and I'd like to add border with different colors along its top and bottom edges. I know that inorder to add a border of one color along all edges we can simply use following code:

<?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="#000000"/>
    </shape>
</item>

<item
    android:top="1dp" android:bottom="1dp">
    <shape android:shape="rectangle">
        <solid android:color="#ffffff"/>
    </shape>
</item>

But what needs to be done if we require edges with different colors?

like image 998
Yogesh Ghaturle Avatar asked Dec 17 '25 14:12

Yogesh Ghaturle


1 Answers

You're quite close to what you want, what you need to do is add another item below your default item. Those two items are your top/bottom borders. By add bottom/top 1dp to both, you reveal the two colours.

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
    android:bottom="1dp">
    <shape android:shape="rectangle">
        <solid android:color="#000000"/>
    </shape>
</item>

<item
    android:top="1dp">
    <shape android:shape="rectangle">
        <solid android:color="#000000"/>
    </shape>
</item>

<item
    android:top="1dp" android:bottom="1dp">
    <shape android:shape="rectangle">
        <solid android:color="#ffffff"/>
    </shape>
</item>
</layer-list>
like image 159
Evan Bashir Avatar answered Dec 19 '25 06:12

Evan Bashir



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!