user1321683
user1321683

Reputation: 183

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?

Upvotes: 6

Views: 7031

Answers (3)

Barak
Barak

Reputation: 16393

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); 
} 

Upvotes: 4

sunil
sunil

Reputation: 6604

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

Upvotes: 3

Some one Some where
Some one Some where

Reputation: 777

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.

Upvotes: 0

Related Questions