user1662334
user1662334

Reputation: 669

selector for button not working

I want to change the background of the button when the button is unclickable.I have used selector for that but it is not working in the case when the button remains unclickable.

This is the selector file:

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

This is the button where i am using this selector file:

<Button
                android:id="@+id/PrevButton"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_centerInParent="true"
                android:layout_marginLeft="5.0dip"
                android:layout_weight="1.0"
                android:background="@drawable/ibtn"
                android:onClick="onPrevButtonClick"
                android:text="Prev" />

Please help me.All other functions are working in the selector file but only the unclickable button case is not working.Thanks

Upvotes: 4

Views: 8426

Answers (3)

Kumar
Kumar

Reputation: 53

Just modify your selectors with below selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- Non focused states -->
    <item android:state_focused="false" android:state_selected="false"
        android:state_pressed="false" android:drawable="@android:color/transparent" />
    <item android:state_focused="false" android:state_selected="true"
        android:state_pressed="false" android:drawable="@drawable/round_corner_button_sort" />

    <!-- Focused states -->
    <item android:state_focused="true" android:state_selected="false"
        android:state_pressed="false" android:drawable="@drawable/round_corner_button_sort" />
    <item android:state_focused="true" android:state_selected="true"
        android:state_pressed="false" android:drawable="@drawable/round_corner_button_sort" />

</selector>

Hope this helps you.

Edit

As per your comments you can't use selector for button which is the state as false(button.setclickable(false);) In this state you can't use your selector

Upvotes: 0

Gaurav Vashisth
Gaurav Vashisth

Reputation: 7737

instead of button.setClickable(false) do button.setEnabled(false) and use the same selector xml.

Upvotes: 2

Ankitkumar Makwana
Ankitkumar Makwana

Reputation: 3485

this may helps you

i think in your selector file having issue try selector file like below

pls change images as you require

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/img_btn_repeat_pressed"
        android:state_focused="true"
        android:state_pressed="true" />
    <item android:drawable="@drawable/img_btn_repeat_pressed"
        android:state_focused="false"
        android:state_pressed="true" />
    <item android:drawable="@drawable/img_btn_repeat_pressed" android:state_focused="true" />
    <item android:drawable="@drawable/img_btn_repeat"
        android:state_focused="false"
        android:state_pressed="false" />
</selector>

Upvotes: 0

Related Questions