Home » Android » java – How to show "Loading" status in Android?

java – How to show "Loading" status in Android?

Posted by: admin June 15, 2020 Leave a comment

Questions:

I’m making an Android application and it has to load some data though Internet (only some data– not all). So when the data is loading, and the Internet connection is slow, I want to show a “Loading…” icon to the user.

So how can I do this? Show a “Loading…” icon while the data is being loaded in the background, and when its completely loaded, hide the icon?

Thanks in advance!

How to&Answers:

use Async Task for your status.

new SomeTask(0).execute();

/** Inner class for implementing progress bar before fetching data **/
private class SomeTask extends AsyncTask<Void, Void, Integer> 
{
    private ProgressDialog Dialog = new ProgressDialog(yourActivityClass.this);

    @Override
    protected void onPreExecute()
    {
        Dialog.setMessage("Doing something...");
        Dialog.show();
    }

    @Override
    protected Integer doInBackground(Void... params) 
    {
        //Task for doing something 

        return 0;
    }

    @Override
    protected void onPostExecute(Integer result)
    {

        if(result==0)
        {
             //do some thing
        }
        // after completed finished the progressbar
        Dialog.dismiss();
    }
}

Answer:

Use AsyncTask along with progress dialog on task completion..That will do..

Answer:

Use asynctask for Background operations, then display progress dialog like below

 private class ProgressTask extends AsyncTask<String, Void, Boolean> {
    private ProgressDialog dialog;
    List<Message> titles;
    private ListActivity activity;
    //private List<Message> messages;
    public ProgressTask(ListActivity activity) {
        this.activity = activity;
        context = activity;
        dialog = new ProgressDialog(context);
    }



    /** progress dialog to show user that the backup is processing. */

    /** application context. */
    private Context context;

    protected void onPreExecute() {
        this.dialog.setMessage("Progress start");
        this.dialog.show();
    }

        @Override
    protected void onPostExecute(final Boolean success) {
            List<Message> titles = new ArrayList<Message>(messages.size());
            for (Message msg : messages){
                titles.add(msg);
            }
            MessageListAdapter adapter = new MessageListAdapter(activity, titles);
            activity.setListAdapter(adapter);
            adapter.notifyDataSetChanged();

            if (dialog.isShowing()) {
            dialog.dismiss();
        }

        if (success) {
            Toast.makeText(context, "OK", Toast.LENGTH_LONG).show();
        } else {
            Toast.makeText(context, "Error", Toast.LENGTH_LONG).show();
        }
    }

    protected Boolean doInBackground(final String... args) {
        try{    
            BaseFeedParser parser = new BaseFeedParser();
            messages = parser.parse();


            return true;
         } catch (Exception e){
            Log.e("tag", "error", e);
            return false;
         }
      }


}

}

Answer:

In the onCreate method:

WebView mWebView;
ProgressDialog pgDiagWebView;

@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.webview);
    pgDiagWebView = ProgressDialog.show(CreateAccountWebView.this, "Loading", "Wait", true);
    mWebView = (WebView) findViewById(R.id.registerWebView);
    mWebView.getSettings().setJavaScriptEnabled(true);
    mWebView.setWebViewClient(new ResgisterWebViewClient()); 
    mWebView.loadUrl("http://www.google.com/");
}

class ResgisterWebViewClient extends WebViewClient {  
    @Override
    public void onPageFinished(WebView view, String url) {
        // TODO Auto-generated method stub
        super.onPageFinished(view, url);
        pgDiagWebView.dismiss();
    }
}