5

I want to add button to each row of my listview. I created an XML file called row.xml in my layout folder and added two textviews and a button in that file. But when a button is added, I am unable to click the item of listview. I'm only able to click the button. Here is row.xml:

<?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"
    >


    <TextView
        android:id="@+id/text11"

        android:layout_alignParentLeft="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
  android:textSize="25sp"
  android:textColor="#000000"
         />
          <TextView
        android:id="@+id/text2"

        android:layout_alignParentLeft="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
  android:textSize="10sp"
    android:textColor="#000000"
         />
          <Button android:text="Button" android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</LinearLayout>

I want to refer to textviews and button in my activity. Please help me and suggest some ideas.

Smi
  • 13,850
  • 9
  • 56
  • 64
hussain
  • 93
  • 2
  • 3
  • 9

3 Answers3

8

I had a similar issue. The simple trick is to add android:focusable="false" to your Button.

marc1s
  • 1,981
  • 1
  • 13
  • 13
  • Thank you. I finally found the solution that worked for me! My particular list view row has two buttons in it, one on the left and one on the right. Those two buttons worked, but when I clicked anywhere else in the row, it didn't call the onItemSelected callback. – hellaandrew May 20 '16 at 17:27
1

you must add focusable="false" and you can

<Button
        android:id="@+id/bt_do"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        **android:focusable="false"** />

in your adapter

public class MyAdapter extends BaseAdapter {
    private Context context;
    private List< Objet > objects;
    private OnClickListener listener;

    public MyAdapter(Context context, List<Objet> objects,
            OnClickListener listener) {
        this.context = context;
        this.objects = objects;
        this.listener = listener;
        // TODO Auto-generated constructor stub
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            LayoutInflater infalInflater = ((Activity) context)
                    .getLayoutInflater();
            convertView = infalInflater.inflate(R.layout.my_line_list, null);
        }
        Button bt_do=(Button)convertView.findViewById(R.id.bt_do);
        bt_do.setOnClickListener(listener);
        return convertView;

    }

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

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return objects.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return objects.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

}

and in your activity create un adapter and implement listener of button.

issam
  • 697
  • 1
  • 9
  • 15
1

You can use a custom adapter (extending an array adapter is fairly simple). In the getView method, set a onClickListener on your TextView, this way both your button and the other parts of the ListItem will respond to touch.

Joru
  • 4,406
  • 1
  • 19
  • 13