Listitem click doesn't work with checkboxes Android
Asked Answered
P

3

34

EDIT (SOLVED) For the answer go to the bottom of the question.

I have a listview and I inflate some rows in it using a customadapter. The row being inflated contains a checkbox. I can maintain the checkbox's status, but the problem is, my list items are no more clickable. Here's my code:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    fl=(ListView)findViewById(R.id.MainMenu);//Mainmenu is listview
    fl.setAdapter(new CustomAdapter(Annotate.this,R.layout.preann2,_dir));//preann2 is row and _dir is the list of objects
    //fl.setOnItemClickListener(this); tried this too
}

public void onItemClick(AdapterView<?> a, View view, int pos, long id) 
{
      //Implementations
}

    public class CustomAdapter extends ArrayAdapter<String>
{
    public CustomAdapter(Context context, int textViewResourceId,String[] objects) {
        super(context, textViewResourceId,objects);
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
            LayoutInflater inflater=getLayoutInflater();
            View row=inflater.inflate(R.layout.preann2, parent, false);
            try
            {

            TextView Name=(TextView)row.findViewById(R.id.title);
            Name.setText("xyz");
            ImageView icon=(ImageView)row.findViewById(R.id.icon);
            row.setFocusable(true);
            row.setClickable(true);
            row.setOnClickListener(new OnClickListener(){

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    checkState[position]=!checkState[position];
                }

            });
            CheckBox c=(CheckBox)row.findViewById(R.id.checkBox);
            c.setChecked(checkState[position]);
            c.setOnCheckedChangeListener(new OnCheckedChangeListener(){

                @Override
                public void onCheckedChanged(CompoundButton v,
                        boolean isChecked) {
                    // TODO Auto-generated method stub
                    CheckBox checkBox=(CheckBox)v;
                    if (isChecked) {
                        checkState[position]=true;
                     }
                    else
                        checkState[position]=false;
                }
            });
            /*  tried this too
                                  c.setOnClickListener(new OnClickListener(){

                @Override
                public void onClick(View v) {
                    CheckBox checkBox=(CheckBox)v;
                    if (checkBox.isChecked()) {
                        //checkBox.setChecked(false);
                        checkState[position]=true;
                     }
                    else
                        checkState[position]=false;
                }});*/
            icon.setImageResource(R.drawable.ic_launcher);
            }catch(Throwable err)
            {
                err.printStackTrace();
            }
            return row;
        }
}

I want the listitems to be clickable in case the user wants to click only a single item.

EDIT (on Akhil's request)- preann2.xml in layouts folder:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:orientation="horizontal"
  android:padding="4dip"
  >

 <ImageView android:id="@+id/icon"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:layout_marginRight="4dip"/>


  <TextView
  android:id="@+id/title"
  android:layout_width="fill_parent"
  android:layout_height="match_parent"
  android:layout_weight="1"
  android:ellipsize="end"
  android:gravity="center_vertical"
  android:singleLine="true"
  android:textStyle="bold" />

<CheckBox
  android:id="@+id/checkBox"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content" />

</LinearLayout>

main.xml in layouts folder:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >


<ListView
    android:id="@+id/MainMenu"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

</ListView>

</LinearLayout>

EDIT (SOLVED) Just add these properties to checkbox:

  android:focusable="false"
  android:focusableInTouchMode="false"
Prohibitive answered 7/4, 2013 at 5:16 Comment(9)
If you want the checkBox to be checked if the row is clicked, then put the conditions with the onClickListener() for the row itself.Valedictory
I want some other actions to do when i click the items in the listview. Checkboxes i can set directly by clicking on them, so problems about that. I actually want to call an activity when i click an item.Prohibitive
Try adding a log inside the onClickListener for the checkbox and let us know if the log is registered.Valedictory
I got it solved. Just had to set focusable and focusonTouchMode properties of checkbox to false. Thanks Eitherways.Prohibitive
Please answer your own question and accept it after 2 days. Might help someone else later.Valedictory
I did try to. But as I am new, site doesn't allow me too. have to wait till i get enough repo.Prohibitive
Just edit your question with the solution in it for now.Valedictory
possible duplicate of ListView OnItemClickListener Not Responding?Roselane
possible duplicate of List item with CheckBox not clickableLimulus
R
17

The solution mentioned in the question solves the problem:

android:focusable="false"
android:focusableInTouchMode="false"

AbsListView checks View#hasFocusable and won't allow presses on the item if so. Thus, disable focus on all normally focusable items.

Roselane answered 12/4, 2014 at 4:20 Comment(0)
T
6

Use this line in the rootView of the list item
android:descendantFocusability="blocksDescendants"

Thoroughgoing answered 28/5, 2014 at 18:33 Comment(0)
K
0

I did misunderstood your issue..please have a look here for your issue.. hope it will resolve your problem..

Koerner answered 7/4, 2013 at 5:26 Comment(4)
I have did the same. Textview,icon and checkbox in one layout. Mainmenu listview is in another layout.Prohibitive
Ya i got that..but what i am saying inside your ListItem Layout keep your TextView & Icon in one Layout and set listener on that layout in same way as you did for CheckboxKoerner
i have shared the layouts as an edit. could you show me how..? please?Prohibitive
I got it solved. Just had to set focusable and focusonTouchMode properties of checkbox to false. Thanks eitherways.Prohibitive

© 2022 - 2024 — McMap. All rights reserved.