Custom Dialog Class
/**
*
*/
package com.exam.customprogress;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.os.Handler;
import android.os.Message;
import android.widget.ProgressBar;
import android.widget.TextView;
/**
* @author Tatyabhau chavan
*
*/
public class MyDialog extends Dialog {
public static final int STATUS_UPDATE = 101;
public static final int STATUS_COMPLETE = 100;
public static ProgressBar progressBar;
TextView textView;
TextView percent;
int increment;
int progress;
public MyDialog(Context context) {
super(context);
// TODO Auto-generated constructor stub
setContentView(R.layout.my_progress);
setDialog();
}
private void setDialog() {
setTitle("Downloading Files....");
textView = (TextView) findViewById(R.id.textProgress);
progressBar = (ProgressBar) findViewById(R.id.progress_horizontal);
percent = (TextView) findViewById(R.id.textPercentage);
percent.setTextColor(Color.BLACK);
textView.setTextColor(Color.BLACK);
progressBar.setProgressDrawable(getContext().getResources()
.getDrawable(R.drawable.greenbackground));
progressBar.setIndeterminate(false);
// set the maximum value
progressBar.setMax(1315);
LauncherThread();
}
// handler for the background updating
Handler progressHandler = new Handler() {
public void handleMessage(Message msg) {
switch (msg.what) {
case STATUS_UPDATE:
progressBar.setProgress(increment);
float value = increment / 1315F;
percent.setText(" " + ((int) (value * 100)) + "%");
System.out.println(value * 100);
textView.setText(String.valueOf(progressBar.getProgress())
.concat(" / " + progressBar.getMax()));
break;
// case STATUS_COMPLETE:
// if (progressBar != null && progressBar.isShown()) {
// dismiss();
// progressBar = null;
// }
// dismiss();
}
}
};
private class LoaderThread extends Thread {
@Override
public void run() {
// TODO Auto-generated method stub
while (progressBar.getProgress() < progressBar.getMax()) {
try {
Thread.sleep(100);
increment++;
progressHandler.sendEmptyMessage(STATUS_UPDATE);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
progressHandler.sendEmptyMessage(STATUS_COMPLETE);
}
}
private class LauncherThread extends Thread {
@Override
public void run() {
// TODO Auto-generated method stub
progressHandler.sendMessage(progressHandler.obtainMessage());
progressHandler.sendEmptyMessage(0);
}
}
private void LauncherThread() {
LoaderThread loaderThread = new LoaderThread();
// if (!loaderThread.isAlive())
loaderThread.start();
LauncherThread launcehrThread = new LauncherThread();
// if (!launcehrThread.isAlive())
launcehrThread.start();
}
}
Main Activity :-
package com.exam.customprogress;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import com.tomdignan.nifty.dialogs.NiftyBaseDialog;
public class MainActivity extends Activity {
NiftyBaseDialog mDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MyDialog dialog = new MyDialog(this);
dialog.show();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ProgressBar
android:id="@+id/progress_horizontal"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="10dip" />
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"
android:layout_marginRight="10dip" >
<TextView
android:id="@+id/textProgress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@android:color/black" />
<TextView
android:id="@+id/textPercentage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:textColor="@android:color/black" />
</RelativeLayout>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dip"
android:layout_marginRight="5dip"
android:gravity="right"
android:text="* Parkeon and Telepark records will be added." />
</LinearLayout>
Comments
Post a Comment