I would like to make a button that is fill with more than 3 colors, say 7 color rainbow, starting from left = red to right = purple.
But I find that the following code could only meet for 3 colors.
Question:
Is there a way to generate a rainbow gradient? I have on my hand a Rainbow.png, would that be used? Thanks!!
Current code:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" >
...
</item>
<item>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<stroke android:width="0dp" android:color="@color/black" />
<gradient
android:startColor="@color/red"
android:centerColor="@color/green"
android:endColor="@color/purple"
android:angle="0" />
<padding android:left="5dp" android:top="2dp"
android:right="5dp" android:bottom="2dp" />
<corners android:radius="0dp" />
</shape>
</item>
</selector>
Answer:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" >
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<stroke android:width="0dp" android:color="@color/black" />
<solid android:color="@color/grey"/>
<padding android:left="5dp" android:top="2dp"
android:right="5dp" android:bottom="2dp" />
<corners android:radius="0dp" />
</shape>
</item>
<item android:drawable="@drawable/rainbow" >
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<padding android:left="5dp" android:top="2dp"
android:right="5dp" android:bottom="2dp" />
<corners android:radius="0dp" />
</shape>
</item>
</selector>
Android has the 9-patch system built in to handle complicated graphics, which is similar in style to how some website backgrounds are drawn.
Android also has it’s own 9-patch creator bundled with the sdk, so it’s easy enough to edit your png file, then apply the 9-patch to the button.
This will allow you to use your png, and it’ll resize according to the stretchable area of your graphic that you define to resize as your button resizes