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 Responses to Android Tutorial–Customized Radio Button List

  1. [...] list view with radio button Tagged: Androideclipsequestions /* * * CONFIGURATION VARIABLES: EDIT BEFORE PASTING INTO YOUR WEBPAGE * * */ var disqus_shortname = 'dkphp'; // required: replace example with your forum shortname var disqus_identifier = "d2520b0a3f1ba94819e6f19391351eee"; var disqus_url = "http://www.dkphp.com/questions-2/eclipse/how-can-i-put-radiobutton-in-listview.html&quot;; /* * * DON'T EDIT BELOW THIS LINE * * */ (function() { var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true; dsq.src = 'http://&#039; + disqus_shortname + '.disqus.com/embed.js'; (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq); })(); var infolink_pid = 1023613; var infolink_wsid = 1; [...]

  2. ana maria says:

    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. Garima says:

    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.

* Copy This Password *

* Type Or Paste Password Here *

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.