How to disable clicking on ListView in android?
Asked Answered
W

7

32

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!

Wonacott answered 30/10, 2012 at 19:53 Comment(0)
L
98

In your custom ArrayAdapter use isEnabled function to return false:

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

always works for me.

Lakendra answered 30/10, 2012 at 20:3 Comment(6)
How I can re-enable after disable ?Retrusion
I would suggest you to put a flag there, and change that flag whenever you need.Lakendra
It's working. We have add this code in listAdapter class in java.Faris
This didn't work for me. But this answer: https://mcmap.net/q/99143/-android-disabling-highlight-on-listview-click did work for me.Vevay
That's not the same thing @Vevay it is still clickable but not visible that it's clicked.Lakendra
Just to elaborate on yahya, just use the flags. Create a small function in your adapter that takes a Boolean argument. Set a global variable inside your adapter and then use it as your flag. It's a really nice solution. Good job yahya :)Clareclarence
E
17

all the easier! list.setEnabled(false)

Enate answered 8/6, 2014 at 22:58 Comment(1)
Bad! This prevents scrolling.Daggerboard
R
9

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>
Robinet answered 30/10, 2012 at 19:58 Comment(1)
This should be the accepted answer! It's not only answering the question but it suggests a better way to handle an empty list, too!Ciliolate
G
3

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

Gaspar answered 15/5, 2014 at 12:4 Comment(0)
R
3

Try setting these two attributes on the ListView:

android:listSelector="@android:color/transparent" 
android:cacheColorHint="@android:color/transparent"
Runt answered 20/12, 2018 at 13:5 Comment(0)
C
0

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

Cline answered 30/10, 2012 at 19:58 Comment(0)
P
0

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);
    }
Pompon answered 19/9, 2014 at 6:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.