Tuesday, August 5, 2014

How to create an AsyncTask with visual ProgressBar and displaying icon and custom message Android

Hi There!

Today i'm gonna show how to create and run an AsyncTask with a visual ProgressBar with displaying loading icon and custom message. This is a very often task while developing apps for Android.

It is used everytime we have to run a task in background. This is specially useful, if the task takes more then 10 seconds to prevent ARN (Application Not Responding) errors.

The example bellow is very streight forward and can be used whenever you may need it.

Anonymous AsyncTask

Let's say we have an alert dialog or a button click in which you fire an action which performs a long process computation or web request. Whatever... let's say it is an AlertDialog.
    final String yourCustomMessage;
    final Context cxt = YourAppContext;
    new AsyncTask < Void,  Void, Void > ( ) {
        private ProgressDialog dialog;
        protected void onPreExecute() {
            dialog = new ProgressDialog(cxt);
            String loading = cxt.getString(R.string.alert_buy_languages_loading);
            dialog.setMessage(loading + yourCustomMessage);
            dialog.show();
        }
        @Override
        protected void onPostExecute(Void result) {
            if (dialog.isShowing()) {
                dialog.dismiss();
            }
            //do something here after processing if needed
        }
        @Override
        protected Void doInBackground(Void... params) {
            // heavy weitht process goes here...
            return null;
        }
    }.execute();

That's all! Hope you like it! This AsyncTask has been used in the available app bellow:

Syntaxionary - Programming Language Dictionary


Syntaxionary - Programming Language Dictionary


😱👇 PROMOTIONAL DISCOUNT: BOOKS AND IPODS PRO ðŸ˜±ðŸ‘‡

Be sure to read, it will change your life!
Show your work by Austin Kleonhttps://amzn.to/34NVmwx

This book is a must read - it will put you in another level! (Expert)
Agile Software Development, Principles, Patterns, and Practiceshttps://amzn.to/30WQSm2

Write cleaner code and stand out!
Clean Code - A Handbook of Agile Software Craftsmanship: https://amzn.to/33RvaSv

This book is very practical, straightforward and to the point! Worth every penny!
Kotlin for Android App Development (Developer's Library): https://amzn.to/33VZ6gp

Needless to say, these are top right?
Apple AirPods Pro: https://amzn.to/2GOICxy

😱👆 PROMOTIONAL DISCOUNT: BOOKS AND IPODS PRO ðŸ˜±ðŸ‘†