You are here
Home > HelloWorld >

Display selected item of ListView using toast in android

ListView

In Android, List of scroll-able items can be displayed using ListView. Users can select any list item by clicking on it.

Problem Statement:

Develop an application to create list of fruits using ListView and display selected fruit of ListView using toast. How to get the selected item from ListView?

Several Ways to create ListView:

There 2 ways to create ListView:

  • we can assign ListView by giving values in main.xml.
  • we can assign values in string.xml.

Sample Code:

Here is sample code for ListView using toast in android, Created two files to develop an application.

  • Layout file(activity_main.xml):This file contains code to design an GUI of an application. In this we have used widget ListView to display list and TextView to display text on lists.
  • Activity file(MainActivity.java): This file contains code to perform activity on listview.In this we have created Array, ArrayList, Adapter and ListView. We have added created Array into ArrayList then ArrayList into Adapter and then Adapter into ListView.

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
tools:context=".MainActivity">
<ListView
android:id="@+id/listViewID"
android:layout_width="match_parent"
android:layout_height="495dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>

MainActivity.java

package com.toast.listviewe;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.view.ViewGroup;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity{
ListView fruitView;
String[] fruits = new String[]
{
"Mango",
"Orange",
"Guava",
"Banana",
"Pineapple",
"Apple"
};
List<String> fruitList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fruitView = (ListView) findViewById(R.id.listViewID);
fruitList = new ArrayList<String>(Arrays.asList(fruits));
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_2, android.R.id.text1, fruitList) {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
TextView ListItemShow = (TextView) view.findViewById(android.R.id.text1);
ListItemShow.setTextColor(Color.parseColor("#ffffff"));
return view;
}
};
fruitView.setAdapter(adapter);
fruitView.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(MainActivity.this, "clicked item is " +fruits[position], Toast.LENGTH_SHORT).show();
}
});
}
}

Output:

2 thoughts on “Display selected item of ListView using toast in android

Leave a Reply

Top
%d bloggers like this: