ListView items are not clickable. why?

37,111

Solution 1

try this to get the focus: View.getFocus();

Solution 2

Android doesn't allow to select list items that have focusable elements (buttons). Modify the button's xml attribute to:

android:focusable="false"

It should still be clickable, just won't gain focus...

Solution 3

I had the same issue with a ListView which contained only a RadioButton:

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

<RadioButton
    android:id="@+id/userNameRadioButton"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

</LinearLayout>

I used the RadioButton in each row to display the default item selection, to make ListView handle clicks I had to use:

radioButton.setFocusableInTouchMode(false);
radioButton.setFocusable(false);

or in the XML-file:

android:focusable="false" 
android:focusableInTouchMode="false"

So it is a focus related issue... With the above modifiers the focus is directed to ListView on click.

Solution 4

This answer here worked for me: https://stackoverflow.com/a/16536355/5112161

Mainly he ADDED in the LinearLayout or RelativeLayout the following:

android:descendantFocusability="blocksDescendants"

You also need to REMOVE from all your xml the following:

android:focusable="false"
android:focusable="true"
android:clickable="true"
android:clickable="false"

Solution 5

I wanted to add a comment to rupps answer, but I do not have enough reputation.

If you are using a custom adapter extending the ArrayAdapter you can overwrite with areAllItemsEnabled() and isEnabled(int position) in your class:

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

@Override
public boolean isEnabled(int position) {
    return true;
}

The above fixed my non clickable list view. Maybe this comment also helps others as "android list not clickable" search term is quite high in Google.

Share:
37,111
Adham
Author by

Adham

Mobile Engineer

Updated on July 14, 2022

Comments

  • Adham
    Adham almost 2 years

    I have a ListView that uses a customized adapter, but I can't click on the ListView Item ..

    Activity for list view ..

    package com.adhamenaya.projects;
    
    import java.util.ArrayList;
    
    import android.app.Activity;
    import android.content.Context;
    import android.os.AsyncTask;
    import android.os.Bundle;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.view.ViewGroup;
    import android.widget.BaseAdapter;
    import android.widget.Button;
    import android.widget.Filter;
    import android.widget.Filterable;
    import android.widget.ListView;
    import android.widget.TextView;
    import android.widget.Toast;
    
    import com.adhamenaya.classes.Place;
    
    public class PlacesListActivity extends Activity {
        private ArrayList<Place> places;
        private ArrayList<String> items;
        GridviewAdapter mAdapter;
        private ListView lvPlaces;
        private EfficientAdapter adap;
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.places_list);
            lvPlaces = (ListView) this.findViewById(R.id.lvPlaces);
            new DowanloadPlaces().execute("");
        }
        private void bindList(ArrayList<Place> places) {
            this.places = places;
            // Start creating the list view to show articles
            items = new ArrayList<String>();
            for (int i = 0; i < places.size(); i++) {
                items.add(String.valueOf(places.get(i).mName));
            }
            adap = new EfficientAdapter(this);
            adap.notifyDataSetChanged();
            lvPlaces.setAdapter(adap);
        }
    
        // EfficientAdapter : to make a customized list view item
        public class EfficientAdapter extends BaseAdapter implements Filterable {
    
            // The function of inflater to convert objects from XML layout file (i.e. main.xml) to a programmable 
            LayoutInflater inflater;
            Context context;
    
            public EfficientAdapter(Context context) {
                inflater = LayoutInflater.from(context);
                this.context = context;
            }
    
            public int getCount() {
                // Get the number of items in the list
                return items.size();
            }
    
            public Object getItem(int position) {
                // To return item from a list in the given position 
                return items.get(position);
            }
    
            public long getItemId(int position) {
                // TODO Auto-generated method stub
                return 0;
            }
    
            public View getView(final int position, View convertView,ViewGroup parent) {
                ViewHolder holder;
                if (convertView == null) {
                    convertView = inflater.inflate(R.layout.adaptor_content, null);
    
                    holder = new ViewHolder();// Create an object to hold at components in the list view item
                    holder.textLine = (TextView) convertView.findViewById(R.id.textLine);
                    holder.buttonLine = (Button) convertView.findViewById(R.id.buttonLine);
                    holder.buttonLine.setOnClickListener(new OnClickListener() {
                        private int pos = position;
    
                        public void onClick(View v) {
                            places.remove(pos);
                            bindList(places);// to bind list items
                            Toast.makeText(getApplicationContext(),"Deleted successfuly :)", Toast.LENGTH_LONG).show();
                        }
                    });
                    convertView.setTag(holder);
                } else {
                    holder = (ViewHolder) convertView.getTag();
                }
                // Bind the data efficiently with the holder.
                holder.textLine.setText(String.valueOf(places.get(position).mName));
                return convertView;
            }
    
            public Filter getFilter() {
                // TODO Auto-generated method stub
                return null;
            }
    
        }
    
        // ViewHolder : class that represents a list view items
        static class ViewHolder {
            TextView textLine;
            Button buttonLine;
        }
    
        // DownloadRSSFeedsTask: works in a separate thread
        private class DowanloadPlaces extends AsyncTask<String, Void, ArrayList<Place>> {
    
            @Override
            protected ArrayList<Place> doInBackground(String... params) {
                ArrayList<Place> places = new ArrayList<Place>();
                Place p = new Place();
                for(int i =0;i<25;i++){
                    p.mName = "Al Mathaf Hotel";
                    places.add(p);              
                }
    
                return places;
            }
    
            @Override
            protected void onPostExecute(ArrayList<Place> places) {
                bindList(places);
    
    
            }
    
        }
    
    
    }
    

    places_list.xml layout

    <?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">
        <ListView 
            android:layout_width="match_parent" 
            android:layout_height="wrap_content"
            android:id="@+id/lvPlaces">
    
        </ListView>
    </LinearLayout>
    

    adaptor_content.xml layout

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textLine"
        android:layout_centerVertical="true"
        android:src="@drawable/settings" />
    
    </RelativeLayout>
    
  • Randy
    Randy over 11 years
    Thank you for that explanation! I had a CheckBox in mine, and was wondering why I could click the CheckBox but not the ListView item. For the record, the checkbox (or in your case the radio button) can remain clickable, but not focusable. Also, you can do it in XML with android:focusable="false" android:focusableInTouchMode="false"
  • user990230
    user990230 about 11 years
    This fixed it for me. Apparently it wasn't enough to do this in xml. It worked once I did it programmatically.
  • Zapnologica
    Zapnologica about 11 years
    Can you elaborate on the "set click listener" how would one go about doing so?
  • Henrique de Sousa
    Henrique de Sousa almost 11 years
    Thanks for the +1. The textIsSelectable="false" should be next to trying after setting android:focusable="false". I know I will take Lint's suggestions with a grain of salt next time ^^
  • Luke Vo
    Luke Vo over 10 years
    Thank you, this should be marked as the answer! Solved my problem.
  • Hector
    Hector over 10 years
    seems strange you have to set this in both the XML and code, but this solution worked for me. Nice Work!
  • Joshua Pinter
    Joshua Pinter over 10 years
    FYI: This occurs not only with buttons but with TextViews that have a background set. I used focusable="false" to get around it.
  • Joshua Pinter
    Joshua Pinter over 10 years
    FYI: This occurs not only with buttons but with TextViews that have a background set. I used focusable="false" to get around it.
  • Henrique de Sousa
    Henrique de Sousa over 9 years
    You could try Upvoting too :) Glad to know another problem is solved too.
  • aimiliano
    aimiliano almost 9 years
    Correct i put everything under android:focusable="false" and now all good :D
  • Johnny Wu
    Johnny Wu about 8 years
    Do you mean setting it to "false?" I have a compound view in the Listview and I set clickable to false for each of the button inside of my compound view and it works.
  • Palak Darji
    Palak Darji almost 8 years
    android:descendantFocusability="blocksDescendants" +1 for this
  • Julian Alberto
    Julian Alberto over 7 years
    I think this is no longer available
  • Prabs
    Prabs almost 7 years
    @MarcioGranzotto this answers works only for ArrayAdapter. how to do it with BaseAdapter
  • Derek
    Derek about 6 years
    I am currently having the same problem. But with a editText inside a list item. If i do focusable="false" I can no longer change the value in my editText is there a way to get around this?
  • vida
    vida about 6 years
    android:descendantFocusability="blocksDescendants" already did the trick for me +1
  • Dinith Rukshan Kumara
    Dinith Rukshan Kumara about 6 years
    I also think this should be the best answer. It worked. Thanks!.
  • Taslim Oseni
    Taslim Oseni almost 3 years
    Are you sure this is correct? Doesn't seem to be.