Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to apply shape and selector simultaneously for Button?

I have applied a shape for a button like:

<?xml version="1.0" encoding="utf-8"?>

<shape xmlns:android="http://schemas.android.com/apk/res/android"
       android:shape="rectangle" >
    <gradient android:startColor="#DD000000" android:endColor="#DD2d2d2d"  android:angle="90"></gradient>
    <corners android:radius="15dip"></corners>

</shape>

Now I want to use a selector like:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/active"
      android:state_pressed="true" />
<item android:drawable="@drawable/passive"/>

for this Button as well. Is it possible ...???

like image 574
Khawar Raza Avatar asked Sep 30 '11 07:09

Khawar Raza


4 Answers

use this way:

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

   <item android:state_pressed="true" >
       <shape>.......</shape>
   </item>
   ..........
   ..........
</selector>
like image 78
Hanry Avatar answered Nov 20 '22 00:11

Hanry


Detailed to the point answer

Create a color resource in

res/values/colors.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>


    <item name="yellow" type="color">#F7B500</item>
    <item name="yellow_dark" type="color">#AC7E00</item>

    <integer-array name="androidcolors">
        <item>@color/yellow</item>
        <item>@color/yellow_dark</item>
    </integer-array>

</resources>

Create a drawable at

res/drawable/bg_yellow_round.xml

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

    <solid android:color="@color/yellow" />

    <stroke
        android:width="2dp"
        android:color="@color/yellow" />

    <corners
        android:bottomLeftRadius="20dp"
        android:bottomRightRadius="20dp"
        android:topLeftRadius="20dp"
        android:topRightRadius="20dp" />

</shape>

Create another drawable, you want for transition at same place and name it

res/drawable/bg_yellow_dark_round.xml

<solid android:color="@color/yellow_dark" />

<stroke
    android:width="2dp"
    android:color="@color/yellow_dark" />

<corners
    android:bottomLeftRadius="20dp"
    android:bottomRightRadius="20dp"
    android:topLeftRadius="20dp"
    android:topRightRadius="20dp" />

Now create a color state list at

res/color/btn_selector_yellow.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" android:exitFadeDuration="@android:integer/config_shortAnimTime">

    <item android:drawable="@color/yellow" android:state_focused="true" android:state_pressed="false"/>
    <item android:drawable="@drawable/bg_yellow_dark_round" android:state_pressed="true"/>
    <item android:drawable="@drawable/bg_yellow_round"/>

</selector>

Now set it to your button as following

<Button
                android:id="@+id/button1"
                android:layout_width="248dp"
                android:layout_height="44dp"
                android:layout_gravity="center_horizontal"
                android:layout_marginBottom="10dp"
                android:layout_marginTop="20dp"
                android:background="@color/btn_selector_yellow"
                android:text="AZ_ is so cool" />

Now this will do transition from light yellow

to

dark yellow.

like image 33
AZ_ Avatar answered Nov 20 '22 01:11

AZ_


shape.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="@color/star_off"/>
    <corners android:radius="5dp"/>
    <padding android:left="0dp" android:top="0dp" android:right="0dp" android:bottom="0dp" />
</shape>

selector.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android" android:exitFadeDuration="@android:integer/config_mediumAnimTime">

    <item android:drawable="@color/tab_focused" android:state_focused="true" android:state_pressed="false"/>
    <item android:drawable="@color/tab_pressed" android:state_pressed="true"/>
    <item android:drawable="@drawable/shape"/>

</selector>
like image 12
Renan Bandeira Avatar answered Nov 20 '22 00:11

Renan Bandeira


You can also create a shape that is using a selector inside. If your shape is just changing its color in different states, this is a lot cleaner.

color/color_selector.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="@color/blue_dark" android:state_pressed="true" />
    <item android:color="@color/blue_light" />
</selector>

drawable/shape.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="@color/color_selector" />
    <corners android:bottomLeftRadius="6dip" android:bottomRightRadius="6dp" />
    <padding android:bottom="0dip" android:left="0dip" android:right="0dip" android:top="0dip" />
</shape>
like image 6
Till Avatar answered Nov 20 '22 01:11

Till