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 disable clicking on ListView in android?
In your custom ArrayAdapter use isEnabled function to return false:
@Override
public boolean isEnabled(int position) {
return false;
}
always works for me.
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
all the easier! list.setEnabled(false)
Bad! This prevents scrolling. –
Daggerboard
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>
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
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
Try setting these two attributes on the ListView:
android:listSelector="@android:color/transparent"
android:cacheColorHint="@android:color/transparent"
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
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);
}
© 2022 - 2024 — McMap. All rights reserved.