Dismiss Custom Dialog In Android

This example will explain how a basic custom dialog, it can be dismissed by buttons. We will show you how you can cancel by system BACK key and touching outside dialog. By calling setCancelable(boolean) and setCanceledOnTouchOutside(boolean) we can set whether this dialog is cancelable with the BACK key, and whether this dialog is canceled when touched outside the window’s bounds. We also implement OnCancelListener and OnDismissListener to handle the cancel and dismiss events.

Algorithm:

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

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 DismissCustomDialog and set the information as stated in the image.

Build Target: Android 4.4
Application Name: DismissCustomDialog
Package Name: com.example.DismissCustomDialog
Activity Name: DismissCustomDialogActivity

dismisscustomdialog1

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

 

package com.example.dismisscustomdialog;

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.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class DismissCustomDialogActivity 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(DismissCustomDialogActivity.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(DismissCustomDialogActivity.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(DismissCustomDialogActivity.this,
"Dismiss by Cancel button",
Toast.LENGTH_LONG).show();
dismissDialog(CUSTOM_DIALOG_ID);
}});

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

dialog.setOnCancelListener(new OnCancelListener(){

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

dialog.setOnDismissListener(new OnDismissListener(){

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

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

dismisscustomdialog2

dismisscustomdialog3

dismisscustomdialog4