Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: change style when focused

I have this custom layout:

- LinearLayout
    - FrameLayout
        - ImageView
    - TextView

This layout reacts to click events (using LinearLayout.setOnClickListener()) and is made focusable using android:focusable="true" in the layout XML file. This all works fine if you're using the touchscreen, but I'm thinking about users who don't use the touchscreen that much and prefer navigation keys or maybe even don't have a touchscreen. These users won't be able to see when that ViewGroup is focused (although it can be focused using the keyboard).

My question is: how can I make a change in that layout when it is focused (I need to change the android:background of the ImageView)? I suppose I could use LinearLayout.setOnFocusChangeListener(), but I'm thinking maybe there's a better way, using just XML files.

like image 420
Felix Avatar asked Oct 16 '25 09:10

Felix


1 Answers

Give something (LinearLayout?) a background image that is a StateListDrawable (drawable XML with <selector> root element). Perhaps use just a transparent color for the normal case, but use something else for the android:focused="true" case. Then, when it gets the focus, the background will change automatically.

like image 163
CommonsWare Avatar answered Oct 18 '25 05:10

CommonsWare