I've spent about 6 hours on this so far, and been hitting nothing but roadblocks. The general premise is that there is some row in a ListView
(whether it's generated by the adapter, or added as a header view) that contains an EditText
widget and a Button
. All I want to do is be able to use the jogball/arrows, to navigate the selector to individual items like normal, but when I get to a particular row -- even if I have to explicitly identify the row -- that has a focusable child, I want that child to take focus instead of indicating the position with the selector.
I've tried many possibilities, and have so far had no luck.
layout:
<ListView
android:id="@android:id/list"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
/>
Header view:
EditText view = new EditText(this);
listView.addHeaderView(view, null, true);
Assuming there are other items in the adapter, using the arrow keys will move the selection up/down in the list, as expected; but when getting to the header row, it is also displayed with the selector, and no way to focus into the EditText
using the jogball. Note: tapping on the EditText
will focus it at that point, however that relies on a touchscreen, which should not be a requirement.
ListView
apparently has two modes in this regard:
1. setItemsCanFocus(true)
: selector is never displayed, but the EditText
can get focus when using the arrows. Focus search algorithm is hard to predict, and no visual feedback (on any rows: having focusable children or not) on which item is selected, both of which can give the user an unexpected experience.
2. setItemsCanFocus(false)
: selector is always drawn in non-touch-mode, and EditText
can never get focus -- even if you tap on it.
To make matters worse, calling editTextView.requestFocus()
returns true, but in fact does not give the EditText focus.
What I'm envisioning is basically a hybrid of 1 & 2, where rather than the list setting if all items are focusable or not, I want to set focusability for a single item in the list, so that the selector seamlessly transitions from selecting the entire row for non-focusable items, and traversing the focus tree for items that contain focusable children.
Any takers?
The most important part is to get the focus working for the list cell. Especially for list on Google TV this is essential:
setItemsCanFocus method of the list view does the trick:
My list cell xml starts like follows:
nextFocusLeft/Right are also important for D-Pad navigation.
For more details check out the great other answers.
I just found another solution. I believe it's more a hack than a solution but it works on android 2.3.7 and android 4.3 (I've even tested that good old D-pad)
init your webview as usual and add this: (thanks Michael Bierman)
During the getView call:
This helped me.
In your manifest :
Just try this
in the
section of your manifest. Yes, it adjusts nothings, which means the editText will stay where it is when IME is opening. But that's just an little inconvenience that still completely solves the problem of losing focus.
this post was matching exactly my keywords. I have a ListView header with a search EditText and a search Button.
In order to give focus to the EditText after loosing the initial focus the only HACK that i found is:
Lost lots of hours and it's not a real fix. Hope it helps someone tough.
We're trying this on a short list that does not do any view recycling. So far so good.
XML:
Java: