views:

718

answers:

2

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.

+2  A: 

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.

CommonsWare
I was researching and I just bumped into StateListDrawable myself. While it does pretty much what I want it to do, it's not really perfect. I want to change the background of the ImageView when the LinearLayout is focused, and I can't do that with StateListDrawable. +1 though, for pointing that very useful class out!
Felix
Also, where do I define the StateListDrawables? Do I create XML files inside the drawable/ directory?
Felix
A: 

Also, where do I define the StateListDrawables? Do I create XML files inside the drawable/ directory

Yes

Aamir Yaseen