Felix
Felix

Reputation: 89566

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.

Upvotes: 4

Views: 5501

Answers (1)

CommonsWare
CommonsWare

Reputation: 1006614

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.

Upvotes: 7

Related Questions