How to set listview in alert dialog on android

The dialog is a small window that prompts the user to make a decision or enter additional information.

The dialog does not fill the screen and is normally used for modal events that require users to take any action before they can proceed.

Some times in your application, if you wanted to ask a user about making a decision between yes or no in the response to any particular action taken by a user,

by remaining in the common activity and without changing the screen, you can use Alert Dialog.

During an order to make an alert dialog, you need to make an object of AlertDialogBuilder which an inner class of AlertDialog.

Its syntax is given below

AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);

To add an action button to the Alert dialog, you have to use the following methods.

  • setPositiveButton (CharSequence text, DialogInterface.OnClickListener listener): The first argument is the text to be displayed. The second argument is the listener to be invoked when the positive button is pressed.
  • setNegativeButton (CharSequence text, DialogInterface.OnClickListener listener): The arguments are the same as the setPositiveButton method. However, the second argument is applicable when the negative button is pressed.
  • setNeutralButton (CharSequence text, DialogInterface.OnClickListener listener): The arguments are same as the SetPositiveButton method.

In the alert dialog, we can add custom views such as editedtext, listview, button, etc. Here I am telling how to set listview in an alert dialog. Follow below steps to add listview in an alert dialog .

<RelativeLayout xmlns:android=" http://schemas.android.com/apk/res/android"xmlns:tools=" http://schemas.android.com/tools"android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" >package com.example.listviewinandroiddialog;import java.util.ArrayList;import java.util.List;import android.os.Bundle;import android.app.Activity;import android.app.AlertDialog;import android.content.Context;import android.content.DialogInterface;import android.view.LayoutInflater;import android.view.Menu;import android.view.View;import android.view.View.OnClickListener;import android.view.ViewGroup;import android.widget.AdapterView;import android.widget.AdapterView.OnItemClickListener;import android.widget.ArrayAdapter;import android.widget.Button;import android.widget.ListView;import android.widget.TextView;import android.widget.Toast;public class MainActivity extends Activity {Button btnDialog;AlertDialog.Builder alertDialog;ArrayList<String> myList;protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);myList = new ArrayList<String>();myList.add("India");myList.add("China");myList.add("South Africa");myList.add("USA");myList.add("UK");myList.add("Japan ");myList.add("Canada");alertDialog = new AlertDialog.Builder(MainActivity.this);btnDialog = (Button) findViewById(R.id.btnDialog);btnDialog.setOnClickListener(new OnClickListener() {public void onClick(View v) {// TODO Auto-generated method stubAlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.this);LayoutInflater inflater = getLayoutInflater();// create view for add item in dialogView convertView = (View) inflater.inflate(R.layout.listview, null);// on dialog cancel button listneralertDialog.setNegativeButton("Cancel",new DialogInterface.OnClickListener() {public void onClick(DialogInterface dialog,// TODO Auto-generated method stub}});// add custom view in dialogalertDialog.setView(convertView);ListView lv = (ListView) convertView. findViewById(R.id.mylistview);final AlertDialog alert = alertDialog.create();alert.setTitle(" Select country...."); // TitleMyAdapter myadapter = new MyAdapter(MainActivity.this,R.layout.listview_item, myList);lv.setAdapter(myadapter);lv.setOnItemClickListener(new OnItemClickListener() {public void onItemClick(AdapterView<?> arg0, View arg1,int position, long arg3) {// TODO Auto-generated method stubToast.makeText(MainActivity.this,"You have selected -: " + myList.get(position),Toast.LENGTH_SHORT).show();alert.cancel();// show dialogclass MainListHolder {private TextView tvText;private class ViewHolder {TextView tvSname;class MyAdapter extends ArrayAdapter<String> {LayoutInflater inflater;Context myContext;List<String> newList;public MyAdapter(Context context, int resource, List<String> list) {super(context, resource, list);// TODO Auto-generated constructor stubmyContext = context;newList = list;inflater = LayoutInflater.from(context);public View getView(final int position, View view, ViewGroup parent) {final ViewHolder holder;if (view == null) {holder = new ViewHolder();view = inflater.inflate(R.layout.listview_item, null);holder.tvSname = (TextView) view.findViewById(R.id.tvtext_item);view.setTag(holder);holder = (ViewHolder) view.getTag();holder.tvSname.setText(newList.get(position).toString());return view;public boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;

Now run your application

Press on button and you can see dialog with listview.

Read also this Article :- Dropdownlist Using BootStrap in ASP.Net

Originally published at https://www.mindstick.com on March 13, 2017.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Mindstick

MindStick Software Pvt. Ltd. is a Microsoft Gold Partner in software application development. Our expertise is in software product design, custom programming, d