30

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>
Rotemmiz
  • 7,625
  • 3
  • 32
  • 36
Adham
  • 58,520
  • 96
  • 215
  • 339
  • where is your OnClickListener for the ListView? – Sergey Benner Jan 21 '12 at 18:20
  • I would also use a ContextMenu for the list items for the task like that(delete list item) but it's up to you I guess... – Sergey Benner Jan 21 '12 at 18:27
  • What behavior do you see that leads you to believe it's not clickable? Are you sure that the ListView item is set to be clickable? If it isn't, then onClick events won't be generated. In general, if you run into a problem: 1. First do as much debugging as you can. 2. If you expect a behavior and it doesn't happen, be sure your code is reaching the point where you'd expect the behavior to occur. If it doesn't, post the code and the question "why isn't my code reaching point "? 3. If you're getting a crash, post the crash message and stack trace, and then ask "Why is my code aborting here?" M – Joe Malin Jan 21 '12 at 18:23
  • You can try this solution: http://stackoverflow.com/a/10333241/1592183 That's helped me with similar problem. – Yorgi Apr 11 '13 at 00:01
  • Check this answer. It worked to me http://stackoverflow.com/a/11624769/5112161 – fpr0001 Sep 22 '15 at 15:15

11 Answers11

140

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...

Rotemmiz
  • 7,625
  • 3
  • 32
  • 36
27

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.

avalancha
  • 1,148
  • 1
  • 19
  • 38
c0de
  • 982
  • 1
  • 9
  • 10
  • 5
    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"` – Randy Nov 20 '12 at 18:56
  • This fixed it for me. Apparently it wasn't enough to do this in xml. It worked once I did it programmatically. – user990230 Mar 08 '13 at 04:02
  • seems strange you have to set this in both the XML and code, but this solution worked for me. Nice Work! – Hector Sep 13 '13 at 08:13
  • 1
    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 Oct 13 '13 at 22:25
14

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"
Community
  • 1
  • 1
fpr0001
  • 141
  • 1
  • 8
4

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.

Opal
  • 70,085
  • 21
  • 151
  • 167
  • @MarcioGranzotto this answers works only for ArrayAdapter. how to do it with `BaseAdapter` – Prabs Apr 26 '17 at 07:10
3

Consider making the text value selectable by specifying android:textIsSelectable="true"

Don't listen to Google. In the rows' layout, set

textIsSelectable="false"

Thank you Lint (not!)

Henrique de Sousa
  • 5,411
  • 46
  • 50
2

I was using a view and button inside the list view so I have used:

android:focusable="false"

for <button> and <view> ...

After that I used following code for my list view and it worked

// Attach the adapter to a ListView
ListView listView = (ListView) findViewById(R.id.shipping_item_list);
listView.setAdapter(adapter);
listView.setOnItemClickListener(listPairedClickItem);

private AdapterView.OnItemClickListener listPairedClickItem = new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {

    Toast.makeText(getBaseContext(), "Item ", Toast.LENGTH_LONG).show();
    }
};
nhahtdh
  • 52,949
  • 15
  • 113
  • 149
Vinod Joshi
  • 7,094
  • 46
  • 49
2

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

niklas
  • 143
  • 10
1

I'm pretty late but discovered something I think it's interesting about this:

if your adapter descends from ArrayAdapter, as much as I have tried, onItemClickListener is not called.

However, if your adapter descends from BaseAdapter (so you have to implement getCount() and getItem(), trivial for an array) it IS always called.

rupps
  • 9,183
  • 4
  • 53
  • 87
1

List view items are clickable. To use it, you have to set item click listener on your list view. Then it will work.

Leigh
  • 28,424
  • 10
  • 49
  • 96
Mitt
  • 91
  • 1
  • 9
0

For me, the problem was that my items within my ListView had clickable set to true.

Karim Varela
  • 7,304
  • 9
  • 48
  • 75
  • 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. – Johnny Wu Apr 18 '16 at 02:15
-2

set android:clickable="false" android:focusable="fasle"