Frage

I am having the following problem. I want to add special image for a tabbuton disabled state but it's not working. This is what I am doing in the selector.

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

<item android:drawable="@drawable/active" android:state_pressed="true"/>
<item android:drawable="@drawable/unactive" android:state_selected=true"/>
<item android:drawable="@drawable/disabled" android:state_enabled="false"/>
<item android:drawable="@drawable/unactive"/>

</selector>

This is exactly the same as selector for a single "standalone" button, but it's not working, it's showing the disabled state even when button is enabled. What am I doing wrong?

EDIT: Thanks to @NightCrawler sugestions I found out that this is the optimal selector for active unactive state, but I still can't make the state_enabled="false" show diffrent state when button is disabled...

<item android:drawable="@drawable/active" android:state_selected="true"/>
<item android:drawable="@drawable/unactive" android:state_selected="false"/>
<item android:drawable="@drawable/disabled" android:state_enabled="false"/>
<item android:drawable="@drawable/unactive"/>

WORKAROUND: I went on to implement the image change for disabled state in code with:

tabWidget.getChildAt(4).setEnabled(true);
icon = (ImageView) wrapper.tabWidget.getChildAt(4).findViewById(R.id.icon);
icon.setImageResource(R.drawable.enabled)

tabWidget.getChildAt(4).setEnabled(false);
icon = (ImageView) wrapper.tabWidget.getChildAt(4).findViewById(R.id.icon);
icon.setImageResource(R.drawable.disabled)

This does work ok but I would still like to know is it possible to define disabled state for button in tabwidget using ONLY XML.

FINDINGS: So after even more tinkering I found out why disabled state is not registered. In my case I am using custom layouts for the buttons, which probably anyone customizing tabhost would do and is probably going to have some sort of image in that layout. Drawable with the states defined above will then be applied to that image in the layout and not the whole layout. Even this selector is enough to have 3 desired states:

<item android:drawable="@drawable/menu_network_active" android:state_selected="true"/>
<item android:drawable="@drawable/menu_network_offline" android:state_enabled="false"/>
<item android:drawable="@drawable/menu_network_unactive"/>

But then when you disable tabwidget button you have to manually set image you are using in it to disabled too like this:

wrapper.tabWidget.getChildAt(0).setEnabled(false);
icon = (ImageView) wrapper.tabWidget.getChildAt(0).findViewById(R.id.icon);
icon.setEnabled(false);

In that case disabled selector state is triggered on the image and desired background is shown.

STILL NOT EXPLAINED:

Only thing left missing in this puzzle is why is the selected state of the image being triggered when tabbutton is selected but not the disabled state. I even tried setting listeners on the "icon" to see if maybe those are called when you change the tab but that is not happening and still image in selector is set to selected. So basically selected is triggered (somehow) on the image in the layout but disabled is not and I have no idea why. If someone looking at this question has some findings please add a comment or answer for feature reference. For those just looking to solve a similar bug just read the WOKRAROUND or FINDINGS parts as these are what will probably solve it for you.

War es hilfreich?

Lösung

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

    <!-- Non focused states -->
    <item android:drawable="@drawable/footer" android:state_focused="false" android:state_pressed="false" android:state_selected="false"/>
    <item android:drawable="@drawable/footer_pressed" android:state_focused="false" android:state_pressed="false" android:state_selected="true"/>

    <!-- Pressed -->
    <item android:drawable="@drawable/footer_pressed" android:state_pressed="true" android:state_selected="true"/>
    <item android:drawable="@drawable/footer_pressed" android:state_pressed="true"/>

</selector>`enter code here`
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top