Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding button to each row in listview

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.

like image 751
hussain Avatar asked Sep 06 '11 10:09

hussain


3 Answers

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

like image 166
marc1s Avatar answered Nov 07 '22 08:11

marc1s


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.

like image 31
Joru Avatar answered Nov 07 '22 06:11

Joru


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.

like image 1
issam Avatar answered Nov 07 '22 07:11

issam