Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I make this listview unselectable?

Tags:

android

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;
    }
like image 569
Binoy Babu Avatar asked Nov 28 '11 01:11

Binoy Babu


People also ask

How to make ListView not selectable?

setEnabled(false) in getView() for such items).

How do you make a ListBox not selectable?

There's an even easier way: set ListBox property IsHitTestVisible="False" . This prevents all the items in the list from receiving mouse events. This has the advantage of stopping the highlighting as you mouse-over as well.

How do I resize a list view?

If you only want to set width and height of listview. you can set it by right clicking on listview in xml and select set width / set height and set it, for ex. 20dp, 50 dp etc..

How to disable ListView?

ListView supports to disable the selection. You can disable it by setting the SelectionMode property to None.


2 Answers

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;
 }
like image 192
William Melani Avatar answered Nov 05 '22 06:11

William Melani


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

like image 44
Grigori A. Avatar answered Nov 05 '22 06:11

Grigori A.