OnItemCLickListener not working in listview

AndroidListview

Android Problem Overview


Activity class code:

conversationList = (ListView)findViewById(android.R.id.list);
ConversationArrayAdapter conversationArrayAdapter=new  ConversationArrayAdapter(this, R.layout.conversation_list_item_format_left, conversationDetails);
conversationList.setAdapter(conversationArrayAdapter);
conversationList.setOnItemClickListener(new AdapterView.OnItemClickListener(){ 
    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
        Log.d("test","clicked");
    }
});
		

The getView function in the Adapter class:

if (v == null) {                  	         	
    LayoutInflater vi = (LayoutInflater)ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    if(leftSideMessageNumber.equals(m.getTo())) {
        v = vi.inflate(R.layout.conversation_list_item_format_left, null);
    } else {
        v = vi.inflate(R.layout.conversation_list_item_format_right, null);
    }
}

Is there a problem with using two xmls while inflating?

Android Solutions


Solution 1 - Android

I just found a solution from here, but by deep clicking.

If any row item of list contains focusable or clickable view then OnItemClickListener won't work.

The row item must have a param like android:descendantFocusability = "blocksDescendants".

Here you can see an example of how your list item should look like. Your list item xml should be... row_item.xml (your_xml_file.xml)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:baselineAligned="false"
    android:descendantFocusability="blocksDescendants"
    android:gravity="center_vertical" >

    // your other widgets here

</LinearLayout>

Solution 2 - Android

The problem is that your layouts contain either focusable or clickable items. If a view contains either focusable or clickable item the OnItemCLickListener won't be called.

Click here for more information.

Please post one of your layout xmls if that isn't the case.

Solution 3 - Android

For my lists, my rows have other things that can be clicked, like buttons, so doing a blanket blocksDescendants doesn't work. Instead I add a line in the button's xml:

    android:focusable="false"

That keeps the buttons from blocking the clicks on the rows, but still lets the buttons take the clicks, too.

Solution 4 - Android

you need to do 2 steps in your listview_item.xml

  1. set the root layout with: android:descendantFocusability="blocksDescendants"
  2. set any focusable or clickable view in this item with:
    android:clickable="false"
    android:focusable="false"
    android:focusableInTouchMode="false"

Here is an example: listview_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="10dp"
    android:layout_marginTop="10dp"
    android:paddingLeft="10dp"
    android:paddingRight="10dp"
    android:gravity="center_vertical"
    android:orientation="vertical"
    android:descendantFocusability="blocksDescendants">

    <RadioButton
        android:id="@+id/script_name_radio_btn"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textStyle="bold"
        android:textColor="#000"
        android:padding="5dp"
        android:clickable="false"
        android:focusable="false"
        android:focusableInTouchMode="false"
        />

</LinearLayout>

Solution 5 - Android

use the below code inside button tag in custom row layout of listview

 android:focusable="false"
 android:clickable="false"

Solution 6 - Android

I had the same problem and I just saw I had accidentally set:

@Override
public boolean isEnabled(int position)
{
	return false;
}

on my CustomListViewAdapter class.

By changing this to:

return true;

I've managed to fix the problem. Just in case if someone has done the same mistake...

Solution 7 - Android

Use android:descendantFocusability

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="80dip"
    android:background="@color/light_green"
    android:descendantFocusability="blocksDescendants" >

Add above in root layout

Solution 8 - Android

I solved it with the help of this answer

1.Add the following in Linear Layout of list_items.xml android:descendantFocusability="blocksDescendants"

2.Child Views of LinearLayout in list_items.xml

 android:focusable="false" 

Solution 9 - Android

if you have textviews, buttons or stg clickable or selectable in your row view only

android:descendantFocusability="blocksDescendants"

is not enough. You have to set

android:textIsSelectable="false"

to your textviews and

android:focusable="false"

to your buttons and other focusable items.

Solution 10 - Android

Even I was having the same problem, I am having checkbox, did the following to masker itemClickListener work,

Added the following properties to the checkbox,

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

and ItemClickListner started working.

For detailed example you can go through the link,

http://knowledge-cess.com/android-itemclicklistner-with-checkbox-or-radiobutton/

Hope it helps Cheers!!

Solution 11 - Android

I had the same problem and tried all of the mentioned solutions to no avail. through testing i found that making the text selectable was preventing the listener to be called. So by switching it to false, or removing it my listener was called again.

android:textIsSelectable="false"

hope this helps someone who was stuck like me.

Solution 12 - Android

  1. Add this in main Layout

    android:descendantFocusability="blocksDescendants"
    
  2. Write this code into every button,Textview,ImageView etc which have onClick

    android:focusable="false"
    
    android:clickable="false"
    

Hope it will work.

Solution 13 - Android

Two awesome solutions were this, if your extending ListFragment from a fragment, know that mListView.setOnItemClickListener wont be called before your activity is created, this ensured it is set when activity has been created

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    mListView.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long rowId) {
            // Do the onItemClick action

            Log.d("ROWSELECT", "" + rowId);
        }
    });
}

While looking at the source code for ListFragment, I came across this

public class ListFragment extends Fragment {
    ...........................................
    ................................................
        
    final private AdapterView.OnItemClickListener mOnClickListener
                = new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
            onListItemClick((ListView)parent, v, position, id);
        }
    };
       
    ................................................................
    ................................................................
    
    public void onListItemClick(ListView l, View v, int position, long id) {
    }
}

An onItemClickListener object is attached and it calls onListItemClick() As such the other similar solution, which works in the exact same way is to override onListItemClick()

@Override
public void onListItemClick(ListView l, View v, int position, long rowId) {
    super.onListItemClick(l, v, position, id);
   // Do the onItemClick action

   Log.d("ROWSELECT", "" + rowId);
} 

Solution 14 - Android

in my case none of xml layout properties was not helpful.

I just add a single line of code like this: convertView.setClickable(false);

@NonNull
@Override
public View getView(final int position, View convertView, @NonNull ViewGroup parent) {
    ViewHolder viewHolder;
    if (convertView == null || convertView.getTag() == null) {
        LayoutInflater inflater = LayoutInflater.from(context);
        convertView = inflater.inflate(R.layout.my_layout_id, parent, false);
        viewHolder = new ViewHolder(convertView);
        convertView.setTag(viewHolder);
    } else {
        viewHolder = (ViewHolder) convertView.getTag();
    }
    ...
    convertView.setClickable(false);
    return convertView;
}

so basically it do the same thing as setting up properties in xml layout but it was only thing which works in my case.

It is not perfect timing but maybe it will helps somebody Happy coding

Solution 15 - Android

I've tried all the above and NOTHING worked.

I solved the problem as follows:

First I define a custom Button called ListButton

public class ListButton extends android.widget.Button
{

private ButtonClickedListener clickListener;

public ListButton(Context context)
{
    this(context, null);
}

public ListButton(Context context, AttributeSet attrs)
{
    this(context, attrs, 0);
}

public ListButton(Context context, AttributeSet attrs, int defStyle)
{
    super(context, attrs, defStyle);
}

public void setClickListener(ButtonClickedListener listener) {
	this.clickListener = listener;
}

@Override
public boolean isInTouchMode() {
	return true;
}

@Override
public boolean onTouchEvent(MotionEvent event) {
	
	return false;
}

@Override
public boolean dispatchTouchEvent(MotionEvent event) {
	
	switch (event.getAction()) 
      {
          case MotionEvent.ACTION_DOWN:
        	  break;
          case MotionEvent.ACTION_UP:
        	  
        	  eventClicked();

              break;
          case MotionEvent.ACTION_CANCEL:
        	  break;
          case MotionEvent.ACTION_MOVE:
        	  break;
          default :
        	  
      }
	return true;
}

private void eventClicked() {
	if (this.clickListener!=null) {
		this.clickListener.ButtonClicked();
	}
}

}

The XML looks like:

<dk.example.views.ListButton
android:id="@+id/cancel_button"
android:layout_width="125dp"
android:layout_height="80dp"
android:text="Cancel"
android:textSize="20sp" 
android:layout_margin="10dp"
android:padding="2dp"
android:background="#000000"
android:textColor="#ffffff"
android:textStyle="bold"  
/>

Then I define my own ButtonClicked Listener interface:

public interface ButtonClickedListener {
    public void ButtonClicked();
}

Then I use my own listener just as if it was the normal OnClickListener:

final ListButton cancelButton = (ListButton) viewLayout.findViewById(R.id.cancel_button);

	cancelButton.setClickListener(new ButtonClickedListener() {

		@Override
		public void ButtonClicked() {
			//Do your own stuff here...
		}
		
	});

Solution 16 - Android

Android:autoText attribute also makes TextView auto focusable.

Solution 17 - Android

I had the same issue, I was using a style for my texts in the row layout that had the "focusable" attribute. It worked after I removed it.

Solution 18 - Android

In my case, I had to remove the next line from the Layout

android:clickable="true"

Solution 19 - Android

If you want to use both the simple click and long click on list view items better way to implement that would be to use context menu for long click. Avoid using setItemLongClickListener especially if you have multiple row layouts for your listview.

Solution 20 - Android

Faced same problem, tried for hours. If you have tried all of the above than try changing layout_width of Listview and list item to match_parent from wrap_content.

Solution 21 - Android

All of the above failed for me. However, I was able to resolve the problem (after many hours of banging my head - Google, if you're listening, please consider fixing what I encountered below in the form of compiler errors, if possible)

You really have to be careful of what android attributes you add to your xml layout here (in this original question, it is called list_items.xml). For me, what was causing the problem was that I had switched from an EditText view to a TextView and had leftover attribute cruft from the change (in my case, inputType). The compiler didn't catch it and the clickability just failed when I went to run the app. Double check all of the attributes you have in your layout xml nodes.

Solution 22 - Android

private AdapterView.OnItemClickListener onItemClickListener;

@Override
public View getView(final int position, View convertView, final ViewGroup parent) {
 .......

final View view = convertView;
convertView.setOnClickListener(new View.OnClickListener() {
    @Override
     public void onClick(View v) {
         if (onItemClickListener != null) {
             onItemClickListener.onItemClick(null, view, position, -1);
         }
      }
 });

return convertView;
}

public void setOnItemClickListener(AdapterView.OnItemClickListener onItemClickListener) {
    this.onItemClickListener = onItemClickListener;
}

Then in your activity, use adapter.setOnItemClickListener() before attaching it to the listview.

Copied from github its worked for me

Solution 23 - Android

The thing that worked for me was to add the below code to every subview inside the layout of my row.xml file:

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

So in my case:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <TextView
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:id="@+id/testingId"
        android:text="Name"
       //other stuff 
        />

    <TextView
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:id="@+id/dummyId"
        android:text="icon"
        //other stuff 
        />

    <TextView
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:id="@+id/assignmentColor"
       //other stuff 
       />
     
    <TextView
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:id="@+id/testID"
        //other stuff 
        />

    <TextView
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:text="TextView"
        //other stuff 
        />
        
</android.support.constraint.ConstraintLayout>

And this is my setOnItemClickListener call in my Fragment subclass:

CustomListView = (PullToRefreshListCustomView) layout.findViewById(getListResourceID());
        CustomListView.setAdapter(customAdapter);
        CustomListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Log.d("Testing", "onitem click working");
              //  other code
            }

        });

I got the answer from here!

Solution 24 - Android

Had the same problem with onClick. The solution was to remove from the xml the following

android:tooltipText=""

Solution 25 - Android

I solved the problem by removing the clickable views from the list.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionhieiView Question on Stackoverflow
Solution 1 - AndroidBhavesh HirparaView Answer on Stackoverflow
Solution 2 - AndroidbalazsbalazsView Answer on Stackoverflow
Solution 3 - AndroidJanene PappasView Answer on Stackoverflow
Solution 4 - AndroidEddyView Answer on Stackoverflow
Solution 5 - AndroidMilan ShuklaView Answer on Stackoverflow
Solution 6 - AndroidWoodyView Answer on Stackoverflow
Solution 7 - AndroidAmar GoreView Answer on Stackoverflow
Solution 8 - AndroidRAJESH KUMAR ARUMUGAMView Answer on Stackoverflow
Solution 9 - AndroidsavepopulationView Answer on Stackoverflow
Solution 10 - AndroidGokul KulkarniView Answer on Stackoverflow
Solution 11 - Androidu2tallView Answer on Stackoverflow
Solution 12 - AndroidWajid khanView Answer on Stackoverflow
Solution 13 - AndroidOtieno RowlandView Answer on Stackoverflow
Solution 14 - AndroidMarek SkóraView Answer on Stackoverflow
Solution 15 - AndroidAndreasReiffView Answer on Stackoverflow
Solution 16 - AndroidYury FinchenkoView Answer on Stackoverflow
Solution 17 - AndroidMiguel RdzView Answer on Stackoverflow
Solution 18 - AndroidIgniteCodersView Answer on Stackoverflow
Solution 19 - AndroidSamuel RobertView Answer on Stackoverflow
Solution 20 - AndroidprashantView Answer on Stackoverflow
Solution 21 - AndroidTed_ZactlyView Answer on Stackoverflow
Solution 22 - AndroidMuhammad WaleedView Answer on Stackoverflow
Solution 23 - AndroidspcarlinView Answer on Stackoverflow
Solution 24 - AndroidBPHView Answer on Stackoverflow
Solution 25 - AndroidsidKhalidView Answer on Stackoverflow