Radio buttons are almost always used to represent single choice from a list of items. Android is no exception to that.

Android provides two ways to use radio buttons. At a higher level, it allows one to use radio button list in ListActivity by setting the choice mode, as indicated in the code below,

public class List10 extends ListActivity {

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_single_choice, CONTENT));

        final ListView listView = getListView();

        listView.setItemsCanFocus(false);

        listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);

    }

    private static final String[] CONTENT = new String[] {

        "Action", "Adventure", "Animation", "Children", "Comedy", "Documentary", "Drama",

        "Foreign", "History", "Independent", "Romance", "Sci-Fi", "Television", "Thriller"

    };

}

The effect is as below,

sc0

At a lower level, one can create radio button list by using RadioGroup and RadioButton. One can find the detailed code in the RadioGroup1.java source file downloadable at the end of this post. The effect of this method is as below,

sc1

However, the lower level default radio button only allows one to place the text on the right of the button, and the higher level method is also difficult to customize. But one can always build a radio button list from scratch.

The method is to place a radio button and a text view in a linear layout to form a single item and then use it as the element to build list view. The effect is as below,sc2

The complete code can be downloaded from here.

 

3 comments on “Android Tutorial–Customized Radio Button List

  1. Pingback: How can i put radiobutton in listview | PHP Developer Resource

  2. ana maria on said:

    Very useful information. Can you tell me please how to make one of the selections a default? The idea is to make the first choice of the list already clicked, just in case the user will not choose anything?

  3. In case I want to add a button at the foot of the radio button list ( referring to the first screen shot ). how do i go about that? Any attempts that I made for the same resulted in the radio button assignment being lost.

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Set your Twitter account name in your settings to use the TwitterBar Section.