f470071
f470071

Reputation: 1567

How to make ListView not respond (change background) on touch?

I have a ListView which displays some inert items, so this should not change their backgrounds when a user touches the list view or item. This turns out hard to do: What i have tried so far: Java:

listView.setLongClickable(false);
listView.setClickable(false);
listView.setFocusable(false);

This has no effect what so ever. If I touch listview the background changes.

In the xml I tried;

<ListView
    android:id="@android:id/list"
    android:choiceMode="none"
    android:clickable="true"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:listSelector="@null" />

Setting listSelector to @null is something I saw elsewehere on stack overflow. Does not work.

I also tried setting each item's background in code to transparent.

convertView.setBackgroundResource(R.color.transparent);

Nothing. On every touch there is always a change in background to blue. How to make my listview not change item background on touch?

Upvotes: 0

Views: 360

Answers (4)

Ineptus
Ineptus

Reputation: 171

You can set view.setEnabled(false); in your Adapter getView() - it will remove background effects.

Upvotes: 2

Divyang Panchal
Divyang Panchal

Reputation: 1909

Try This, It works For Me

<ListView 
android:listSelector="@android:color/transparent" 
android:cacheColorHint="@android:color/transparent"
/>

Upvotes: 4

Karthika PB
Karthika PB

Reputation: 1373

for this you need to make a custom background for the listview like

 <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:state_enabled="false"
        android:drawable="@android:color/white" />
    <item
        android:state_pressed="true"
        android:state_enabled="true"
        android:drawable="@android:color/white"  />
    <item
        android:state_focused="true"
        android:state_enabled="true"
       android:drawable="@android:color/white"  />
    <item
        android:state_enabled="true"
       android:drawable="@android:color/white"  />
    </selector>

place this xml file with name listbg.xml inside your drawable folder

<ListView
    android:id="@+id/listt"
    android:choiceMode="none"
    android:clickable="true"
    android:background="@drawable/listbg"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:listSelector="@null" />

Upvotes: 0

Evripidis Drakos
Evripidis Drakos

Reputation: 870

You are probably using a layout for your list items that has a background which has a different selected state.

See what layout you use for the list items and change that to not include that background. If you are using the default (android.R.layout.simple_list_item) layout, you can create and use your own layout.

Upvotes: 0

Related Questions