Android custom ListView with ImageButton is not getting focus
Asked Answered
L

6

18

I am developing an app for android TV. I have a listview with an ImageButton + Textviews as children. As TV does not take touch events but uses D-PAD or trackball remote I have to get focus on all clickable items so that user knows where the focus is. But the problem is I can get focus on list row if I add

android:descendantFocusability="blocksDescendants"

in the custom list row layout but ImageButton in the list row will not get focus. And if I remove above code then ImageButton gets focus but I can't click list row.

Also I added setItemsCanFocus for my ListView but no luck, nothing changed. I read many documents but i could not find a proper answer for this problem. I want focus on both List row as well as ImageButton. Is it possible, if any one knows please help me...

Limey answered 6/3, 2013 at 14:15 Comment(0)
P
24

I have to get focus on all clickable items so that user knows where the focus is. But the problem is I can get focus on list row if I add

View getting hang up due to multiple views inside row file, to get click You have to add one more property to your main layout of row xml.

android:descendantFocusability="blocksDescendants

But when I write above property then I'm not able to focus ImageButton which is in my row file.

I just tried one way to acheive to get foucus on ImageButton as well as on listview.

To get focus on ImageButton:

  1. Create one selector file.
  2. Applied on Imagebutton.
  3. Added android:focusableInTouchMode="true" to ImageButton.

To get focus on Listview:

  1. Add android:descendantFocusability="blocksDescendants to main-layout within row xml.

Now when you click on your ImageButton it will focus with your desire color even it will focus on listview click too.

Selecotor.xml

<?xml version="1.0" encoding="utf-8"?>
<selector 
    xmlns:android="http://schemas.android.com/apk/res/android"
    >
    <item 
        android:state_pressed="true"
        android:drawable="@android:color/transparent" 
        /> <!-- pressed -->    
    <item 
        android:state_focused="true"
        android:drawable="@android:color/darker_gray" 
        /> <!-- focused -->    
    <item 
        android:drawable="@android:drawable/btn_star" 
        /> <!-- default -->
</selector>

Tested in S3 emulator, works well.

Pga answered 1/4, 2013 at 12:23 Comment(2)
I did all the above things but its not working in nexus 7 emulator as well as in TV. I think its because there is no touch screen for TV so android:focusableInTouchMode="true" is not workingLimey
Is focus going to list row and button...?Limey
S
6

I had the same problem before. I solved my problem with adding (to the layout in xml)

android:focusableInTouchMode="true"
android:clickable="true"

So my problem was resolved.

Note: Adding only android:focusableInTouchMode="true" doesn't work, You must have to make the layout clickable true, after focusableInTouchMode true.

Sieve answered 2/4, 2013 at 12:54 Comment(0)
H
4

I had the same problem in listView but I found the solution,simply use imageView instead of image button, then row will be clickable and focusable as well.

NOTE: when ever we use button or imagebutton in listView the row of the list view gets disable to prevent this from happening use view or imageview instead.

Hope so this will help you.

Heliport answered 2/4, 2013 at 13:29 Comment(1)
I couldn't get the ImageButton to work even with all the suggested comments, but this worked.Allegedly
M
3

Add below code to the parent layout of custom row,

android:descendantFocusability="blocksDescendants"

and add below code to any button or ImageButton in that layout

android:focusable="false"
android:focusableInTouchMode="false"
Mosaic answered 14/2, 2014 at 7:22 Comment(0)
T
1

Have you tried :

setFocusableInTouchMode(false);

or

setFocusable(false);

On both your items (ImageButton and TextView) ?

Thready answered 6/3, 2013 at 15:52 Comment(1)
Thanks for your time but if user can't focus then with remote control user won't know if he is on the correct ImageButton...Limey
P
0

List view items not not focusable with Android Tv Remote. This is how I solved it.

If you are using List View or Recycler View. In OnBindView method. root layout of single item of listview/reyclerview

rootLayout.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {
                if(b){  
                  rootLayout.setCardBackgroundColor(context.getColor(R.color.grey));
                }else { 
                 rootLayout.setCardBackgroundColor(context.getColor(R.color.white));
                }
            }
        });
Pygmy answered 21/7, 2022 at 17:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.