14

In each ListView item I have EditText.

When I set android:focusable="false" for EditText then onItemClick on the ListView item is working, but EditText doesn't get cursor when I click inside.

If I'll set android:focusable="true" for EditText, then EditText is focusable, but onItemClick for the ListView doesn't work when I click on it.

How to separate onItemClick and focusable EditText in this item?

Renjith
  • 5,783
  • 9
  • 31
  • 42
Sviatoslav
  • 1,301
  • 2
  • 17
  • 42
  • And what about if your remove `android:focusable` attribute from EditText? – user370305 Aug 23 '12 at 11:08
  • I think it happens because when you apply `android:focusable="true"` to EditText then the `onItemClick` of ListItem dispatch to only EditText. And the Event is not passed to it Parent List Item View. – user370305 Aug 23 '12 at 11:12
  • @user370305 behavior is the same as android:focusable="true". i.e. `EditText` is focusable, but `onItemClick` for the `ListView` doesn't work when I click on it. – Sviatoslav Aug 23 '12 at 11:15
  • @user370305 I think so to :) But how to overcome it? – Sviatoslav Aug 23 '12 at 11:18
  • Leave the `EditText` without any focusable attribute and then add to the activity `android:windowSoftInputMode="adjustPan"` in the manifest. This should solve the focus problems. – user Aug 23 '12 at 11:39
  • possible duplicate of [Android: EditText in ListView](http://stackoverflow.com/questions/2825571/android-edittext-in-listview) – user Aug 23 '12 at 11:39
  • @user370305 I can't give the code :( – Sviatoslav Aug 23 '12 at 11:41
  • @Luksprog my `ListView` is in `Fragment` that I put in `ViewPager` that I am using in FragmentActivity. For this FragmentActivity in `Manifest` I set `` and when I deleted focusable attribute it didn't helped. – Sviatoslav Aug 23 '12 at 11:55
  • 3
    Can you try this, `setOnTouchListener()` to `EditText` and override `onTouch()` in onTouch just `return false`... ? And let me know what happen... – user370305 Aug 23 '12 at 11:58
  • @Luksprog you can write in `EditText` and your `onItemClick` is working too? – Sviatoslav Aug 23 '12 at 12:21
  • No, I'm sorry I misread your question. – user Aug 23 '12 at 14:08

3 Answers3

15

Thanks @user370305 for the idea with OnTouchListener. Now it is working for me by using setOnTouchListener():

public class AdapterListCards extends CursorAdapter implements View.OnTouchListener {
 public AdapterListCards(Context context) {
    super(context, null, true);
 }

 @Override
 public boolean onTouch(View view, MotionEvent motionEvent) {
    if (view instanceof EditText) {
        EditText editText = (EditText) view;
        editText.setFocusable(true);
        editText.setFocusableInTouchMode(true);
    } else {
        ViewHolder holder = (ViewHolder) view.getTag();
        holder.edtCode.setFocusable(false);
        holder.edtCode.setFocusableInTouchMode(false);
    }
    return false;
 }

 private class ViewHolder {
    TextView txtName;
    EditText edtCode;
}

 @Override
 public View newView(final Context context, Cursor cursor, ViewGroup parent) {
    View convertView = LayoutInflater.from(context).inflate(R.layout.list_item, parent, false);
    final ViewHolder holder = new ViewHolder();
    holder.txtName = (TextView) convertView.findViewById(R.id.txt_name);
    holder.edtCode = (EditText) convertView.findViewById(R.id.pass);
    holder.edtCode.setOnTouchListener(this);
    convertView.setOnTouchListener(this);
    convertView.setTag(holder);

    return convertView;
 }

@Override
public void bindView(View view, Context context, Cursor cur) {
    ViewHolder holder = (ViewHolder) view.getTag();
    if (cur!=null) holder.txtName.setText(cur.getString(cur.getColumnIndex("name")));
 }
}

and of course: android:windowSoftInputMode="adjustPan" for activity in the manifest.

user370305
  • 108,599
  • 23
  • 164
  • 151
Sviatoslav
  • 1,301
  • 2
  • 17
  • 42
  • 2
    Good. Now you can accept your own answer. And +1 from me for implement my suggestion and post it as solution so other users can also find this helpful. – user370305 Aug 23 '12 at 17:26
1

My solution:

@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
    ViewHolder vh = (ViewHolder) view.getTag();
    //vh.row is the convertView in getView or you may call it the row item itself
    ((ViewGroup)vh.row).setDescendantFocusability(view instanceof EditText?ViewGroup.FOCUS_AFTER_DESCENDANTS:ViewGroup.FOCUS_BLOCK_DESCENDANTS);
    return false;
}

And add

android:descendantFocusability="blocksDescendants"

to the view group in the layout xml file for the list row item.

Pui Ho Lam
  • 232
  • 1
  • 2
  • 11
1

The solution that worked for me was to use the EditText with TextView and a flag in adater indicating edit mode enabled / disabled.

I added the TextView and the EditText in the same position in a relative layout, meaning they would be overlapping one another. The EditText will be hidden by default and TextView will be displayed. In this the item click of listview works flawlessly.

Then I assigned touch listener for TextView and onTouch I hide the textview and display the EditText and requestfocus enabling the input. Even the entire view's on click will request focus for EditText. So no item click shall be enabled.

I have a button in actionbar "Done". Once I click it I update my flag in Adapter saying "modeEdit = false" and call notifydatasetchanges.

In GetView we have check for the flag and if modeEdit = false, then display TextView.

This was only best solution I got to be working!!!

Hope this helps. Let me know if anyone is interested in the working copy of source code.