0

I have a ListView :

  <ListView
        android:id="@+id/my_list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

The layout of each row of the ListView is:

<? Xml version="1.0"encoding="utf-8"?> 
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    > 
    <ImageView 
          android:id="@+id/my_img" 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:layout_algnParentLeft="true" 
          android:layout_centerVertical="true"          
          /> 
    <TextView 
         android:id="@+id/my_value" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_toRightOf="@id/my_img"
         android:textColor="@drawable/black" /> 
    <RadioButton 
         android:id="@+id/my_radio" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_toRightOf="@id/my_value" /> 

</ RelativeLayout>

As you see above, there is a RadioButton on each row of the list, how to implement the following two features regards to the RadioButton:

1. Suppose there are 3 items in the list, how to make the radio button on each row perform single selection on the list ? (By default, I can select all of the radio buttons)

2. How to have each item row area clickable for the radio button selection instead of only click on radio button for selection?

Paramone
  • 2,634
  • 4
  • 31
  • 58
Leem.fin
  • 40,781
  • 83
  • 202
  • 354

1 Answers1

0

Get a reference to your listView, set its property as

lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE);

Also remove the radio button from your xml layout. The above method will automatically put a radio button in each of your list view rows. Use :

<? Xml version = "1.0" encoding = "utf-8"?> 
<RelativeLayout 
    xmlns: android = "http://schemas.android.com/apk/res/android" 
    android: layout_width = "fill_parent" 
    android: layout_height = "fill_parent" 
    > 
    <ImageView 
          android: id = "@+id/my_img" 
          android: layout_width = "wrap_content" 
          android: layout_height = "wrap_content" 
          android:layout_algnParentLeft="true" 
          android: layout_centerVertical = "true"          
          /> 
    <TextView 
         android: id = "@+id/my_value" 
         android: layout_width = "wrap_content" 
         android: layout_height = "wrap_content" 
         android:layout_toRightOf="@id/my_img"
         android: textColor = "@drawable/ black" /> 


</ RelativeLayout>
Akhil
  • 13,888
  • 7
  • 35
  • 39
  • I would like when use click on the row area (no matter it is radio button or image icon), the radio button get checked, how to do that? – Leem.fin Mar 26 '12 at 12:14
  • After setting choice mode single, on click of row, the radio button for that row will be checked and others unchecked. – Akhil Mar 26 '12 at 12:52
  • I removed from my row layout, and added the single choice mode to my list, But there is no Radio button. Can you be more clear, where should the radio button be defined?? – Leem.fin Mar 26 '12 at 13:19