1

i want to highlight the currently selected image in gallery. is der any way to have that?

Thanks, Nital

Nital
  • 975
  • 3
  • 9
  • 15
  • This [SO answer](http://stackoverflow.com/a/5806001/10119) is a good write-up, with explanation, and credit to the original author. – Charley Rathkopf Mar 12 '12 at 18:15

1 Answers1

1

Look at the StateListDrawable

EDIT:

You can use the mergeDrawableStates() method of the View class to provide your custom states:

http://code.google.com/android/reference/android/view/View.html#mergeDrawableStates(int[],%20int[])

Try the steps below to create new custom states:

1) Define the state resources in res/values/attrs.xml

<declare-styleable name="MyCustomState">
        <attr name="state_fried" format="boolean" />
        <attr name="state_baked" format="boolean" />
    </declare-styleable>

2) In your custom view class :

A) Declare the state variables:

private static final int[] FRIED_STATE_SET = {
        R.attr.state_fried
   };

   private static final int[] BAKED_STATE_SET = {
        R.attr.state_baked
   };

B) Override the onCreateDrawableState() method:

 @Override
    protected int[] onCreateDrawableState(int extraSpace) {
        final int[] drawableState = super.onCreateDrawableState(extraSpace + 2);
        if (isFried()) {
            mergeDrawableStates(drawableState, FRIED_STATE_SET);
        }
        if (isBaked()) {
            mergeDrawableStates(drawableState, BAKED_STATE_SET);
        }
        return drawableState;
    }

Once this is done, you should be able to use these in ColorStateListDrawable, but you should use your app's namespace to use these new states:

<selector xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res/<my_app_package>">

<item android:drawable="@drawable/item_baked" state_baked="true"
state_fried="false" />
<item android:drawable="@drawable/item_fried" state_baked="false"
state_fried="true" />
<item android:drawable="@drawable/item_overcooked" state_baked="true"
state_fried="true" />
<item android:drawable="@drawable/item_raw" state_baked="false"
state_fried="false" />
</selector>
evilone
  • 22,410
  • 7
  • 80
  • 107