One common UI feature in an Android device is the “Please wait” dialog that you typically see
when an application is performing a long-running task. For example, the application may be logging in to a server before the user is allowed to use it, or it may be doing a calculation before displaying the result to the user. In suchcases, it is helpful to display a dialog, known as a progress dialog, so that the user is kept in the loop.
The following Try It Out demonstrates how to display such a dialog.
1. Using the same project created in the previous section, add the following statements in bold to the
main.xml file:
<?xml version=”1.0” encoding=”utf-8”?>
android:layout_width=”fill_parent” android:layout_height=”fill_parent” android:orientation=”vertical” >
<Button android:id=”@+id/btn_dialog” android:layout_width=”fill_parent” android:layout_height=”wrap_content” android:text=”Click to display a dialog” android:onClick=”onClick” />
<Button android:id=”@+id/btn_dialog2” android:layout_width=”fill_parent” android:layout_height=”wrap_content” android:text=”Click to display a progress dialog” android:onClick=”onClick2” />
</LinearLayout>
2. Add the following statements in bold to the DialogActivity.java file:
package net.learn2develop.Dialog;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.app.ProgressDialog; import android.content.DialogInterface; import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
public class DialogActivity extends Activity {
CharSequence[] items = { “Google”, “Apple”, “Microsoft” };
boolean[] itemsChecked = new boolean [items.length];
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main);
}
public void onClick(View v) { showDialog(0);
}
public void onClick2(View v) {
//---show the dialog---
final ProgressDialog dialog = ProgressDialog.show(
this, “Doing something”, “Please wait...”, true);
new Thread(new Runnable(){
public void run(){
try {
//---simulate doing something lengthy---
Thread.sleep(5000);
//---dismiss the dialog---
dialog.dismiss();
} catch (InterruptedException e) { e.printStackTrace();
}
}
}).start();
}
@Override
protected Dialog onCreateDialog(int id) { ... }
}
3. Press F11 to debug the application on the Android
emulator. Clicking the second button will display the progress dialog, as shown in Figure 2-6. It will go away after five seconds.
FIGURE 2-6
How It Works
Basically, to create a progress dialog, you created an instance of the
ProgressDialog class and called its show() method:
//---show the dialog---
final ProgressDialog dialog = ProgressDialog.show(
this, “Doing something”, “Please wait...”, true);
This displays the progress dialog that you have just seen. Because this is a modal dialog, it will block the UI until it is dismissed. To perform a long-running task in the background, you created a Thread using a Runnable block(you will learn more about threading in Chapter 11). The code that you
placed inside the run() method will be executed in a separate thread, and in this case you simulated it performing something for five seconds by inserting a delay using the sleep() method:
new Thread(new Runnable(){
public void run(){
try {
//---simulate doing something lengthy---
Thread.sleep(5000);
//---dismiss the dialog---
dialog.dismiss();
} catch (InterruptedException e) { e.printStackTrace();
}
}
}).start();
After the five seconds elapse, you dismiss the dialog by calling the dismss() method.
Displaying a More Sophisticated Progress Dialog
Besides the generic “please wait” dialog that you created in the previous section, you can also create a dialog that displays the progress of an operation, such as the status of a download.
The following Try It Out shows you how to display a specialized progress dialog.
1. Using the same project created in the previous section, add the following lines in bold to the
main.xml file:
<?xml version=”1.0” encoding=”utf-8”?>
<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android” android:layout_width=”fill_parent” android:layout_height=”fill_parent”
android:orientation=”vertical” >
<Button android:id=”@+id/btn_dialog” android:layout_width=”fill_parent” android:layout_height=”wrap_content” android:text=”Click to display a dialog” android:onClick=”onClick” />
<Button android:id=”@+id/btn_dialog2” android:layout_width=”fill_parent” android:layout_height=”wrap_content” android:text=”Click to display a progress dialog” android:onClick=”onClick2” />
<Button android:id=”@+id/btn_dialog3” android:layout_width=”fill_parent” android:layout_height=”wrap_content”
android:text=”Click to display a detailed progress dialog”
android:onClick=”onClick3” />
</LinearLayout>
2. Add the following statements in bold to the DialogActivity.java file:
package net.learn2develop.Dialog;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.app.ProgressDialog; import android.content.DialogInterface; import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
public class DialogActivity extends Activity {
CharSequence[] items = { “Google”, “Apple”, “Microsoft” };
boolean[] itemsChecked = new boolean [items.length];
ProgressDialog progressDialog;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) { ... }
public void onClick(View v) { ... } public void onClick2(View v) { ... } public void onClick3(View v) {
showDialog(1);
progressDialog.setProgress(0);
new Thread(new Runnable(){
public void run(){
for (int i=1; i<=15; i++) {
try {
//---simulate doing something lengthy---
Thread.sleep(1000);
//---update the dialog---
progressDialog.incrementProgressBy((int)(100/15));
} catch (InterruptedException e) { e.printStackTrace();
}
}
progressDialog.dismiss();
}
}).start();
}
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case 0:
return new AlertDialog.Builder(this)
//...
).create();
case 1:
progressDialog = new ProgressDialog(this); progressDialog.setIcon(R.drawable.ic_launcher); progressDialog.setTitle(“Downloading files...”); progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);progressDialog.setButton(DialogInterface.BUTTON_POSITIVE, “OK”,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton)});
{
Toast.makeText(getBaseContext(),
“OK 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. Press F11 to debug the application on the Android emulator. Click the third button to display the progress dialog (see Figure 2-7). Note that the progress bar will count up to 100%.
FIGURE 2-7
How It Works
To create a dialog that shows the progress of an operation, you first create an instance of the ProgressDialog
class and set its various properties, such as icon, title, and style:
progressDialog = new ProgressDialog(this);
progressDialog.setIcon(R.drawable.ic_launcher); progressDialog.setTitle(“Downloading files...”); progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
You then set the two buttons that you want to display inside the progress dialog:
progressDialog.setButton(DialogInterface.BUTTON_POSITIVE, “OK”,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton)
{});
Toast.makeText(getBaseContext(),
“OK 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();
}
FIGURE 2-8
The preceding code causes a progress dialog to appear
(see Figure 2.8). To display the progress status in the progres dialog, you can use a Thread object to run a Runnable block of code:
new Thread(new Runnable(){
public void run(){
for (int i=1; i<=15; i++) {
try {
//---simulate doing something lengthy---
Thread.sleep(1000);
//---update the dialog---
progressDialog.incrementProgressBy((int)(100/15));
} catch (InterruptedException e) { e.printStackTrace();
}
}
progressDialog.dismiss();
}
}).start();
Không có nhận xét nào:
Đăng nhận xét