Asim
Asim

Reputation: 7114

Button not changing color onClick

I think I've got the required stuff. I want to change the color of my button on state_pressed. I've got the following resources:

color.xml in values with the following content:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="blue">#045FB4</color>
    <color name="clicked">#A9E2F3</color>
</resources>

button_dashboard in the drawable folder with the following content:

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

    <item android:drawable="@color/blue" />
    <item android:drawable="@color/clicked" android:state_pressed="true"/>

</selector>

And finally this Button in my layout:

<Button
        android:id="@+id/button2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1.0"
        android:background="@drawable/button_dashboard"
        android:text="Button" />

Am I doing something wrong? I haven't written any onClick code yet because that's not required at this point (just experimenting).

Upvotes: 2

Views: 6387

Answers (6)

user2223820
user2223820

Reputation: 593

how about this?

<?xml version="1.0" encoding="utf-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">    
    <item android:state_pressed="true">        
        <color android:color="@color/clicked" />    
    </item>    
    <item android:state_pressed="false">        
        <color android:color="@color/blue" />    
    </item>
 </selector>

Upvotes: 0

diordna
diordna

Reputation: 550

Add one more line to your button_dashboard

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

    <item android:drawable="@color/clicked" android:state_pressed="true"/>
    <item android:drawable="@color/clicked" android:state_selected="true"/>
    <item android:drawable="@color/blue" />

</selector>

After this please add one line code to your button click listener

button2.setSelected(true);

Hope this will help you.

Upvotes: 0

CRUSADER
CRUSADER

Reputation: 5472

Change content of button_dashboard in the drawable folder to this

<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" >
        <shape>
            <solid
                android:color="@color/clicked" />

        </shape>
    </item>
    <item>
        <shape>
            <solid
                android:color="@color/blue" />
        </shape>
    </item>
</selector>

And finally this Button in your layout:

<Button
        android:id="@+id/button2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1.0"
        android:background="@drawable/button_dashboard"
        android:text="Button" />

Hope this helps..

Upvotes: 1

sjain
sjain

Reputation: 23344

Add the following line to your button layout:

android:clickable="true"

So the button in your layout should now be:

<Button
        android:id="@+id/button2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1.0"
        android:clickable="true"
        android:background="@drawable/button_dashboard"
        android:text="Button" />

Upvotes: 0

getKonstantin
getKonstantin

Reputation: 1250

Try:

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

        <item android:drawable="@color/clicked" android:state_pressed="true"/>
        <item android:drawable="@color/blue" />


    </selector>

Upvotes: 3

Sunil Kumar
Sunil Kumar

Reputation: 7092

use this one drawable/selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
     <item android:state_focused="true" android:drawable="@color/Red"/>
     <item android:state_pressed="true" android:drawable="@color/Red" />
     <item android:drawable="@color/White" /> 
</selector>

and set as background of button in oncreate method

btn.setBackgroundResource(R.drawable.selector);

Upvotes: 0

Related Questions