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

10 Best Things for Android Development in 2020

2020 is a crazy year for all of us, for sure. In this article, I tried to remember what happened in 2020 in Android development and came up with these 10 main things. #6 is the important one.                                          Refer -  https://vladsonkin.com/  #1 View Binding View Binding makes our work with views easier, and it brings advantages over the standard  findViewById()  and other alternative solutions. View Binding generates a binding class for each XML layout, and the instance contains references to all the views with the ID. Column 1 ButterKnife Kotlin Synthetics Data Binding findViewById View Binding Fast  *  * Null-safe Compile-time safe  ** View Binding  takes the best parts of the other solutions and avoids their drawbacks.  And it’s more important now since the popular  Kotlin Synthetics  are deprecated in...

Android: New Features and Plugins

Android New Features and Plugins: Hello Geeks! My name is Umesh Jangid , In this blog we will learn about  the new android features in last few versions.    Last Versions Android M : Marshmellow Android N : Nougat Newer Version Android O : Android  Oreo   Features:   1.Picture in Picture (PiP) mode: The split-window mode introduced in Android Nougat is a helpful means for multitasking with most apps, but not all apps.  Picture in picture (PiP) takes this a step further by allowing you to miniaturize a YouTube video feed or a video call on Hangouts or Duo into the corner of your screen so you can carry on with other tasks simultaneously. This is one of those "I didn't know I needed this until I tried it" features and one that makes multitasking far less of a compromise than split-window mode. We're excited to see how this feature develops throughout the life of Oreo and beyond. 2.Faster boot time Speed...

Speed Up Android Development

Hello Coders!  Thanks to being here again! The evolution of software development is so fast. Mobile app development is one of the fast-growing part of this world. Mobile app developers always have to follow up all changes to catch up the world and be updated. Mobile apps are living organisms and should to be improved due to end users needs and platform changes. I mean using time effectively with the word fast. The articles below will help you to shorten the time for the process of coding. 1. Shortcuts You will have fun with coding when you use shortcuts. Try to do everything with the keyboard. Exactly this is the first and the most important one. https://www.jetbrains.com/idea/docs/IntelliJIDEA_ReferenceCard_Mac.pdf 2. Live Templates Add your most used code blocks to Live Templates then get rid of the time you spend every time https://www.bignerdranch.com/blog/android-studio-live-templates/ 3. Genymotion: Use Genymotion instead of Android Emulato...