Android button with rainbow gradient

I would like to make a button filled with more than 3 colors, for example 7 rainbow colors starting from left = red to right = purple.

But I believe the following code can only appear for three colors.

Question:

Is there a way to generate a rainbow gradient? I have Rainbow.png on hand, will this be used? Thank!!

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>

      

+3


source to share


1 answer


Android has a 9-patch system built in to handle complex graphics that are similar to the drawing style of some websites.

Android also has its own 9-patch creator bundled with the sdk, so it's easy enough to edit your png file and then apply the 9-patch to the button.



This will allow you to use your png and it will resize to fit the stretchable area of ​​your graphic that you define to resize as your button resizes

-1


source







All Articles