10

I want to make a listview unselectable and unclickable. I'm talking about the color change that occurs when I click on a list item. The code is given below. I'm a newbie in android so please be kind.

from : listitem.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="8px">

<TextView
    android:id="@+id/label"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="#FFFFFF"/>

<TextView
    android:id="@+id/data"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="16px"/>

</LinearLayout>

from : details.java

TestActionAdapter() {
        super(TestDetails.this, R.layout.action_list_item, actions);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        TestAction action = actions.get(position);
        LayoutInflater inflater = getLayoutInflater();
        View view = inflater.inflate(R.layout.action_list_item, parent, false);
        TextView label = (TextView) view.findViewById(R.id.label);
        label.setText(action.getLabel());
        TextView data = (TextView) view.findViewById(R.id.data);
        data.setText(action.getData());
        return view;
    }
Binoy Babu
  • 16,699
  • 17
  • 91
  • 134

3 Answers3

19

Check out the answer here:

How to disable list items...

Basically extend ArrayAdapter, and add these 2 functions:

 @Override
 public boolean areAllItemsEnabled() {
     return false;
 }

 @Override
 public boolean isEnabled(int position) {
     return false;
 }
Community
  • 1
  • 1
William Melani
  • 4,270
  • 1
  • 21
  • 44
10

If all you want is to prevent all rows highlighting on click just use ListView android:listSelector="@android:color/transparent"

Grigori A.
  • 2,628
  • 1
  • 21
  • 19
0

When you return view from getView(..) Method, just put in view.setEnabled(false) before return view .

PH7
  • 3,926
  • 3
  • 24
  • 29