0

I have been through many posts on this site about listview with buttons and text view not being able to be selected. But my problem is different and I'm not able to derive at a conclusion from other related posts. I have a listview populated by a BaseAdapter. The list view's layout is 2 textviews on the left and a button on the right for each row. I want the button as well as entire row of the listview to be able to selected.

I know the button when used is gaining focus. Can someone tell me how can i make both button as well as entire row - selectable?

Regards, Ajith

Ajith Kamath
  • 51
  • 1
  • 6
  • entire row Selctable ,Means When You its change color as selected? – Samir Mangroliya Mar 01 '12 at 09:14
  • I want both entire row to be selectable as well as the button together. By this i mean when i click someone else other than the button the entire row should get selected and do some activity. And when I click on the button, the button should get pressed and do some activity. – Ajith Kamath Mar 01 '12 at 09:29

3 Answers3

0

I faced the same problem too, and below sample code on how I solved it on Android API level 16. The most important thing is having the android:clickable and android:focusable set to true.

In your row XML file:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/row_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@android:drawable/list_selector_background"
    android:clickable="true"
    android:focusable="true"
    android:gravity="center_vertical"
    android:orientation="horizontal"
    android:padding="5dp" >

    <TextView
        android:id="@+id/label"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button" />

</LinearLayout>

In your base adapter, you can then set the listener to do something:

public class MyBaseAdapter extends BaseAdapter {

    // Some other method implementation here...

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // Initialize the convertView here...

        LinearLayout layout = (LinearLayout) convertView.findViewById(R.id.row_layout);
        Button button = (Button) convertView.findViewById(R.id.button);

        layout.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Toast.makeText(context, "Row clicked!", Toast.LENGTH_LONG).show();
            }
        });
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Toast.makeText(context, "Button clicked!", Toast.LENGTH_LONG).show();
            }
        });
    }

}
VCD
  • 889
  • 10
  • 27
0

yes, you can do it by set focusable to false of button at layout, do this you will get ur issue solved

A List will only click when its doesn't have any element which is focusable. So if you want to click on list and button then make button's focusable to false. By this you can click on both button and entire list

Maneesh
  • 6,098
  • 5
  • 36
  • 55
  • I did do it. But that makes list's row selectable but not the button. I want both entire row to be selectable as well as the button together. By this i mean when i click someone else other than the button the entire row should get selected and do some activity. And when I click on the button, the button should get pressed and do some activity. Am I missing here something? – Ajith Kamath Mar 01 '12 at 09:28
  • Here first we are concentrate on click event of both list and button. Now a List will only click when its doesn't have any element which is focusable. So if you want to click on list and button then make button's focusable to false. By this you can click on both button and entire list – Maneesh Mar 01 '12 at 09:36
  • Thanks a lot. Although this works, from user point of view it seems that click sound will be generated but the feel of pressing the button doesn't come since focusable has been changed to false. Thanks again – Ajith Kamath Mar 01 '12 at 10:19
0

Add android:onClick property to your Buttons in layout XML.

android:onClick:

Name of the method in this View's context to invoke when the view is clicked. This name must correspond to a public method that takes exactly one parameter of type View. For instance, if you specify android:onClick="sayHello", you must declare a public void sayHello(View v) method of your context (typically, your Activity).

Sadeshkumar Periyasamy
  • 4,848
  • 1
  • 26
  • 31