Android custom AlertDialog dialog box (loading prompt box) and Android alertdialog
AlertDialog has the following six usage methods:
1. Simple AlertDialog (only a simple piece of information is displayed)
Ii. AlertDialog with buttons (display prompt information for user operation)
Iii. AlertDialog (display content) similar to ListView)
4. AlertDialog similar to RadioButton)
5. AlertDialog similar to CheckBox (allow multiple users to select)
6. Customize the AlertDialog of the custom View (when the above method cannot meet your needs, you must customize it)
Here is the sixth usage, as shown below (the effect is similar to MBProgressHUD in IOS)
The specific implementation is as follows:
. Java code
// In the custom pop-up box, place the image in the box and set the rotation animation for the image.
AlertDialog alert_progress = new AlertDialog. Builder (current activity. this). create ();
Alert_progress.show ();
Alert_progress.setCancelable (false); // the dialog box does not disappear when you click the background.
// Alert_progress.dismiss (); // cancel the dialog box
Window window = alert_progress.getWindow ();
Window. setContentView (R. layout. alert_dialog_progress_view); // load the custom layout File
WindowManager. LayoutParams wm = window. getAttributes ();
Wm. width = 250; // set the dialog box width
Wm. height = 200; // The height of the Setting Dialog Box
Wm. alpha = 0.5f; // background transparency of the dialog box
Wm. dimAmount = 0.6f; // mask layer brightness
Window. setAttributes (wm );
ImageView img = (ImageView) window. findViewById (R. id. progress_bar); // obtain the ImageView control in the layout file.
Img. setBackgroundResource (R. drawable. loading_one); // you can set the image in the layout file.
// Set the rotation Animation
Animation tranfrom = new RotateAnimation (0,359, Animation. RELATIVE_TO_SELF, 0.5f, Animation. RELATIVE_TO_SELF, 0.5f)
Tranfrom. setDuration (2000); // rotation speed
Tranfrom. setFillAfter (true );
Tranfrom. setRepeatCount (-1); //-1 is a single rotation. If it is 10, it is rotated for 10 times and then stopped.
// Tranfrom. cancel (); // cancel the animation
Img. setAnimation (tranfrom );
Layout code:
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: orientation = "vertical"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent"
Android: background = "@ color/colorBlack">
<ImageView
Android: id = "@ + id/progress_bar"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_gravity = "center"
Android: layout_marginTop = "10dp"
Android: layout_marginBottom = "10dp"/>
<TextView
Android: text = "loading ..."
Android: layout_width = "match_parent"
Android: layout_height = "20dp"
Android: textColor = "@ color/colorWhite"
Android: layout_gravity = "center"
Android: gravity = "center"/>
</LinearLayout>
Appendix: rotating the background image