user3009824
user3009824

Reputation: 129

Change drawableleft in button selector

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?

Upvotes: 8

Views: 5789

Answers (1)

philomath
philomath

Reputation: 2209

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!

Upvotes: 12

Related Questions