Skip to content

Instantly share code, notes, and snippets.

@jaisonfdo
Last active October 5, 2019 06:45
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jaisonfdo/453cf8a499f4cfd6cb3c9204e31aab8f to your computer and use it in GitHub Desktop.
Save jaisonfdo/453cf8a499f4cfd6cb3c9204e31aab8f to your computer and use it in GitHub Desktop.
It is the utility class contains set of utility methods to show dialogs and returns their clicks using callback methods.For more information, check out my detailed guide here : http://droidmentor.com/show_alertdialog_android/
import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Looper;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
/**
* Created by Jaison on 05/10/16.
*/
public class AlertDialogHelper
{
Context context;
AlertDialog alertDialog=null;
AlertDialogListener callBack;
Activity current_activity;
public AlertDialogHelper(Context context)
{
this.context = context;
this.current_activity = (Activity) context;
callBack = (AlertDialogListener) context;
}
/**
* Displays the AlertDialog with 3 Action buttons
*
* you can set cancelable property
*
* @param title
* @param message
* @param positive
* @param negative
* @param neutral
* @param from
* @param isCancelable
*/
public void showAlertDialog(String title,String message,String positive,String negative,String neutral,final int from,boolean isCancelable)
{
final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(current_activity);
if(!TextUtils.isEmpty(title))
alertDialogBuilder.setTitle(title);
if(!TextUtils.isEmpty(message))
alertDialogBuilder.setMessage(message);
if(!TextUtils.isEmpty(positive)) {
alertDialogBuilder.setPositiveButton(positive,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
callBack.onPositiveClick(from);
alertDialog.dismiss();
}
});
}
if(!TextUtils.isEmpty(neutral)) {
alertDialogBuilder.setNeutralButton(neutral,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
callBack.onNeutralClick(from);
alertDialog.dismiss();
}
});
}
if(!TextUtils.isEmpty(negative))
{
alertDialogBuilder.setNegativeButton(negative,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
callBack.onNegativeClick(from);
alertDialog.dismiss();
}
});
}
alertDialogBuilder.setCancelable(isCancelable);
alertDialog = alertDialogBuilder.create();
alertDialog.show();
if(TextUtils.isEmpty(negative))
alertDialog.getButton(AlertDialog.BUTTON_NEGATIVE).setEnabled(false);
}
/**
* Displays the AlertDialog with positive action button only
*
* you can set cancelable property
*
* @param title
* @param message
* @param positive
* @param from
* @param isCancelable
*/
public void showAlertDialog(String title,String message,String positive,final int from,boolean isCancelable)
{
showAlertDialog(title,message,positive,"","",from,isCancelable);
}
/**
* Displays the AlertDialog with positive action button only
*
* cancelable property is false (Default)
*
* @param title
* @param message
* @param positive
* @param from
*/
public void showAlertDialog(String title,String message,String positive,final int from)
{
showAlertDialog(title,message,positive,"","",from,false);
}
/**
*
* Displays the AlertDialog with positive & negative buttons
*
* you can set cancelable property
*
* @param title
* @param message
* @param positive
* @param negative
* @param from
* @param isCancelable
*/
public void showAlertDialog(String title,String message,String positive,String negative,final int from,boolean isCancelable)
{
showAlertDialog(title,message,positive,negative,"",from,isCancelable);
}
/**
*
* Displays the AlertDialog with positive & negative buttons
*
* cancelable property is false (Default)
*
* @param title
* @param message
* @param positive
* @param negative
* @param from
*/
public void showAlertDialog(String title,String message,String positive,String negative,final int from)
{
showAlertDialog(title,message,positive,negative,"",from,false);
}
/**
* Displays the AlertDialog with 3 Action buttons
*
* cancelable property is false (Default)
*
* @param title
* @param message
* @param positive
* @param negative
* @param neutral
* @param from
*/
public void showAlertDialog(String title,String message,String positive,String negative,String neutral,final int from)
{
showAlertDialog(title,message,positive,negative,neutral,from,false);
}
public interface AlertDialogListener
{
public void onPositiveClick(int from);
public void onNegativeClick(int from);
public void onNeutralClick(int from);
}
}
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;
/**
* Created by Jaison on 04/10/16.
*/
public class AlertDialogSampleActivity extends AppCompatActivity implements AlertDialogHelper.AlertDialogListener
{
AlertDialogHelper alertDialogHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_alertdialog);
try
{
// instance of the helper class
alertDialogHelper =new AlertDialogHelper(this);
// show AlertDialog
alertDialogHelper.showAlertDialog("Draft","Discard draft ?","Discard","Cancel",1,true);
/* // 3 Action Buttons
alertDialogHelper.showAlertDialog("Draft","Discard draft ?","Discard","Later","Cancel",1,false);
alertDialogHelper.showAlertDialog("Draft","Discard draft ?","Discard","Later","Cancel",2);
// 2 Action Buttons
alertDialogHelper.showAlertDialog("Draft","Discard draft ?","Discard","Cancel",1,true);
alertDialogHelper.showAlertDialog("Draft","Discard draft ?","Discard","Cancel",2);
// 1 Action Button
alertDialogHelper.showAlertDialog("Draft","Discard draft ?","Discard",1,true);
alertDialogHelper.showAlertDialog("Draft","Discard draft ?","Discard",2);
// Without Title (Empty String for Title)
alertDialogHelper.showAlertDialog("","Discard draft ?","Discard",1,true);
// Without Message (Empty String for Message)
alertDialogHelper.showAlertDialog("Draft","","Discard","Cancel",1,true);*/
}
catch(Exception e)
{
}
}
@Override
public void onPositiveClick(int from) {
Toast.makeText(getApplicationContext(), "Positive Click from :"+from, Toast.LENGTH_LONG).show();
}
@Override
public void onNegativeClick(int from) {
Toast.makeText(getApplicationContext(), "Negative Click from :"+from, Toast.LENGTH_LONG).show();
}
@Override
public void onNeutralClick(int from) {
Toast.makeText(getApplicationContext(), "Neutral Click from :"+from, Toast.LENGTH_LONG).show();
}
}
@aqibshxhzd
Copy link

may be some improvement by implementing custom style

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment