Follow treslines by email clicking Here!

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);
        protected void onPostExecute(Void result) {
            if (dialog.isShowing()) {
            //do something here after processing if needed
        protected Void doInBackground(Void... params) {
            // heavy weitht process goes here...
            return null;

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

No comments:

Post a Comment