Wednesday, January 23, 2013

Progress bar in Android : Custom Horizontal Progress bar demo with dialog box

Hi Friends,
Today I am going to share very Important code for custom horizontal progress bar with positive and negative dialog box..

1)Print Screen:


2)ProgressBarActivity

package com.example.progressbardemo;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class ProgressBarActivity extends Activity {
CharSequence[] values = { "Android", "Apple", "Java" };
boolean[] itemsChecked = new boolean[values.length];
private ProgressDialog _progressDialog;
private int _progress = 0;
private Handler _progressHandler;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_progress_bar);
/**
* Set click on button
*/
Button btn = (Button) findViewById(R.id.button1);
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
showDialog(1);
_progress = 0;
_progressDialog.setProgress(0);
_progressHandler.sendEmptyMessage(0);
}
});
/**
* start progress thread
*/
_progressHandler = new Handler() {
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (_progress >= 100) {
_progressDialog.dismiss();
} else {
_progress++;
_progressDialog.incrementProgressBy(1);
_progressHandler.sendEmptyMessageDelayed(0, 100);
}
}
};
}

/**
* create dialog for hide and cancel
*/
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case 0:
return new AlertDialog.Builder(this)
.setIcon(R.drawable.ic_launcher)
.setTitle("Open Dialog with text...")
/**
* set positive button
*/
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
Toast.makeText(getBaseContext(),
"OK button clicked!",
Toast.LENGTH_SHORT).show();
}
})
/**
* set negative button
*/
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
Toast.makeText(getBaseContext(),
"Cancel button clicked!",
Toast.LENGTH_SHORT).show();
}
})
.setMultiChoiceItems(values, itemsChecked,
new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which, boolean isChecked) {
Toast.makeText(
getBaseContext(),
values[which]
+ (isChecked ? " checked!"
: " unchecked!"),
Toast.LENGTH_SHORT).show();
}
}).create();
case 1:
_progressDialog = new ProgressDialog(this);
_progressDialog.setIcon(R.drawable.ic_launcher);
_progressDialog.setTitle("Uploading files...");
_progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
_progressDialog.setButton(DialogInterface.BUTTON_POSITIVE, "Hide",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
Toast.makeText(getBaseContext(), "Hide clicked!",
Toast.LENGTH_SHORT).show();
}
});
_progressDialog.setButton(DialogInterface.BUTTON_NEGATIVE,
"Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
Toast.makeText(getBaseContext(), "Cancel clicked!",
Toast.LENGTH_SHORT).show();
}
});
return _progressDialog;
}
return null;
}
}


3)activity_progress_bar.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:padding="@dimen/padding_medium"
android:text="@string/hello_world"
android:textSize="28sp"
tools:context=".ProgressBarActivity" />

<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Test Demo" />

</RelativeLayout>

Thanks,