How set ListView not clickable
Asked Answered
L

14

37

I have this ListView that just needs to show data.
So I don't want to make it clickable.
First I've tried changing XML listview to:

<ListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:clickable="false" >

But it didn't work.
So to solve my problem I set through code:

  list.setSelector(android.R.color.transparent); 

but I can't believe there's no better solution. Any idea?

Lustihood answered 30/7, 2013 at 11:14 Comment(4)
One way: ListView.setOnClickListener(null); OR add android:focusable="false" android:focusableInTouchMode="false" OR add in the layout android:listSelector="@android:color/transparent"Refuge
I think the clickable attribute only set this property for the view. If you want to set the items not clickable you should their clickable attribute to false. Hope this helpsClio
@Refuge just reply with your comment so I can set as Accepted AnswerLustihood
possible duplicate of Android: disabling highlight on listView clickTiemannite
R
58

Here it is, following the comment:

One way to do so is: ListView.setOnClickListener(null); OR

You can add android:focusable="false" android:focusableInTouchMode="false" OR

another way in the layout android:listSelector="@android:color/transparent"

Cheers.

Refuge answered 7/8, 2013 at 15:17 Comment(1)
list.setOnClickListener(null) gave me an error everytime, but list.setOnItemClickListener(null); worked perfectly.Landmeier
R
55

Just override isEnabled(position) in your adapter and return false

@Override
public boolean isEnabled(int position) {
    return false;
}
Reichard answered 30/7, 2013 at 11:19 Comment(3)
This method prevented the separator lines from showing in between my list items. Instead, I set the following property in the XML of the ListView android:listSelector="@android:color/transparent" and it worked for me.Puppy
how to do it with BaseAdapter @ArunChivalry
Maybe you should override areAllItemsEnabled() and return false too. Otherwise, the isEnabled method may not have effect.Ji
C
12

override the below method. Return false to say all the items are not clickable.

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

And override below method to return which item is not clickable

public boolean isEnabled(int position) {
    if(position == your_item_pos) {
          return false;
    }
    return true;
}
Coincident answered 30/7, 2013 at 11:23 Comment(4)
Where should I put this method?Lustihood
in your adapter classCoincident
its @Override public boolean areAllItemsEnabled() { return false; }Moresque
This method prevented the separator lines from showing in between my list items. Instead, I set the following property in the XML of the ListView android:listSelector="@android:color/transparent" and it worked for me.Puppy
R
4
android:listSelector="@android:color/transparent"

it changes the on-clicked color to same as when it is not clicked! so it appears as if it is plain text...it's a work around.

Rici answered 8/3, 2014 at 18:46 Comment(1)
or listView1.setSelector(android.R.color.transparent);Phosphatize
G
2

Just copy this method in your adapter class. Nothing to do .........

@Override
public boolean isEnabled(int position) {

    return false;
}

You may set any condition to make some selected item unclickable.

@Override
public boolean isEnabled(int position) {
    //Y for non clickable item
      if(data_list.get(position).equalsIgnoreCase("Y"))
        {
            return false;
        }
    return true;
}
Gratia answered 11/7, 2014 at 6:10 Comment(0)
G
1
android:focusable="true"

to any of the item inside listview now listview wont clickable

Grishilda answered 30/7, 2013 at 11:17 Comment(0)
P
1

You could use

yourListView.setOnItemClickListener(null);
Polyphonic answered 14/10, 2014 at 23:50 Comment(0)
W
1

Just set android:enabled="false" to the listview and it would do the trick..why to unneccesary keep overriding methods..

Winfordwinfred answered 6/4, 2015 at 7:55 Comment(2)
Perfect answer!Buitenzorg
This also disables the scrollingCensorious
H
1

Easy way:

listView.setSelector(android.R.color.transparent);
Hydric answered 4/6, 2015 at 17:31 Comment(0)
V
0

Try this code

In Adapter.getView()

i.setOnClickListener( null );
i.setLongClickable( false );
i.setClickable( false );
Vizor answered 30/7, 2013 at 11:16 Comment(2)
I'd like to set everything by XML, I can't believe I need to manage it by codeLustihood
@AntonioCalì the common android beginner's pitfall.Catechize
D
0

In fact, @override isEnabled() and setOnClickListener(null) setClickable(false) setLongClickable(false) There are not working for me. I solve it by override performItemClick

@Override
public boolean performItemClick(View view, int position, long id) {
 if(!clickAble){
     return false;
 }
    return super.performItemClick(view,position,id);
}
Diluent answered 25/4, 2015 at 2:33 Comment(0)
P
0

This is because your ListView item may have a focusable or clickable element, please try the following approach either in XML Layout file or in code:

  1. Add this attribute to the root ViewGroup element of your ListItem XML file (i.e. LinearLayout, RelativeLayout, ...) :

    android:descendantFocusability="blocksDescendants"

  2. Add this line to your List Adapter method which is responsible for creating/recycling the list item (most probably getView() or newView()), while parent is the ViewGroup of list item:

    parent.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);

Proparoxytone answered 21/1, 2019 at 9:41 Comment(0)
P
0

Only using list.requestFocusFromTouch() in the onResume() method of the containing fragment worked for me.

¯\(ツ)

Pumpernickel answered 5/10, 2022 at 16:22 Comment(0)
S
-1

I have this ListView that just needs to show data. So I don't want to make it clickable.

I have done this in one of my projects, and the sample bellow works fine.

Basically, you just need:

  • a data source (ArrayList, for example)
  • a ListView Widget
  • an ArrayAdapter, in order to bind the data source with the ListView


MainActivity.java

package com.sample.listview;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import android.widget.ArrayAdapter;
import android.widget.ListView;

    public class SecondActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);

         // data source
         String[] arrayData = {"Debian", "Ubuntu", "Kali", "Mint"};

         // ListView Widget from a Layout
         ListView listView = (ListView) findViewById(R.id.mainListView);

         // an ArrayAdapter (using a layout as model for displaying the array items)
         ArrayAdapter aa = new ArrayAdapter<String>(this, R.layout.main_list_item_layout, arrayData);

         // binding the ArrayAdapter with the ListView Widget
         listView.setAdapter(aa);
    }
}


activity_main.xml
--->Make sure that the ListView Widget is using wrap_content for layout_width and layout_height

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.sample.listview.MainActivity">


    <ListView
        android:id="@+id/mainListView"
        android:layout_width="wrap_content"  <---- HERE
        android:layout_height="wrap_content" <---- HERE
        />

</LinearLayout>


main_list_item_layout.xml

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mainListItem"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:textColor="#ff0000"
    android:textSize="20sp"
    >

</TextView>



That's it: a ListView not clickable, just presenting data on the screen.

Samekh answered 30/6, 2016 at 16:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.