Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Layout changes are not reflecting in the application

I have created a button widget with custom images by referring this article. I have set images for onclick and onfocus. But I can only see the normal image for the button, nothing changes when I focus or press the button.

Here is my code:

custom_button.xml:

<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/ic_reload_4" />
    <item 
        android:drawable="@drawable/ic_reload_2"
        android:state_pressed="true" />
    <item 
        android:drawable="@drawable/ic_reload_3"
        android:state_focused="true" />
</selector>

Layout:

<Button 
    android:id="@+id/reload_btn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    android:background="@drawable/custom_button" />

Images are placed in the drawable folder. I m not sure why its not working. I have also faced issues with the layout,ie I can see the changes in the eclipse when selecting the 'layout' tab but its not shown in the application after updating it. Can anyone help me on this...

like image 210
Dijo David Avatar asked Dec 07 '25 02:12

Dijo David


1 Answers

Your selector should be like this:

    <?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item 
        android:drawable="@drawable/ic_reload_2"
        android:state_pressed="true" />
    <item 
        android:drawable="@drawable/ic_reload_3"
        android:state_focused="true" />
 <item android:drawable="@drawable/ic_reload_4" />
</selector>

I.e. you have to place the default item to the end. It seems that android inflates this like a switch-case block.

like image 103
ernazm Avatar answered Dec 08 '25 14:12

ernazm



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!