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

Popular posts from this blog

Bye build.gradle

Hello Geeks, It’s been a while since  Gradle  released support for  Kotlin  scripts. We have been writing  Gradle  script in a language called  Groovy  since ages. Let’s be honest, We all know that  Groovy  sucks! Half of the times I have been like this when there’s an issue Why does it needs -  No Auto-Completion No Content Assist No Navigation to Source Don’t know what to write and how to write Finally, We don’t have to learn Groovy for  Gradle  scripts. Now we can write our build scripts in  Kotlin .   Kotlin  script overcomes almost all the cons of  Groovy Added advantages over groovy (Old Pattern) Auto-Completion😎 Content Assist😍 Navigation to Source😱 Kotlin Extensions support😘 Errors at compile time instead of runtime💪 Sometimes it can be a bit messy to rewrite  gradle.build  into  gradle.build.kts  files, especially when all its cache is malfunctioning dur...