android – List item with button not clickable anymore-ThrowExceptions

Exception or error:

I have a list item with a button inside.

When the button is shown, the list item is not clickable anymore.
To make it clickable again, I have replaced the button with a view. The problem is that, when the list item is clicked, the button changes background image (like if it is clicked). How can avoid this bad behaviour?

Thanks

How to solve:

Set the following properties for you button

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

For ImageButton, also add the following to the parent view

  android:descendantFocusability="blocksDescendants" 

###

Actually I have just found a wonderful explaination: http://android.cyrilmottier.com/?p=525

The problem and the solution is very well explained there.

###

The part of the link provided by @Matroska that answers the question:
You must add

android:descendantFocusability="blocksDescendants"

to the parent ViewGroup that defines the layout of an item of your ListView.

Note: this will no longer allow you to focus on the inner button with hardware buttons.
(sorry, I cannot comment yet)

###

You can try this:

             yourButton.setFocusable(false);
             yourButton.setFocusableInTouchMode(false);

###

Here is an example of a clickable button inside a ListView. If you want to download the project you can download the IntelliJ Gradle project from my web site:
http://developersfound.com/ListButtonClickExample.zip

The custom adapter in this example has the click listener instead of the listener being inside the Fragment or Activity. It’s done is such a way that there is only on listener object and all button are bound to them for efficiency.

Here is the ListItem layout:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"
    tools:context="com.jc_systems.listbuttonclickexample.app.ItemFragment">

    <LinearLayout
        android:id="@+id/test_container"
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">

        <ImageView
            android:id="@+id/image_list_image"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/exaple_icon"
            android:layout_weight=".1"
            android:layout_gravity="left|top"/>

        <TextView
            android:id="@+id/lbl_list_item"
            android:layout_width="168dp"
            android:layout_height="wrap_content"
            android:text="I think this should take up two lines..."
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:gravity="center_vertical|center_horizontal"
            android:layout_gravity="center_vertical"/>

        <Button
            android:id="@+id/cmd_list_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello"
            android:layout_weight="0.2"
            android:paddingLeft="0dp"
            android:paddingRight="0dp"
            android:paddingTop="0dp"
            android:paddingBottom="0dp"
            android:layout_gravity="right|top"/>

    </LinearLayout>

</FrameLayout>

And here is the CustomAdapter:

import android.app.Activity;
import android.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;

public class MyCustomAdapter extends ArrayAdapter {

    private final ArrayList<FragmentItems> list;
    private static Activity context;
    private View.OnClickListener adaptrDynaListener = null;

    public MyCustomAdapter(Activity context, ArrayList<FragmentItems> list) {

        super(context, R.layout.fragment_items, list);
        this.context = context;
        this.list = list;

        adaptrDynaListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String buttonText = ((Button) v).getText().toString();
                new AlertDialog.Builder(MyCustomAdapter.context).setTitle("Alert").setMessage(buttonText).setNeutralButton("OK", null).show();

            }

        };

    }

    static class ViewHolder {

        protected ImageView image_list_image;
        protected TextView lbl_list_item;
        protected Button cmd_list_button;

    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = null;
        ViewHolder viewHolder = new ViewHolder();

        if (convertView == null) {

            LayoutInflater inflator = context.getLayoutInflater();
            view = inflator.inflate(R.layout.fragment_items, null);
            viewHolder.image_list_image = (ImageView) view.findViewById(R.id.image_list_image);
            viewHolder.lbl_list_item = (TextView) view.findViewById(R.id.lbl_list_item);
            viewHolder.cmd_list_button = (Button) view.findViewById(R.id.cmd_list_button);
            viewHolder.cmd_list_button.setTag(viewHolder);
            view.setTag(viewHolder);

        }
        else {

            view = convertView;
            viewHolder = (ViewHolder) view.getTag();

        }

        ViewHolder holder = (ViewHolder) view.getTag();

        holder.lbl_list_item.setText(list.get(position).getMessage());
        holder.cmd_list_button.setText(list.get(position).getButtonText());
        holder.cmd_list_button.setOnClickListener(adaptrDynaListener);

        return view;

    }//public View getView(int position, View convertView, ViewGroup parent)

    public int getCount() {

        if(list.size() <= 0) {

            return 1;

        }

        return list.size();

    }

    public Object getItem(int position) {

        return position;

    }

    public long getItemId(int position) {

        return position;

    }

}

I’ve tested this Adapter pattern quite extensively and it seems very stable in ListView, ListActivities and ListFragments.

###

You can create an xml file that contains the clicked behavior of the view. Create an xml file, custom_button.xml (or whatever you want to call it) and fill it with this code:

<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <!-- Focused -->
    <item android:state_focused="true" android:state_pressed="false" android:color="@color/black"/>
    <!-- Pressed -->
    <item android:state_focused="false" android:state_pressed="true" android:color="@color/black"/>
    <!-- Focused+Pressed -->
    <item android:state_focused="true" android:state_pressed="true" android:color="@color/black"/>
    <!-- Disabled -->
    <item android:state_enabled="false" android:color="@color/dark_grey_text"/>
    <!-- Default -->
    <item android:color="@color/white"/>

</selector>

You can then change the

android:color=""

To

android:drawable=""

And assign them to any drawable resources you have in your drawable folder. Then in the xml file for your layout containing the view, add:

android:background="custom_button"

Leave a Reply

Your email address will not be published. Required fields are marked *