Tuesday, February 4, 2014

A CustomConfirmDialog for android using AlertDialog and Abstract Class

Some of these days while i was working on some android project which uses a hell lot of confirmations, i got tired of using the actual implementation of AlertDialog and decided to write an abstract class to simplify the AlertDialog for confirmation dialog usage, Please do share your thoughts on this concept, Here is what i came up with : 

Usage : 

//Parms : Context,title,prompt text,yes button caption,no button caption
new CustomConfirmDialog(this,"title","prompt","yes","no") {
      
      @Override
      public void onPositiveResult(DialogInterface dialog) {
       // TODO Auto-generated method stub
      
       
       //to Update UI Componenets
       runOnUiThread(new Runnable() {
     
     @Override
     public void run() {
    // TODO Auto-generated method stub
      
     }
    });
       
       
       //for Normal Operations
       dialog.dismiss();
       
      }
      
      @Override
      public void onNegativeResult(DialogInterface dialog) {
       // TODO Auto-generated method stub
       dialog.dismiss();
      }
     };






Class File : CustomConfirmDialog

/* 
http://www.apkman.blogspot.com
*/

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;

public abstract class CustomConfirmDialog {



public CustomConfirmDialog(Context context,String title,String prompt,String positiveButton,String negativeButton)
{
  AlertDialog.Builder builderInner = new AlertDialog.Builder(context);
     builderInner.setMessage(prompt);
     builderInner.setTitle(title);
     builderInner.setPositiveButton(positiveButton,
             new DialogInterface.OnClickListener() {

      
      
                 @Override
                 public void onClick(
                         DialogInterface dialog,
                         int which) {

               onPositiveResult(dialog);
                 }
                 
                 
                 
                 
             }).setNegativeButton(negativeButton, new OnClickListener() {
     
     @Override
     public void onClick(DialogInterface dialog, int which) {
      // TODO Auto-generated method stub
           
      onNegativeResult(dialog);
     }
    });
     
     
   
     builderInner.show();
 

}





public abstract void onPositiveResult(DialogInterface dialog);
public abstract void onNegativeResult(DialogInterface dialog);
 
}

No comments:

Post a Comment

Followers