Android custom ProgressDialog and progressdialog

Source: Internet
Author: User

Android custom ProgressDialog and progressdialog

Reprinted please indicate the source:Http://blog.csdn.net/allen315410/article/details/41699063

When developing Android applications, we often encounter "latency" waits, for example, when loading data, especially when connecting to the Internet, the request network may have a waiting time, in this waiting time, you need to give a friendly prompt to the user, prompting the user what operation is being done, waiting patiently, etc. Then a progress dialog box can be solved. Android provides us with a good control called ProgressDialog, which is used to create custom information and some related operations. The only bad thing is that the Android native control gives me a monotonous and ugly one, the source code and effects of the native ProgressDialog are as follows:

Public class MainActivity extends Activity {@ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); ProgressDialog dialog = new ProgressDialog (this); dialog. setMessage ("logging in ...... "); Dialog. show ();}}


The source code is extremely simple and the effect is extremely simple. So how can we make this ProgressDialog look better? In fact, you may wish to make a custom ProgressDialog. Let's take a look at the effect of the custom ProgressDialog!


Okay. It looks good! Next we will start to implement it step by step!

1. Customize the ProgressDialog layout.

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:layout_gravity="center"    android:background="@drawable/progress_custom_bg"    android:gravity="center_horizontal"    android:orientation="vertical"    android:paddingBottom="20dp"    android:paddingLeft="30dp"    android:paddingRight="30dp"    android:paddingTop="20dp" >    <ImageView        android:id="@+id/spinnerImageView"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:background="@anim/spinner" />    <TextView        android:id="@+id/message"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_marginTop="15dp"        android:text="Message"        android:textColor="#FFFFFF" /></LinearLayout>

2. Set the background of the scroll bar.

In the XML layout above, we can see that the scroll bar is an ImageView. We need to set a dynamic background for the ImageView. What should we do with this dynamic background? In fact, it is to give the ImageView an animation background, give a certain number of images, and switch the images at a constant speed within a certain period of time in the animation. The image resources are as follows:


In the res/anim folder, create an animation set, "spinner. xml:

<animation-list xmlns:android="http://schemas.android.com/apk/res/android"    android:oneshot="false" >    <item        android:drawable="@drawable/spinner_0"        android:duration="60"/>    <item        android:drawable="@drawable/spinner_1"        android:duration="60"/>    <item        android:drawable="@drawable/spinner_2"        android:duration="60"/>    <item        android:drawable="@drawable/spinner_3"        android:duration="60"/>    <item        android:drawable="@drawable/spinner_4"        android:duration="60"/>    <item        android:drawable="@drawable/spinner_5"        android:duration="60"/>    <item        android:drawable="@drawable/spinner_6"        android:duration="60"/>    <item        android:drawable="@drawable/spinner_7"        android:duration="60"/>    <item        android:drawable="@drawable/spinner_8"        android:duration="60"/>    <item        android:drawable="@drawable/spinner_9"        android:duration="60"/>    <item        android:drawable="@drawable/spinner_10"        android:duration="60"/>    <item        android:drawable="@drawable/spinner_11"        android:duration="60"/></animation-list>
Then set a background color progress_custom_bg.xml for the entire ProgressDialog:

<?xml version="1.0" encoding="UTF-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android" >    <solid android:color="#ff404040" />    <corners        android:bottomLeftRadius="8dp"        android:bottomRightRadius="8dp"        android:topLeftRadius="8dp"        android:topRightRadius="8dp" /></shape>

Then you need to set a style for the custom ProgressDialog, which is defined in res/value/style. xml as follows:

<! -- Custom Dialog --> <style name = "Custom_Progress" parent = "@ android: style/Theme. dialog "> <item name =" android: windowFrame "> @ null </item> <item name =" android: windowIsFloating "> true </item> <item name =" android: windowContentOverlay "> @ null </item> <item name =" android: windowAnimationStyle "> @ android: style/Animation. dialog </item> <item name = "android: windowSoftInputMode"> stateUnspecified | adjustPan </item> <item name = "android: windowBackground"> @ android: color/transparent </item> <item name = "android: windowNoTitle"> true </item> </style>

3. Next we will focus on building a custom ProgressDialog through code. The specific practice is to inherit the Dialog class:

Package com. example. myexample; import android. app. dialog; import android. content. context; import android. graphics. drawable. animationDrawable; import android. view. gravity; import android. view. view; import android. view. windowManager; import android. widget. imageView; import android. widget. textView; public class CustomProgress extends Dialog {public CustomProgress (Context context) {super (context);} public Custom Progress (Context context, int theme) {super (context, theme);}/*** call */public void onWindowFocusChanged (boolean hasFocus) when the window focus changes) {ImageView imageView = (ImageView) findViewById (R. id. spinnerImageView); // obtain the animation background of the ImageView. getBackground (); // starts the animation spinner. start ();}/*** set the prompt information for Dialog ** @ param message */public void setMessage (CharSequence mes Sage) {if (message! = Null & message. length ()> 0) {findViewById (R. id. message ). setVisibility (View. VISIBLE); TextView txt = (TextView) findViewById (R. id. message); txt. setText (message); txt. invalidate ();}} /*** pop up the custom ProgressDialog ** @ param context * @ param message * prompt * @ param cancelable * whether to press the return key to cancel * @ param cancelListener * click the return key to listen *@ return */public static CustomProgress show (Context context, charSequence message, boolean cancelable, OnCancelListener cancelListener) {CustomProgress dialog = new CustomProgress (context, R. style. custom_Progress); dialog. setTitle (""); dialog. setContentView (R. layout. progress_custom); if (message = null | message. length () = 0) {dialog. findViewById (R. id. message ). setVisibility (View. GONE);} else {TextView txt = (TextView) dialog. findViewById (R. id. message); txt. setText (message);} // press the return key to cancel the dialog. setCancelable (cancelable); // listens to the Return key to process the dialog. setOnCancelListener (cancelListener); // set the center Diener. getWindow (). getAttributes (). gravity = Gravity. CENTER; WindowManager. layoutParams lp = dialog. getWindow (). getAttributes (); // sets the background layer transparency lp. dimAmount = 0.2f; dialog. getWindow (). setAttributes (lp); // dialog. getWindow (). addFlags (WindowManager. layoutParams. FLAG_BLUR_BEHIND); dialog. show (); return dialog ;}}
Reference custom ProgressDialog in Activity:

Public class MainActivity extends Activity {@ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); CustomProgress. show (this, "logging on... ", true, null );}}
Now, the entire process is complete, and our simple custom progress dialog box is ready. The effect is already done and will not be posted here. I will not explain the code in it. It is relatively simple and has some comments. If you like it, click Download source code below.


Download the source code here


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.