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”
2. Select a build target for this application. For this application let us selection Android 2.3.3 ( API level 10 )
3. Enter application related details like package name, main activity class name etc
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
7. Download

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

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
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
How to make the first one selected when the list view is firs initiated?
how can i achieve this single selection in listview using expandable listveiw?