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:
😱👇 PROMOTIONAL DISCOUNT: BOOKS AND IPODS PRO 😱👇
Be sure to read, it will change your life!
Show your work by Austin Kleon: https://amzn.to/34NVmwx
This book is a must read - it will put you in another level! (Expert)
Agile Software Development, Principles, Patterns, and Practices: https://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 😱👆