Home » Android » How to disable clicking on ListView in android?

How to disable clicking on ListView in android?

Posted by: admin April 23, 2020 Leave a comment

Questions:

I have a ListView that I’m populating with values from my database. If the database is empty, I’m setting the first item of the ListView to “No data.”. I want to disable clicking on this item. I’ve used ArrayAdapter. I tried making areAllItemsEnabled,isEnabled false, but it was of no use. Even if I set the ListView’s isClickable and setEnabled to false, it is of no use. And I put the code for the OnItemClickListener in the else condition,even that doesn’t stop the list item from being clickable. Does someone have an alternate solution? Thanks!

How to&Answers:

In your custom ArrayAdapter use isEnabled function to return false:

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

always works for me.

Answer:

all the easier! list.setEnabled(false)

Answer:

You can set the empty View as showed and it will be handled automatically:

 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
         android:orientation="vertical"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:paddingLeft="8dp"
         android:paddingRight="8dp">

     <ListView android:id="@android:id/list"
               android:layout_width="match_parent"
               android:layout_height="match_parent"
               android:background="#00FF00"
               android:layout_weight="1"
               android:drawSelectorOnTop="false"/>

     <TextView android:id="@android:id/empty"
               android:layout_width="match_parent"
               android:layout_height="match_parent"
               android:background="#FF0000"
               android:text="No data"/>
 </LinearLayout>

Answer:

create Adapter for that list, and there override this method

public boolean isEnabled(int position);

then return false when you want to disable the click

Answer:

Perhaps you could use an if statement to check the contents of the listview entry in the OnClick, if it contains ‘No data’ do nothing, else do the usual

Answer:

I did like this according to my requirement hope it can help you some how

@Override

public boolean isEnabled(int position) {
        if(data.get(position).isClickable==false)
        {
            return false;
        }
        return super.isEnabled(position);
    }

Answer:

Try setting these two attributes on the ListView:

android:listSelector="@android:color/transparent" 
android:cacheColorHint="@android:color/transparent"