Search This Blog

Showing posts with label progress. Show all posts
Showing posts with label progress. Show all posts

23 April 2010

Progress Bar

Again building off any of our previous examples; this time we are going to do a progress dialog...

Open MyActivity.java and replace the onCreate method:

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        final ProgressDialog dialog = new ProgressDialog(MyActivity.this);
        dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        dialog.setMessage("Downloading 200gb...");
        dialog.setCancelable(true);
        dialog.setMax(200);
        dialog.setProgress(0);
        dialog.show();

        Thread t = new Thread(new Runnable(){
            public void run() {
                while(dialog.getProgress() < dialog.getMax())
                {
                    dialog.incrementProgressBy(1);
                    try{Thread.sleep(50);}catch(Exception e){/* no-op */}
                }
                dialog.dismiss();
            }
        });
        t.start();
    }

Redeploy (mvn clean install) and launch.

Loading, please wait...

This time we are going to show the standard Please Wait dialog with the spinning circle.  This example is taken from the developer documentation.  Using any of our examples as a starting point, let's replace the onCreate method in MyActivity.

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        final ProgressDialog dialog = ProgressDialog.show(MyActivity.this, "", "Loading. Please wait...", true);
        Thread t = new Thread(new Runnable(){
            public void run() {
                try{
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    // no-op
                }
                dialog.dismiss();
            }
        });
        t.start();
    }

Redeploy (mvn clean install) and the dialog will show for 5 seconds before going away.