4

I am trying to change tab icon when state changes. All is working properly if I use drawable from following xml:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:drawable="@drawable/d_selected"
            android:state_selected="true" />
        <item android:drawable="@drawable/d_normal" />
</selector>

However, now I need to load images from data/data folder and drawables "d" and "dSel" are generated from these images. Using following code only "dSel" is shown and other tab images does not appear! Thank you

    ImageView icon = (ImageView) tabIndicator.findViewById(R.id.icon);

                selector.addState(new int[]{ android.R.attr.state_pressed }, d);
                selector.addState(new int[]{ android.R.attr.state_selected }, dSel);
                selector.addState(new int[]{ android.R.attr.state_focused }, d);

          icon.setImageDrawable(selector);

    //icon.setImageResource(drawableId); used with other method described, if related to xml
Jaume
  • 3,672
  • 19
  • 60
  • 119
  • try this `ImageView icon = (ImageView) getTabWidget().getChildAt(0).findViewById(android.R.id.icon); icon .setImageDrawable(getResources().getDrawable(R.drawable.state_pressed));` – ρяσѕρєя K Mar 31 '12 at 22:05
  • mmm and where are you assigning the image to state_press? – Jaume Apr 01 '12 at 09:09

1 Answers1

1

Finally I found the solution. True/false for each state is handled by "-".

selector.addState(new int[]{ android.R.attr.state_selected }, dSel);
selector.addState(new int[]{ -android.R.attr.state_selected }, d);

refer to this link for more information, Android : How to update the selector(StateListDrawable) programmatically

Community
  • 1
  • 1
Jaume
  • 3,672
  • 19
  • 60
  • 119