Focus in GridView Layout

In my Android application, I have a GridView layout with a set of buttons. The problem is that I can’t set the focus correctly (I mean the cursor controlled by the joystick on the Android device) on the buttons. I can describe my problem with the image:

Focus in GridView layout

I set the buttons in the GridView dynamically in the BaseAdapter code using a LayoutInflater. In the code, I set the button text, image (CompoundDrawable) and background color, because my buttons work like checkboxes (blue background icons are checked).

It seems that the program focuses the grid field, not the button itself. I mean something like a table cell, not a button in a table. Since the default focus color is (green), I set the color blue to the selector. Focus pressing also does not work. And the focus is obviously behind the button, from beyond the borders of the buttons. Can someone help me with this problem please? The code for my XML layouts is:

main.xml:

... <GridView android:id="@+id/channels" android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="6dip" android:verticalSpacing="10dip" android:horizontalSpacing="10dip" android:numColumns="auto_fit" android:columnWidth="60dip" android:stretchMode="columnWidth" android:gravity="center" android:background="@color/container_main"> </GridView> ... 

channel.xml:

 <Button android:id="@+id/channel" android:layout_height="fill_parent" android:layout_width="fill_parent" android:gravity="center_horizontal" android:background="@color/willBeSetInAdapter" <!-- white/blue/darkblue focus background --> android:drawableTop="@drawable/willBeSetInAdapter" <!-- icon --> android:drawablePadding="0dip" android:text="WillBeSetInAdapter" <!-- label text --> android:textColor="@color/text_container_main" android:textSize="12sp" android:focusable="true" android:focusableInTouchMode="false"> </Button> 

I tried to set the focus parameters in Button as well as in GridView and tried a lot of things, but unfortunately it still does not work :(

+4
source share
2 answers

Setting android: descendantFocusability = "afterDescendants" for GridView solved my problem . Because the program focused the grid field, not the button itself. Using the descendantFocusability parameter, I prohibit focusing the grid field and allow only the buttons to focus.

+10
source

Have you tried setting android:drawSelectorOnTop="true" for your GridView ?

+1
source

All Articles