user468311
user468311

Reputation:

State list drawable not working for disabled state

I have a custom zoom control view, when my map is zoomed out to max level, I want the view to be transparent. Here's my code:

zoomOut = (TextView)findViewById(R.id.zoom_out);
zoomOut.setOnClickListener(new View.OnClickListener() {
     @Override
     public void onClick(View v) {
         boolean maxLevel = mapFragment.zoomOut();
         zoomOut.setEnabled(maxLevel);
     }
});

And the resources:

<TextView
        android:id="@+id/zoom_out"
        android:layout_width="@dimen/map_button_size_small"
        android:layout_height="@dimen/map_button_size_small"
        android:background="@drawable/floor_button_bg"
        android:gravity="center"
        android:text="&#8211;"
        android:textSize="28sp"
        android:textColor="@color/btn_map_text_color" />

Here's the state list drawable for my view:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_pressed="true">
        <shape android:shape="rectangle">
            <solid android:color="@color/btn_map_bg_selected" />
        </shape>
    </item>

    <item android:state_selected="true">
        <shape android:shape="rectangle">
            <solid android:color="@color/btn_map_bg_selected" />
        </shape>    
    </item>

    <item android:state_enabled="false">
        <shape android:shape="rectangle">
            <solid android:color="@android:color/transparent" />
        </shape>
    </item>

</selector>

When I zoom out to max level, view is disable(not clickable), but the background is the same. Thanks for your help.

Upvotes: 0

Views: 388

Answers (1)

vipul mittal
vipul mittal

Reputation: 17401

Try changing the order of items:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">


  <item android:state_enabled="false">
        <shape android:shape="rectangle">
            <solid android:color="@android:color/transparent" />
        </shape>
    </item>
    <item android:state_pressed="true">
        <shape android:shape="rectangle">
            <solid android:color="@color/btn_map_bg_selected" />
        </shape>
    </item>

    <item android:state_selected="true">
        <shape android:shape="rectangle">
            <solid android:color="@color/btn_map_bg_selected" />
        </shape>    
    </item>

</selector>

Selector applies the first item that matches the condition, in your case textview must be in selected or pressed state so that item is applied.

Upvotes: 1

Related Questions