Android ListView with Toast displaying the Item selected.
MainLayout File
list_view_page.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:id="@+id/listversions"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</ListView>
</RelativeLayout>
ListView row TextView from different xml file (res/layout/samplerow.xml)
samplerow.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:textSize="15sp" >
</TextView>
Java Code in MainActivity oncreate method
ListViewPage.java
package com.example.listview;
import java.util.ArrayList;
import java.util.Arrays;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class ListViewPage extends Activity {
private ListView samplelistview;
private ArrayAdapter<String> listadapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_view_page);
// Find the Listview resource
samplelistview = (ListView) findViewById(R.id.listversions);
// Create List of android versions
String[] android_versions = { "Cupcake", "Donut", "Eclair", "Froyo",
"Gingerbread", "Honeycomb", "Ice Cream Sandwich", "Jelly Bean",
"KitKat", "Lolipop" };
ArrayList<String> androidlist = new ArrayList<String>();
androidlist.addAll(Arrays.asList(android_versions));
// create array adapter for androidlist
listadapter = new ArrayAdapter<String>(this, R.layout.samplerow,
androidlist);
// set array adapter as ListView's adapter
samplelistview.setAdapter(listadapter);
samplelistview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
String Item_clicked = parent.getItemAtPosition(position)
.toString();
Toast.makeText(getApplicationContext(),
"Version Selected:" + Item_clicked, Toast.LENGTH_SHORT)
.show();
}
});
}
}
Screen Shots:
Run the application.
Click on the item in the list, Toast displays the selected item.
MainLayout File
list_view_page.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:id="@+id/listversions"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</ListView>
</RelativeLayout>
ListView row TextView from different xml file (res/layout/samplerow.xml)
samplerow.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:textSize="15sp" >
</TextView>
Java Code in MainActivity oncreate method
ListViewPage.java
package com.example.listview;
import java.util.ArrayList;
import java.util.Arrays;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class ListViewPage extends Activity {
private ListView samplelistview;
private ArrayAdapter<String> listadapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_view_page);
// Find the Listview resource
samplelistview = (ListView) findViewById(R.id.listversions);
// Create List of android versions
String[] android_versions = { "Cupcake", "Donut", "Eclair", "Froyo",
"Gingerbread", "Honeycomb", "Ice Cream Sandwich", "Jelly Bean",
"KitKat", "Lolipop" };
ArrayList<String> androidlist = new ArrayList<String>();
androidlist.addAll(Arrays.asList(android_versions));
// create array adapter for androidlist
listadapter = new ArrayAdapter<String>(this, R.layout.samplerow,
androidlist);
// set array adapter as ListView's adapter
samplelistview.setAdapter(listadapter);
samplelistview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
String Item_clicked = parent.getItemAtPosition(position)
.toString();
Toast.makeText(getApplicationContext(),
"Version Selected:" + Item_clicked, Toast.LENGTH_SHORT)
.show();
}
});
}
}
Screen Shots:
Run the application.
Click on the item in the list, Toast displays the selected item.
No comments:
Post a Comment