Custom network loading dialog box
When the App interacts with the server over the network, a prompt loading box is required. In this case, you can customize a dialog box for loading. The Code is as follows:
Public class LoadingDialog extends Dialog {
Private static final int CHANGE_TITLE_WHAT = 1;
Private static final int CHNAGE_TITLE_DELAYMILLIS = 300;
Private static final int MAX_SUFFIX_NUMBER = 3;
Private static final char SUFFIX = '.';
Private ImageView iv_route;
Private TextView detail_ TV;
Private TextView TV _point;
Private RotateAnimation mAnim;
Private Handler handler = new Handler (){
Private int num = 0;
Public void handleMessage (android. OS. Message msg ){
If (msg. what = CHANGE_TITLE_WHAT ){
StringBuilder builder = new StringBuilder ();
If (num> = MAX_SUFFIX_NUMBER ){
Num = 0;
}
Num ++;
For (int I = 0; I <num; I ++ ){
Builder. append (SUFFIX );
}
TV _point.setText (builder. toString ());
If (isShowing ()){
Handler. sendEmptyMessageDelayed (CHANGE_TITLE_WHAT, CHNAGE_TITLE_DELAYMILLIS );
}
Else {
Num = 0;
}
}
};
};
Public LoadingDialog (Context context ){
Super (context, R. style. Dialog_bocop );
Init ();
}
Public LoadingDialog (Context context, boolean isTrans ){
Super (context, isTrans? R. style. Loading_Dialog_trans: R. style. Dialog_bocop );
Init ();
}
Private void init (){
SetContentView (R. layout. common_dialog_loading_layout );
Iv_route = (ImageView) findViewById (R. id. iv_route );
Detail_ TV = (TextView) findViewById (R. id. detail_ TV );
TV _point = (TextView) findViewById (R. id. TV _point );
InitAnim ();
GetWindow (). setWindowAnimations (R. anim. alpha_in );
}
Private void initAnim (){
// MAnim = new RotateAnimation (360, 0, Animation. RESTART, 0.5f, Animation. RESTART, 0.5f );
MAnim = new RotateAnimation (0,360, Animation. RESTART, 0.5f, Animation. RESTART, 0.5f );
MAnim. setDuration (2000 );
MAnim. setRepeatCount (Animation. INFINITE );
MAnim. setRepeatMode (Animation. RESTART );
MAnim. setStartTime (Animation. START_ON_FIRST_FRAME );
}
@ Override
Public void show () {// call this method where you want to use it
Iv_route.startAnimation (mAnim );
Handler. sendEmptyMessage (CHANGE_TITLE_WHAT );
Super. show ();
}
@ Override
Public void dismiss (){
MAnim. cancel ();
Super. dismiss ();
}
@ Override
Public void setTitle (CharSequence title ){
If (TextUtils. isEmpty (title )){
Detail_ TV .setText ("loading ");
}
Else {
Detail_ TV .setText (title );
}
}
@ Override
Public void setTitle (int titleId ){
SetTitle (getContext (). getString (titleId ));
}
Public static void dismissDialog (LoadingDialog loadingDialog ){
If (null = loadingDialog) {return ;}
LoadingDialog. dismiss ();
}
}
------------- The corresponding layout is as follows ------------------
Xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "160dp"
Android: layout_height = "160dp"
Android: layout_gravity = "center"
Android: background = "@ drawable/common_show_dialog"
Android: orientation = "vertical">
Android: layout_width = "fill_parent"
Android: layout_height = "0dp"
Android: layout_weight = "3"
Android: paddingTop = "22dp"
Android: gravity = "center">
Android: id = "@ + id/iv_route"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_centerHorizontal = "true"
Android: layout_centerVertical = "true"
Android: background = "@ drawable/dialog_bocop_loading_rotate_anim_img"/>
Android: layout_width = "fill_parent"
Android: layout_height = "0dp"
Android: layout_marginBottom = "15dp"
Android: layout_marginLeft = "10dp"
Android: layout_marginRight = "10dp"
Android: layout_weight = "1"
Android: gravity = "center_horizontal">
Android: id = "@ + id/detail_ TV"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_toLeftOf = "@ + id/TV _point"
Android: ellipsize = "marquee"
Android: gravity = "center"
Android: singleLine = "true"
Android: text = "loading"
Android: textColor = "# ffffff"
Android: textSize = "20sp"/>
Android: id = "@ + id/TV _point"
Android: layout_width = "20dp"
Android: layout_height = "wrap_content"
Android: layout_alignParentRight = "true"
Android: text = "..."
Android: textColor = "# ffffff"
Android: textSize = "20sp"/>
For example, you need to call the load dialog box in the Activity:
LoadingDialog loadingDialog;
If (null = loadingDialog ){
LoadingDialog = new LoadingDialog (aty );
LoadingDialog. setOnCancelListener (this );
}
LoadingDialog. setTitle ("loading data ");
If (! LoadingDialog. isShowing () loadingDialog. show ();