Skip to main content

Customized Alert Dialog box and Handling Clicklistener

Hello Geeks

It's time to learn new little bit new stuff about the android alert dialog box. It is not so new for the profession engineers but it can help a lot of new beginners who want to improve their knowledge and speed up their productivity.

So let's start

Just make a method in your utility class and start using. 
here is an example


//Made: By Umesh Jangid
public static void askForInput(Context context, String title, String message, String positiveButtonText, String negativeButtonText, boolean showOnlyOneButton, final AlertDialogCallback<String> callback) {
    AlertDialog.Builder alert = new AlertDialog.Builder(context);    alert.setTitle(title);    alert.setIcon(R.mipmap.ic_launcher);    alert.setMessage(message);    alert.setPositiveButton(positiveButtonText != null ? positiveButtonText : "Ok", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
            callback.alertDialogCallback("1");        }
    });    if (!showOnlyOneButton) {
        alert.setNegativeButton(negativeButtonText != null ? negativeButtonText : "Cancel", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
                // Canceled.                callback.alertDialogCallback("0");            }
        });    }
    alert.setCancelable(false);    alert.show();}

Now make an interface named 'AlertDialogCallback' of the generic type. 
snippet is below
public interface AlertDialogCallback<T> {
    public void alertDialogCallback(T callback);}

Usage of this method in your Activity  or in Fragment

I used this check to check active internet connection in my module, here is  an example


AppUtility.askForInput(context, "No Internet", Constants.NO_INTERNET_STRING, "Ok", "cancel", true, new AlertDialogCallback<String>() {
    @Override    public void alertDialogCallback(String callback) {
        finish();    }
});



Please share and let me know if you face any problem in the comment section, I will try to reply as soon as possible.
Thanks 
Happy coding!! :)


Comments