Android Implementation Load Dialog Box

  • 2021-11-14 07:12:16
  • OfStack

This article example for everyone to share the Android implementation of the specific load dialog box code, for your reference, the specific content is as follows

Here is a simple 1 under the two ways to load dialog: 1. Use animation to rotate a picture 2. Use progressbar.

Simply put, dialog is a pop-up window, and the layout defined by yourself is placed in window. The loading dialog box is a loaded animation, and the core is to realize this animation. So the methods can be, add animation to pictures, or use progressbar.

Method 1: Use animation to rotate 1 picture

Look at the layout first:


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:background="@drawable/dialog_bg_while"
 android:orientation="vertical" >
 
 <ImageView
  android:layout_width="54dp"
  android:id="@+id/loading_dialog_pic"
  android:layout_height="54dp"
  android:layout_gravity="center_horizontal"
  android:layout_marginTop="15dp"
  android:background="@drawable/loading" />
 
 <TextView
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_gravity="center_horizontal"
  android:layout_marginTop="10dp"
  android:text=" Loading ..." />
 
</LinearLayout>

Then customize Alertdialog and add rotation animation to the picture:


public class LoadingDialog extends AlertDialog {
 private final String DEFAULT_TEXT=" Loading ";
 private ImageView mImageview;
 private TextView mTextView;
 private LinearLayout mLayout;
 private String mText;
 
 protected LoadingDialog(Context context) {
 super(context);
 // TODO Auto-generated constructor stub
 }
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 // TODO Auto-generated method stub
 super.onCreate(savedInstanceState);
 mLayout=(LinearLayout) LinearLayout.inflate(getContext(), R.layout.loading_dialog, null);
 mImageview=(ImageView) mLayout.findViewById(R.id.loading_dialog_pic);
 mTextView=(TextView) mLayout.findViewById(R.id.loading_dialog_text); 
 loadanimation();
 getWindow().setContentView(mLayout);
 
 }
 
 private void loadanimation() {// Add rotation animation to the picture 
 // TODO Auto-generated method stub
 Animation anim=AnimationUtils.loadAnimation(getContext(), R.anim.loading_dialog_anim);
 LinearInterpolator lin = new LinearInterpolator(); 
 anim.setInterpolator(lin);
 mImageview.setAnimation(anim);
 
 }
 
 
}

Look at the animation of xml:


<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
 
 <rotate
  android:duration="1500"
  android:pivotX="50%"   
  android:pivotY="50%"
  android:fromDegrees="0.0" 
  android:repeatCount="infinite"
  android:toDegrees="-358" />
 
</set>

Mode 2: Use progressbar

The first is an animation-list:


<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" >
 
 <item
  android:drawable="@drawable/loading1"
  android:duration="100"/>
 <item
  android:drawable="@drawable/loading2"
  android:duration="100"/>
 <item
  android:drawable="@drawable/loading3"
  android:duration="100"/>
 <item
  android:drawable="@drawable/loading4"
  android:duration="100"/>
 <item
  android:drawable="@drawable/loading5"
  android:duration="100"/>
 <item
  android:drawable="@drawable/loading6"
  android:duration="100"/>
 <item
  android:drawable="@drawable/loading7"
  android:duration="100"/>
 <item
  android:drawable="@drawable/loading8"
  android:duration="100"/>
 
 
</animation-list>

Look at the implementation of the layout under 1:


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:background="@drawable/dialog_bg_while"
 android:orientation="vertical" >
 
 <ProgressBar
  style="@android:style/Widget.ProgressBar.Large"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_gravity="center_horizontal"
  android:layout_marginTop="10dp"
  android:indeterminateDrawable="@drawable/loading_animation_list"
  android:indeterminateDuration="1500" />
 
 <View
  android:layout_width="match_parent"
  android:layout_height="1dp"
  android:background="#00BCD4" />
 
 <TextView
  android:id="@+id/loading_dialog_text"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_gravity="center_horizontal"
  android:layout_marginTop="10dp"
  android:text=" Loading ..." />
 
</LinearLayout>

Then customize 1 alertdialog:


public class LoadingDialog extends AlertDialog {
 private final String DEFAULT_TEXT=" Loading ";
 private TextView mTextView;
 private LinearLayout mLayout;
 private String mText;
 
 protected LoadingDialog(Context context) {
 super(context);
 // TODO Auto-generated constructor stub
 }
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 // TODO Auto-generated method stub
 super.onCreate(savedInstanceState);
 mLayout=(LinearLayout) LinearLayout.inflate(getContext(), R.layout.loading_dialog, null); 
 mTextView=(TextView) mLayout.findViewById(R.id.loading_dialog_text);
 WindowManager m=(WindowManager) getContext().getSystemService(getContext().WINDOW_SERVICE);
 int windowwith=m.getDefaultDisplay().getWidth();
 int w=windowwith*3/5;
 int h=300; 
 getWindow().setLayout(w, h);// Setting the dialog box form size 
 getWindow().setContentView(mLayout);
 
 }
 
 
}

Related articles: