Rectangle shape with two solid colors

nathan picture nathan · Jul 13, 2013 · Viewed 17.5k times · Source

I'd like to create a rectangle shape with two solid colors (horizontally) to achieve something like this:

enter image description here

I heard about layer-list, i though i could use it to contains two rectangle with a different color but it seems that it only lays shapes vertically.

Is there a way to achieve this using lalyer-list or should i use something totally different? I'd like to keep it simple with ability to change the shape colors at runtime.

Thanks.

Answer

Tarsem Singh picture Tarsem Singh · Jul 13, 2013

this will surely draw the shape as per your Requirement :

Adjust size of <item> as you need !

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
        android:left="50dip">
        <shape
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:shape="rectangle" >
            <solid android:color="#0000FF" />
        </shape>
    </item>
    <item android:right="50dip">
        <shape
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:shape="rectangle" >
            <solid android:color="#ff0000" />
        </shape>
    </item>

</layer-list>