Event Listener for a ListView in Single Selection mode

June 15, 2012

In this article we will create an event listener for listview in single selection mode ( Radio Button ) .

1. Create an Android project namely ListViewRadioListener

New Android Project

Figure 1: New Android Project

2. Select build target for this application

Select build target

Figure 2: Select build target

3. Enter application details

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:orientation="vertical" >


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

package in.wptrafficanalyzer.listviewradiolistener;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends Activity {

    // String array of countries for the listview
    String[] countries = new String[] {
        "Sri Lanka",
        "North Korea",
        "South Korea",

    /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState) {

        // Reference to listview object of the main layout
        ListView listView = (  ListView ) findViewById(R.id.listview);

        // Creating an array adapter for listView
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_single_choice, countries);

        // Defining the item click listener for listView
        OnItemClickListener itemClickListener = new OnItemClickListener() {
            public void onItemClick(AdapterView<?> arg0, View arg1, int position, long id) {
                Toast.makeText(getBaseContext(), "You selected :" + countries[position], Toast.LENGTH_SHORT).show();

        // Setting the item click listener for listView


6. Running the application

Application in execution

Figure 4: Application in execution

7. Download the project source code

8. Reference


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

Android Knowledge Quiz

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


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