May 28, 2012

Cancel and Dismiss Custom dialog

Last article demonstrate a basic custom dialog, it can be dismissed by buttons. How about cancelled by system BACK key and touching outside dialog? by calling setCancelable(boolean) and setCanceledOnTouchOutside(boolean) we can sets 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.

Cancel and Dismiss Custom dialog


Modify the main activity in the last article "Custom dialog, with data passed in Bundle".
package com.AndroidCustomDialog;

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 AndroidCustomDialogActivity 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(AndroidCustomDialogActivity.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(AndroidCustomDialogActivity.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(AndroidCustomDialogActivity.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(AndroidCustomDialogActivity.this, 
       "OnCancelListener", 
       Toast.LENGTH_LONG).show();
    }});
      
      dialog.setOnDismissListener(new OnDismissListener(){

    @Override
    public void onDismiss(DialogInterface dialog) {
     // TODO Auto-generated method stub
     Toast.makeText(AndroidCustomDialogActivity.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;
     }
  
 }

}


No comments:

Post a Comment

Infolinks In Text Ads