Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EditText focus in a ListView

I have a ListView and in that ListView I have a Row with some TextViews and EditTexts in it. When I press anywhere on the row I want the EditText to take focus so text can be entered.

The problem is that I cannot get this to work. When i place the EditText in the ListView the OnItemClick will not respond when I click on the ListView.

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

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

like image 936
user1321683 Avatar asked Jul 10 '12 13:07

user1321683


1 Answers

Try this, it should let you click on a line and focus 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); 
} 
like image 66
Barak Avatar answered Oct 13 '22 03:10

Barak