I want to make a button style on Android with two background colors, such as the following image:
https://i.stack.imgur.com/ExKXl.png
Is it possible to make with drawable resources? I´ve being searching for a solution on http://developer.android.com/guide/topics/resources/drawable-resource.html but none of them can have two colors.
Is there a way?
[editing the answer]
The solution was to create a <layer-list>
with items and each <item>
has one <shape>
. The code is bellow (the entire button has 32dp height so I used half-height for each color):
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- Top color -->
<item android:bottom="16dp">
<shape android:shape="rectangle">
<solid android:color="#FF0000" /> <!-- RED -->
</shape>
</item>
<!-- Bottom color -->
<item android:top="16dp">
<shape android:shape="rectangle">
<solid android:color="#00FF00" /> <!-- GREEN -->
</shape>
</item>
</layer-list>
But I had another issue, I was trying to put corners on each shape. I tried to put android:topLeftRadius
and android:topRightRadius
on the first shape and android:bottomLeftRadius
and android:bottomRightRadius
on the second shape but it didn´t show me the corners! So the solution was to use android:radius
(all the 8 corners became rounded, dammit!) and put another two items to overcome the extra corners. At the end the XML has like that:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- Top color with corner -->
<item android:bottom="16dp">
<shape android:shape="rectangle">
<corners android:radius="5dp" /> <!-- It´s obligatory, It didn´t work only with android:topLeftRadius and android:topRightRadius -->
<solid android:color="#FF0000" /> <!-- RED Color-->
</shape>
</item>
<!-- Takes off the center corner -->
<item android:top="8dp" android:bottom="8dp">
<shape android:shape="rectangle">
<solid android:color="#FF0000" /> <!-- RED Color-->
</shape>
</item>
<!-- Bottom color with corner -->
<item android:top="16dp">
<shape android:shape="rectangle">
<corners android:radius="5dp" /> <!-- It´s obligatory, It didn´t work only with android:bottomLeftRadius and android:bottomRightRadius -->
<solid android:color="#00FF00" /> <!-- GREEN Color -->
</shape>
</item>
<!-- Takes off the center corner -->
<item android:top="16dp" android:bottom="8dp">
<shape android:shape="rectangle">
<solid android:color="#00FF00" /> <!-- GREEN Color -->
</shape>
</item>
</layer-list>
It is working now, thanks you all!