Custom Dialog With Data Passing Using Bundle

This example will explain how a custom dialog is create in onCreateDialog(). The text in the EditText will be passed to the custom dialog via a bundle in onPrepareDialog(int, Dialog, Bundle).

If you use showDialog(int), the activity will call onCreateDialog() method the first time, and hang onto it thereafter. Any dialog that is created by this method will automatically be saved and restored for you, including whether it is showing. If you would like an opportunity to prepare your dialog before it is shown, override onPrepareDialog(int, Dialog, Bundle). We will pass the text from main activity to our custom dialog, so we have to implement onPrepareDialog(int, Dialog, Bundle). And the text will be passed to dialog in bundle.

Algorithm:

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

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" />
<EditText
android:id="@+id/textpass"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<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">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher"/>
<TextView
android:id="@+id/dialogtext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/dialog_ok"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="OK" />
<Button
android:id="@+id/dialog_cancel"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="cancel" />

</LinearLayout>

 

4.) Run for output.

Steps:

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

Build Target: Android 4.4
Application Name: CustomDialogWithBundle
Package Name: com.example.CustomDialogWithBundle
Activity Name: CustomDialogWithBundleActivity

CustomDialogWithBundle1

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

 

package com.example.customdialogwithbundle;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class CustomDialogWithBundleActivity extends Activity {

EditText editTextPass;
Button buttonOpenDialog;

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

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

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

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Bundle bundle = new Bundle();
bundle.putString(KEY_TEXTPSS, editTextPass.getText().toString());
showDialog(CUSTOM_DIALOG_ID, bundle);
}});

}

@Override
protected Dialog onCreateDialog(int id) {

Dialog dialog = null;

switch(id) {
case CUSTOM_DIALOG_ID:
dialog = new Dialog(CustomDialogWithBundleActivity.this);
dialog.setContentView(R.layout.dialoglayout);
dialog.setTitle("Custom Dialog");
dialog_TextView = (TextView)dialog.findViewById(R.id.dialogtext);

Button dialog_OK = (Button)dialog.findViewById(R.id.dialog_ok);
dialog_OK.setOnClickListener(new OnClickListener(){

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(CustomDialogWithBundleActivity.this,
"Dismiss by OK button",
Toast.LENGTH_LONG).show();
dismissDialog(CUSTOM_DIALOG_ID);
}});

Button dialog_Cancel = (Button)dialog.findViewById(R.id.dialog_cancel);
dialog_Cancel.setOnClickListener(new OnClickListener(){

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(CustomDialogWithBundleActivity.this,
"Dismiss by Cancel button",
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:
dialog_TextView.setText("Text passed to Dialog: " + bundle.getString(KEY_TEXTPSS));
break;
}
}
}

 

 

3.) Compile and build the project.

Output

CustomDialogWithBundle2

CustomDialogWithBundle3