Custom network loading dialog box, custom loading dialog box

Source: Internet
Author: User

Custom network loading dialog box, custom 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 ------------------

<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout
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">


<RelativeLayout
Android: layout_width = "fill_parent"
Android: layout_height = "0dp"
Android: layout_weight = "3"
Android: paddingTop = "22dp"
Android: gravity = "center">


<ImageView
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"/>
</RelativeLayout>


<RelativeLayout
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">


<TextView
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"/>


<TextView
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"/>
</RelativeLayout>
</LinearLayout>

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 ();

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.