Focusing an EditText in a ListView

I have a ListView , and in this ListView I have a row with some TextViews and EditTexts in it. When I click anywhere on the line, I want the EditText focus so that the text can be entered.

The problem is that I cannot get this to work. When I put an EditText in a ListView , OnItemClick will not respond when I click on the ListView .

I tried using focusable="false" in EditText , and it allowed me to click on the ListView again, but I could not get EditText to get focus even after setting focusable to true. Then I tried using android:descendantFocusability="beforeDescendants" in the ListView , but it didn't seem to make any changes, it still wouldn't work.

Does anyone have an idea on what I can do to make it work?

+4
source share
3 answers

Try this, it should allow you to click on the line and focus on the EditText (modified from this SO answer ):

 public void onItemSelected(AdapterView<?> listView, View view, int position, long id) { EditText yourEditText = (EditText) view.findViewById(R.id.youredittextid); listView.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS); yourEditText.requestFocus(); } public void onNothingSelected(AdapterView<?> listView) { // onNothingSelected happens when you start scrolling, so we need to prevent it from staying // in the afterDescendants mode if the EditText was focused listView.setDescendantFocusability(ViewGroup.FOCUS_BEFORE_DESCENDANTS); } 
+4
source

do it

 ListView listView = (ListView) findViewById(R.id.yourListViewId); listView.setItemsCanFocus(true); listView.setClickable(false); 

t

after receiving the listview object from xml, disable click on this list and pass focus to child views

+3
source

you can add the property as enabled="false" in the layout of the xml string, and in onItemClickListener you can setEnabled(true) and then setFocusable(true) , this should solve it.

0
source

All Articles