Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Focus in GridView layout

In my Android app, I have a GridView layout with a set of Buttons. The problem is, that I can't set properly the focus (I mean cursor, controlled with joystick on the Android device) on the buttons. I can describe my problem with a picture:

Focus in GridView layout

I set the Buttons to the GridView dynamically in the code in BaseAdapter using LayoutInflater. In the code, I set button's text, image (CompoundDrawable) and background color, because my buttons works like a checkboxes (blue background means checked).

It seems like the program is focusing the grid field and not the button itself. I mean something like cell of table and not the button in table. Because the focus color is default (green) however I set the blue color in selector. Focus press also doesn't work. And the focus is evidently behind the button, out of button's bounds. Can somebody help me with this trouble please? Code of my XML layouts:

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 focus parametres in Button and also in GridView and tried a lot of things, but unfortunately it still doesn't work :(

like image 995
petrnohejl Avatar asked Dec 17 '22 15:12

petrnohejl


1 Answers

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

like image 156
petrnohejl Avatar answered Dec 19 '22 06:12

petrnohejl