Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to specify different images when my custom widget has focus?

I am trying to setup a selector drawable in my asset file like this: * 2 different images for 'on'/'off' mode in rest mode (i.e. it does not has focus) * another 2 different images for 'on'/'off' mode in focus mode (i.e. it has the focus).

In my case, the 'rest' mode works, but the 'focus' mode does not.

Can you please tell me what am I missing in the focus mode? Thank you.

<!-- these 2 images works --->
<item android:state_checked="false" android:drawable="@drawable/off" />
<item android:state_checked="true" android:drawable="@drawable/on" />


<!-- these 2 images does not work -->

 <item android:state_focused="true" android:state_checked="false"
android:drawable="@drawable/off_focus" />

 <item android:state_focused="true" android:state_checked="true"
android:drawable="@drawable/on_focus" />
like image 324
yinglcs Avatar asked Feb 16 '09 00:02

yinglcs


1 Answers

I don't believe that instances of ImageView are focusable by default, meaning that they would never take focus and your selector would never be triggered. In the place where you define your ImageView you need to set it to be focusable, see these methods: setFocusable and setFocusableInTouchMode. It can be set from XML or in code.

like image 177
satur9nine Avatar answered Oct 15 '22 21:10

satur9nine