Android development primer: Progress Dialog in Android – Horizontal Style

This entry is part 17 of 50 in the series Android Development Primer

In the last post, we saw how to make a Progress Dialog with Spinner style progress bar. Here, we will see how to make a Horizontal Style bar.

  • Set the content view of the activity. Make sure the layout of the activity contains a button so that the Progress Dialog can be triggered on the click of the button.
  • After setting the content view, declare and define the button in the java file.
  • Add the Progress Dialog hereafter, using the following code.
    final ProgressDialog pd = new ProgressDialog(this);
    pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
    pd.setMessage("Working...");
    pd.setIndeterminate(false);
    pd.setCancelable(true);
    

    We will not be defining the Progress Dialog inside the onClick() method of the button so that we can use this dialog in other places, if need be.

  • Set up the onClickListener() and within it the onClick() methods. Inside the onClick() method, write the following code.
    pd.show();
    pd.setProgress(30);
    

    In addition to showing the Progress Dialog we also need to set the progress of the bar.

  • Save your work and execute on an emulator/device.

Progress2

Points to Note :-

  • On examining the code carefully, you will find that in addition to change in the setProgressStyle(), the setIndeterminate() method now has a value of false.
  • This is because we will be showing the progress of the work in percentage now. So the dialog needs not be indeterminately displaying till the work ends.
  • We have set the initial percentage progress only for demonstration purposes. When you will be incorporating this in your application, you will be performing some tasks and updating the progress of the dialog accordingly.

COMPLETE SOURCE CODE

MainActivity.java

package com.nero.myfirstapp;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class Main extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button but = (Button) findViewById(R.id.but);
        final ProgressDialog pd = new ProgressDialog(Main.this);
        pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        pd.setMessage("Working...");
        pd.setIndeterminate(false);
        pd.setCancelable(true);

        but.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				pd.show();
				pd.setProgress(30);
				//Do some more tasks
				//update the progress of the dialog
				//Do some more tasks
				//update the progress of the dialog
			}

		});

    }
}

Series Navigation<< Android development primer: Progress Dialog in Android – Spinner StyleAndroid development primer: Working with Audio in Android >>

Leave a Reply

Your email address will not be published. Required fields are marked *

*

* Copy This Password *

* Type Or Paste Password Here *

42,383 Spam Comments Blocked so far by Spam Free Wordpress

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>