Single Selection mode for ListView in Android

June 15, 2012
By

In this application, we are creating a listview with single selection mode. That is only one item can be selected at a time. This is achieved by adding a radio button on each item in the listview.


1. Create an Android Project namely “ListViewRadio”

New Android Project

1. New Android Project


2. Select a build target for this application. For this application let us selection Android 2.3.3 ( API level 10  )

Select Build Target

Figure 2: Select Build Target


3.  Enter application related details like package name, main activity class name etc

Application Details

Figure 3 : Application Details

 


4. Open and update the file /res/layout/main.xml with the given below code.


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ListView
        android:id="@+id/listview"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:choiceMode="singleChoice"
    />

</LinearLayout>

5. Open and update the file /src/in/wptrafficanalyzer/listviewradio/MainActivity.java


package in.wptrafficanalyzer.listviewradio;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends Activity {
    String[] countries = new String[] {
        "India",
        "Pakistan",
        "Sri Lanka",
        "China",
        "Bangladesh",
        "Nepal",
        "Afghanistan",
        "North Korea",
        "South Korea",
        "Japan"
    };

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        // Getting object reference to listview of main.xml
        ListView listView = (ListView) findViewById(R.id.listview);

        // Instantiating array adapter to populate the listView
        // The layout android.R.layout.simple_list_item_single_choice creates radio button for each listview item
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_single_choice,countries);

        listView.setAdapter(adapter);

    }
}

6. Run the application in Android Virtual Device, by clicking the menu Run -> Run of the Eclipse IDE

Application in execution

Figure 4: Application in execution


7. Download


8. Reference

http://developer.android.com/guide/index.html


How to hire me?

I am George Mathew, working as software architect and Android app developer at wptrafficanalyzer.in

You can hire me on hourly basis or on project basis for Android applications development.

For hiring me, please mail your requirements to info@wptrafficanalyzer.in.

My other blogs
store4js.blogspot.com


Android Knowledge Quiz

Ready to test your knowledge in Android? Take this quiz :



Tags:

3 Responses to Single Selection mode for ListView in Android

  1. sunil pal on August 27, 2012 at 4:05 pm

    String[] countries = new String[] {
    …..
    …..
    };
    in the dotted area i want it to show email messages in list view how can i do it please tell me

  2. khaled on May 17, 2013 at 12:26 am

    How to make the first one selected when the list view is firs initiated?

  3. divya on February 10, 2016 at 6:57 pm

    how can i achieve this single selection in listview using expandable listveiw?

Leave a Reply

Your email address will not be published. Required fields are marked *

Be friend at g+

Subscribe for Lastest Updates

FBFPowered by ®Google Feedburner