6

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?

Zaz Gmy
  • 4,236
  • 3
  • 19
  • 30
user1321683
  • 183
  • 1
  • 2
  • 5
  • Possible duplicate of [Focusable EditText inside ListView](http://stackoverflow.com/questions/2679948/focusable-edittext-inside-listview) – blahdiblah Oct 31 '13 at 23:40

3 Answers3

4

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); 
} 
Community
  • 1
  • 1
Barak
  • 16,318
  • 9
  • 52
  • 84
3

do like this

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

ie,

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

sunil
  • 6,444
  • 1
  • 32
  • 44
0

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

Some one Some where
  • 777
  • 1
  • 8
  • 26