Reputation:
When you create an android button, the original color is grey. when you press it, the color changes, when you release it, it goes back to it's original grey.
that's what I would like to do with mine, but with other colors than the default grey. The problem is, when I change the color of my button then I click on it, nothing happens, as if I was clicking on an empty layout.
Here's the XML file i wrote to solve my problem - button_connexion_style.xml
?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#ff0000ff" android:state_focused="true" /> <!-- focused -->
<item android:color="#449D44"/> <!-- default -->
<item android:drawable="#278727" android:state_pressed="true"/>
</selector>
In my button XML code I do this to call the previous XML :
android:background="@drawable/button_connexion_style"
But it doesn't work.
If anyone can help, please let me know.
Upvotes: 0
Views: 5008
Reputation: 3845
You are almost right just one thing you miss default should always last statement
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#ff0000ff" android:state_focused="true" /> <!-- focused -->
<item android:drawable="#278727" android:state_pressed="true"/>
<item android:drawable="@color/yourDefaultColor" />
</selector>
let me know if not work
Upvotes: 0
Reputation: 1615
Please do fill with according to your color code,
button_connexion_style.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/green" android:state_pressed="true" /> <!-- pressed -->
<item android:drawable="@color/blue" android:state_focused="true" /> <!-- focused -->
<item android:drawable="@color/default_green" /> <!-- default -->
</selector>
and add these color code in values colors ,
<color name="blue">#ff0000ff</color>
<color name="green">#278727</color>
<color name="default_green">#449D44</color>
Upvotes: 0
Reputation: 2079
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_enabled="false"
android:drawable="@color/red" />
<item
android:state_pressed="true"
android:state_enabled="true"
android:drawable="@color/blue" />
<item
android:state_focused="true"
android:state_enabled="true"
android:drawable="@color/green" />
<item
android:state_enabled="true"
android:drawable="@color/grey" />
</selector>
Create an xml file in your drawable like above, And set images/color accordingly and then set this xml as background of your imageButton.
Upvotes: 0
Reputation: 1020
flat_selector.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@drawable/rect_pressed"/>
<item android:drawable="@drawable/rect_normal"/>
</selector>
rect_normal.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/rect_pressed" />
<item android:bottom="@dimen/layer_padding">
<shape android:shape="rectangle">
<corners android:radius="@dimen/corner_radius" />
<solid android:color="@color/colorPrimary" />
</shape>
</item>
</layer-list>
rect_pressed.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="@dimen/corner_radius" />
<solid android:color="@color/colorPrimaryDark" />
</shape>
add below line in your button
android:background="@drawable/flat_selector"
Upvotes: 1