Set/Show listview items inside AlertDialog in android

How to create multiple value items list inside Alert dialog box.

In this tutorial we are simply creating a custom AlertDialog with string array inside it, This will look line a whole listview with its items are inside AlertDialog box. Now after selecting element from AlertDialog we will set that element into TextView for proper conformation that our element is successfully selected. So here is the complete step by step tutorial for AlertDialog Set/Show listview items inside AlertDialog in android.

android-project-download-code-button

How to Set/Show listview items inside AlertDialog in android.

Code for MainActivity.java file.

 package com.showlistviewitemsinsidealertdialog_android_examples.com;

import java.util.Arrays;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {

 Button button;
 TextView textview;
 String[] value = new String[]{
 "Android",
 "Php",
 "Blogger",
 "WordPress"
 };
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 button = (Button)findViewById(R.id.button1);
 textview = (TextView)findViewById(R.id.textView1);
 
 button.setOnClickListener(new View.OnClickListener() {
 @Override
 public void onClick(View v) {
 
 AlertDialog.Builder alertdialogbuilder = new AlertDialog.Builder(MainActivity.this);

 
 alertdialogbuilder.setTitle("Select A Item ");

 alertdialogbuilder.setItems(value, new DialogInterface.OnClickListener() {
 @Override
 public void onClick(DialogInterface dialog, int which) {
 String selectedText = Arrays.asList(value).get(which);
 
 textview.setText(selectedText);
 
 }
 });

 AlertDialog dialog = alertdialogbuilder.create();
 
 dialog.show();
 }
 });
 }
}

Code for activity_main.xml layout file.

 <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="com.showlistviewitemsinsidealertdialog_android_examples.com.MainActivity" >

 <Button
 android:id="@+id/button1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_centerHorizontal="true"
 android:layout_centerVertical="true"
 android:text="Click here to show listview items inside AlertDialog in android" />

 <TextView
 android:id="@+id/textView1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_above="@+id/button1"
 android:layout_centerHorizontal="true"
 android:layout_marginBottom="72dp"
 android:text="Selected Text"
 android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>

Screenshots:

select item

Set/Show listview items inside AlertDialog in android

text selected

Click here to download Set/Show listview items inside AlertDialog in android project with source code.

Leave a Reply

Your email address will not be published. Required fields are marked *