1

My custom ListView is not clickable. I am using setOnItemClickListener. My ListView row contains a button, image view and some textviews. Please help me make the items clickable.

Vatev
  • 7,493
  • 1
  • 32
  • 39
Jose Kurian
  • 703
  • 7
  • 10

2 Answers2

0

set clickable=false to all clickable views in listview row like (button).

Brijesh Patel
  • 676
  • 1
  • 5
  • 13
0

in case if you use controls like ImageButton, CheckBox ,Button etc then you would face problems discussed here and here.

This is simply because such controls can steal focus from the ListView and the complete list item cant be selected/clicked.

I am assuming that you are using an Adapter to set the content of your list. Inside that adapter you can assign onClickListener()s for each item like this:

public class MyListAdapter extends ArrayAdapter<String>{
    .......
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
         View rowView=inflator.inflate(R.layout.list_item, null, true);
         ImageView image=(ImageView) rowView.findViewById(R.id.image1);
         image.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                 //do something
            }
         }

         Button button=(ImageView) rowView.findViewById(R.id.button1);
         button.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                 //do something
            }
         }
         .......
    }
}

but remember that while using controls like ImageButton, CheckBox ,Button you need to assign property android:focusable="false" in the XML. and for the ImageButton you need to do this inside the getView() method:

    final ImageButton imgBtn=(ImageButton) rowView.findViewById(R.id.imgBtn);
    imgBtn.setFocusable(false);
    imgBtn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            //do your task here
        }
    });

Hope I answered your question.

Community
  • 1
  • 1
SMR
  • 6,628
  • 2
  • 35
  • 56