Custom ListView In A Dialog In Android

This example will explain how you can display custom listview in a dialogbox in android.

Algorithm:

1.) Create a new project by File-> New -> Android Project name it CustomListViewDialog.

2.) Write following into main.xml:

 

<?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" >

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Hello" />

<Button
android:id="@+id/opendialog"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Open Dialog" />

</LinearLayout>

 

 

3.) Create and write following into dialoglayout.xml:

 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/customdialog"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="20dp"
android:minWidth="300dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher"/>

<ListView
android:id="@+id/dialoglist"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />

</LinearLayout>

 

 

4.) Run for output.

Steps:

1.) Create a project named CustomListViewDialog and set the information as stated in the image.

Build Target: Android 4.4
Application Name: CustomListViewDialog
Package Name: com.example.CustomListViewDialog
Activity Name: CustomListViewDialogActivity

customlistview1

2.) Open CustomListViewDialogActivity.java file and write following code there:

 

package com.example.customlistviewdialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.content.DialogInterface.OnDismissListener;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

public class CustomListViewDialogActivity extends Activity {

Button buttonOpenDialog;

String KEY_TEXTPSS = "TEXTPSS";
static final int CUSTOM_DIALOG_ID = 0;

ListView dialog_ListView;

String[] listContent = {
"January", "February", "March", "April",
"May", "June", "July", "August", "September",
"October", "November", "December"};

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

buttonOpenDialog = (Button)findViewById(R.id.opendialog);
buttonOpenDialog.setOnClickListener(new Button.OnClickListener(){

@Override
public void onClick(View arg0) {
showDialog(CUSTOM_DIALOG_ID);
}});

}

@Override
protected Dialog onCreateDialog(int id) {

Dialog dialog = null;

switch(id) {
case CUSTOM_DIALOG_ID:
dialog = new Dialog(CustomListViewDialogActivity.this);
dialog.setContentView(R.layout.dialoglayout);
dialog.setTitle("Custom Dialog");

dialog.setCancelable(true);
dialog.setCanceledOnTouchOutside(true);

dialog.setOnCancelListener(new OnCancelListener(){

@Override
public void onCancel(DialogInterface dialog) {
// TODO Auto-generated method stub
Toast.makeText(CustomListViewDialogActivity.this,
"OnCancelListener",
Toast.LENGTH_LONG).show();
}});

dialog.setOnDismissListener(new OnDismissListener(){

@Override
public void onDismiss(DialogInterface dialog) {
// TODO Auto-generated method stub
Toast.makeText(CustomListViewDialogActivity.this,
"OnDismissListener",
Toast.LENGTH_LONG).show();
}});

//Prepare ListView in dialog
dialog_ListView = (ListView)dialog.findViewById(R.id.dialoglist);
ArrayAdapter<String> adapter
= new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, listContent);
dialog_ListView.setAdapter(adapter);
dialog_ListView.setOnItemClickListener(new OnItemClickListener(){

@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
Toast.makeText(CustomListViewDialogActivity.this,
parent.getItemAtPosition(position).toString() + " clicked",
Toast.LENGTH_LONG).show();
dismissDialog(CUSTOM_DIALOG_ID);
}});

break;
}

return dialog;
}

@Override
protected void onPrepareDialog(int id, Dialog dialog, Bundle bundle) {
// TODO Auto-generated method stub
super.onPrepareDialog(id, dialog, bundle);

switch(id) {
case CUSTOM_DIALOG_ID:
//
break;
}
}
}

 

 

3.) Compile and build the project.

Output

customlistview2

customlistview3