Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change drawableleft in button selector

Tags:

android

I found this Q&A Is it possible to change the left drawable of a Button in Selector xml? and it doesn't work for me. I tried this:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/blue_rounded_button" android:drawableLeft="@drawable/aj_share_button_drawableleft" android:state_pressed="true"/>
    <item android:drawable="@drawable/blue_rounded_button_frame" android:drawableLeft="@drawable/aj_share_button_drawableleft" />
</selector>


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

Set selector like this:

<Button android:id="@+id/button1" 
android:background="@drawable/aj_share_button_state"
android:drawableLeft="@drawable/aj_share_button_drawableleft" /> 

Can somebody tell me how to make this works?

like image 834
user3009824 Avatar asked Dec 03 '14 08:12

user3009824


1 Answers

I would suggest: Your button xml:

<Button android:id="@+id/button1" 
    android:background="@drawable/selector_aj_share_button_state"
    android:drawableLeft="@drawable/selector_aj_share_button_drawableleft"
/> 

selector_aj_share_button_drawableleft.xml (in your drawable folder)

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

It works with left drawable now. Try to do the same thing with selector_aj_share_button_state. Hope it helps!

like image 167
Great Question Avatar answered Sep 28 '22 12:09

Great Question