34

I implemented the android listview with the ListActivity. Here I have the problem that when i click on the list item no action is performed when the flash color is also not coming that is the orange color. So do you have any idea about this kindly answer to my question.

@Override
protected void onListItemClick(ListView l, View v, int position, long id) 
{
    super.onListItemClick(l, v, position, id);
    Toast.makeText(getApplicationContext(), "msg msg", Toast.LENGTH_SHORT)
            .show();

}

I put this code also into the Main ListActivity.

MKJParekh
  • 34,073
  • 11
  • 87
  • 98
Rajesh Rajaram
  • 3,271
  • 4
  • 29
  • 48

10 Answers10

125

The first thing what you have to note here is, whenever there are Clickable elements like Buttons or ImageButtons present in your ListView element, they take the control of click events. And so your ListView won't get the chance to accept the click event.

What you simply have to do is, set the focusable attribute to false for the Button or ImageButton you have in your ListView. But still they will work without any problem and also your ListView's onListItemClick will also work.

Try this,

        <Button  android:id="@+id/textsize_increaser"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/back_button"
        android:focusable="false"
        android:text=" A + "/>

Here I have added this android:focusable="false" and it works fine. try it.

Andro Selva
  • 53,910
  • 52
  • 193
  • 240
  • 3
    You should add also `clickable=false` if you listen to item click only – mente Dec 12 '13 at 08:43
  • Please help me. I have been stuck on this for the last 7 hours now, and it is such a simple question. http://stackoverflow.com/questions/35108940/why-cant-i-remove-an-item/35109304#35109304 – Ruchir Baronia Jan 31 '16 at 03:09
  • 4
    For me, `imageButton.setFocusable(false)` and `imageButton.setFocusableInTouchMode(false)` **in adapter** helped. In XML there was no effect! – user2878850 Oct 06 '17 at 20:13
  • you saved my @ss, thank you from the future, stranger! :) – Teekam Suthar Jun 06 '21 at 11:38
12

Have you set the choice mode of ListView to SINGLE :

     listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);

And if you have any clickable imageview or textview or button in the list item, then make them not focusable (in your Adapter class):

        yourButton.setFocusable(false);
        yourButton.setFocusableInTouchMode(false);
Yogesh Somani
  • 2,624
  • 3
  • 21
  • 34
  • Please help me. I have been stuck on this for the last 7 hours now, and it is such a simple question. http://stackoverflow.com/questions/35108940/why-cant-i-remove-an-item/35109304#35109304 – Ruchir Baronia Jan 31 '16 at 03:09
4

Are you using custom Adapter? and inflating layout with button or any view that eats away the list list view focus as child, then it won't work obviously. make sure to set

    android:focusable="false"

to such view in xml file. hope this works for you.

vinay kumar
  • 1,451
  • 13
  • 33
  • Please help me. I have been stuck on this for the last 7 hours now, and it is such a simple question. http://stackoverflow.com/questions/35108940/why-cant-i-remove-an-item/35109304#35109304 – Ruchir Baronia Jan 31 '16 at 03:09
4

Set this in your listactivity java file

listview1.setFocusable(false);
Mehul
  • 71
  • 4
2

Actually there is a parameter meant for that to prevent children views from getting focus, just add the following in the parent layout:

android:descendantFocusability="blocksDescendants"

As the documentation explains:

The ViewGroup will block its descendants from receiving focus.

Yoann Hercouet
  • 17,894
  • 5
  • 58
  • 85
1

Eclipse suggested me to add textIsSelectable="true" to my TextViews in the layout xml which was used for list view.

Well, if you want to click the items in the list then you should not add those tags.

Mika
  • 1,419
  • 18
  • 37
1

make sure that you are

  1. Not using Scroll View with List View
  2. Not using Scroll View in your row item layout for List View

If Scroll View is present at any of above place remove it

Dhaval Jotaniya
  • 1,193
  • 2
  • 10
  • 19
0

refer to this post for a solution:

Click is not working on the Listitem Listview android

View v = parent.getChildAt(position);
parent.requestChildFocus(v,view);
v.setBackground(res.getDrawable(R.drawable."Some drawable for clicked row"));

int count = parent.getChildCount();
for(int i=0; i<count; i++)
{
   if(i!=position)
   {
     v = parent.getChildAt(i);
     v.setBackground(res.getDrawable(R.drawable."some drawable for not clicked row));
   }
}
Community
  • 1
  • 1
Wes
  • 79
  • 1
  • 7
0
listview.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View v, int pos,
                    long id) {
                Toast.makeText(v.getContext(), exm.get(pos).getDefinition(),
                        Toast.LENGTH_SHORT).show();


            }
        });
Rons Jackos
  • 75
  • 4
  • 9
0
listItemButton.setFocusable(false);
listItemButton.setFocusableInTouchMode(false);

Set the above in your adapter. It's not working in XML

Nipun
  • 990
  • 1
  • 16
  • 25