Hi,
I will show you how to create a custom progress dialog. Below is the code for doing this:
import java.util.Timer; import java.util.TimerTask; import android.app.Activity; import android.app.Dialog; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.ProgressBar; import android.widget.TextView; public class MyApp extends Activity { Timer test; int ticks = 0; boolean counting = false; Dialog dialog; TextView text1; TextView text2; ProgressBar mProgress; @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.main); //Create new daemon timer test = new Timer(true); test.scheduleAtFixedRate(new TimerTask() { @Override public void run() { TimerMethod(); }//run }, 1000, 1000);//schedule Button start = (Button)findViewById(R.id.button_start); start.setOnClickListener(new View.OnClickListener() { public void onClick(View view) { ticks = 0; counting = true; dialog = new Dialog(MyApp.this); dialog.setContentView(R.layout.custom_dialog); dialog.setTitle("Load Progress"); mProgress = (ProgressBar) dialog.findViewById(R.id.progressbar); text1 = (TextView) dialog.findViewById(R.id.text1); text1.setText("0%"); text2 = (TextView) dialog.findViewById(R.id.text2); text2.setText("0Mb/0Mb"); dialog.show(); } }); } private void TimerMethod() { //This method is called directly by the timer //and runs in the same thread as the timer. //We call the method that will work with the UI //through the runOnUiThread method. this.runOnUiThread(Timer_Tick); }//timermethod private Runnable Timer_Tick = new Runnable() { public void run() { //This method runs in the same thread as the UI. //Do something to the UI thread here if (counting) { ticks++; int percent = (int)(100f * ((float)ticks/10f)); String newRatio = String.format("%1$dMb/%2$dMb", ticks, 10); String newPercent = String.format("%1$d%%", percent); text1.setText(newPercent); text2.setText(newRatio); mProgress.setMax(10); mProgress.setProgress(ticks); if (ticks == 10) { counting = false; dialog.dismiss(); }//ending }//counting }//run };//runnable //TODO: Fill In Methods Etc. }//class
And here is the custom_dialog.xml code:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/layout_root" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="wrap_content" android:padding="10dp"> <ProgressBar android:id="@+id/progressbar" android:layout_width="fill_parent" android:layout_height="wrap_content" style="@android:style/Widget.ProgressBar.Horizontal" android:paddingTop="20dp" android:paddingBottom="20dp" /> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:padding="10dp"> <TextView android:id="@+id/text1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#FFF" android:layout_alignParentLeft="true" /> <TextView android:id="@+id/text2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#FFF" android:layout_alignParentRight="true" /> </RelativeLayout> </LinearLayout>
Nice. Been looking for this for a while. Other forums just tried to “hack it” by using animation xmls, no threading involved. But this looks great. Offers more control.