Android custom ProgressDialog example implementation
There is nothing left to worry about. Two custom ProgressDialog are summarized. You can refer to and modify the ProgressDialog as needed:
Effect:
Example 1:
Example 2:
The Code is as follows:
MainActivity: only two Button click events
Package com. customwaitdialog; import android. app. activity; import android. content. intent; import android. OS. bundle; import android. view. view; import android. view. view. onClickListener; import android. widget. button; public class MainActivity extends Activity implements OnClickListener {private Button btn_customDialog1; private Button btn_customDialog2; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); initView ();} private void initView () {btn_customDialog1 = (Button) findViewById (R. id. btn_customDialog1); btn_customDialog2 = (Button) findViewById (R. id. btn_customDialog2); btn_customDialog1.setOnClickListener (this); btn_customDialog2.setOnClickListener (this);} @ Overridepublic void onClick (View v) {switch (v. getId () {// Example 1 case R. id. btn_customDialog1: Intent intent1 = new Intent (this, DialogActivity1.class); startActivity (intent1); break; // Example 2 case R. id. btn_customDialog2: Intent intent2 = new Intent (this, DialogActivity2.class); startActivity (intent2); break; default: break ;}}}
Example 1DialogActivity1:
Package com. customwaitdialog; import android. app. activity; import android. OS. asyncTask; import android. OS. bundle; import com. dialogutil. customWaitDialog1; public class DialogActivity1 extends Activity {private MainFrameTask values = null; private CustomWaitDialog1 progressDialog = null; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R . Layout. activity1); mMainFrameTask = new effeccute () ;}@ Overrideprotected void onDestroy () {stopProgressDialog (); if (mMainFrameTask! = Null &&! MMainFrameTask. isCancelled () {mMainFrameTask. cancel (true);} super. onDestroy ();} private void startProgressDialog () {if (progressDialog = null) {progressDialog = CustomWaitDialog1.createDialog (this); progressDialog. setMessage (loading ...);} progressDialog. show ();} private void stopProgressDialog () {if (progressDialog! = Null) {progressDialog. dismiss (); progressDialog = null ;}} public class MainFrameTask extends AsyncTask
{Private DialogActivity1 mainFrame = null; public MainFrameTask (DialogActivity1 mainFrame) {this. mainFrame = mainFrame;} @ Overrideprotected void onCancelled () {stopProgressDialog (); super. onCancelled () ;}@ Overrideprotected Integer doInBackground (Integer... params) {try {Thread. sleep (10*1000);} catch (InterruptedException e) {e. printStackTrace () ;}return null ;}@ Overrideprotected void onPreExecute () {startProgressDialog () ;}@ Overrideprotected void onPostExecute (Integer result) {stopProgressDialog ();}}}
The focus is CustomWaitDialog1:
package com.dialogutil;import android.app.Dialog;import android.content.Context;import android.graphics.drawable.AnimationDrawable;import android.view.Gravity;import android.widget.ImageView;import android.widget.TextView;import com.customwaitdialog.R;public class CustomWaitDialog1 extends Dialog {private Context context = null;private static CustomWaitDialog1 customProgressDialog = null;public CustomWaitDialog1(Context context) {super(context);this.context = context;}public CustomWaitDialog1(Context context, int theme) {super(context, theme);}public static CustomWaitDialog1 createDialog(Context context) {customProgressDialog = new CustomWaitDialog1(context,R.style.CustomProgressDialog);customProgressDialog.setCanceledOnTouchOutside(false);customProgressDialog.setContentView(R.layout.customprogressdialog);customProgressDialog.getWindow().getAttributes().gravity = Gravity.CENTER;return customProgressDialog;}public void onWindowFocusChanged(boolean hasFocus) {if (customProgressDialog == null) {return;}ImageView imageView = (ImageView) customProgressDialog.findViewById(R.id.loadingImageView);AnimationDrawable animationDrawable = (AnimationDrawable) imageView.getBackground();animationDrawable.start();}public CustomWaitDialog1 setTitile(String strTitle){ return customProgressDialog; } public CustomWaitDialog1 setMessage(String strMessage){ TextView tvMsg = (TextView)customProgressDialog.findViewById(R.id.id_tv_loadingmsg); if (tvMsg != null){ tvMsg.setText(strMessage); } return customProgressDialog; } }
Style:
Layout customprogressdialog. xml:
Rotation Animation: progress_round.xml:
Example 2DialogActivity2:
package com.customwaitdialog;import android.app.Activity;import android.content.DialogInterface;import android.content.DialogInterface.OnDismissListener;import android.os.Bundle;import com.dialogutil.CustomWaitDialog2;public class DialogActivity2 extends Activity {private CustomWaitDialog2 waitDialog;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity2); waitDialog = new CustomWaitDialog2(this); waitDialog.show(); waitDialog.setOnDismissListener(new OnDismissListener() { @Override public void onDismiss(DialogInterface dialog) { } });}}
Wait for Dialog: CustomWaitDialog2:
Package com. dialogutil; import android. app. dialog; import android. content. context; import android. content. dialogInterface; import android. view. window; import android. view. windowManager; import android. view. animation. animation; import android. view. animation. animationUtils; import android. widget. imageView; import com. customwaitdialog. r; public class CustomWaitDialog2 {private Context mContext; private ImageView Image; private ImageView loading_pic_bigView; private Dialog waitDialog; private Animation mAnimation; public CustomWaitDialog2 (Context mContext) {this. mContext = mContext; waitDialog = new Dialog (mContext, R. style. common_dialog); waitDialog. setContentView (R. layout. loading); waitDialog. setCanceledOnTouchOutside (false);/*** set the backdrop, that is, the background layer dimAmount of the dialog is between 0.0f and 1.0f, and 0.0f is completely invisible, that is, the background is visible *, when 1.0f is used, the background becomes dark. ** If you want to darken the background, Set * dialog. getWindow (). addFlags (WindowManager. LayoutParams *. FLAG_DIM_BEHIND). Otherwise, the background is ineffective. */Window window = waitDialog. getWindow (); WindowManager. layoutParams lp = window. getAttributes (); lp. dimAmount = 0.8f; window. setAttributes (lp); window. addFlags (WindowManager. layoutParams. FLAG_DIM_BEHIND); // waitDialog. getWindow (). getAttributes (). gravity = Gravity. CENTER;/*** sets the transparency. The main setting is the transparency of the dialog itself */loading_pic_bigView = (ImageView) waitDialog. findViewById (R. id. loading_pic_bigView); loading_pic_bigView.setAlpha (0.6f); image = (ImageView) waitDialog. findViewById (R. id. loading_pic_big); mAnimation = AnimationUtils. loadAnimation (mContext, R. anim. loading);} public void show () {image. startAnimation (mAnimation); waitDialog. show ();} public void dismiss () {waitDialog. dismiss ();} // used for network request interruption. public void setOnDismissListener (DialogInterface. onDismissListener dismissListener) {waitdiener. setOnDismissListener (dismissListener );}}