Android ProgressDialog Control Customization

Source: Internet
Author: User

In the project, we often need to go to the server through the network request to obtain the corresponding data, in order to display on the client. And this process requires a network, so there is the process of waiting. For the fast-moving children's boots, the waiting time is short, and for the network's slow boots, the waiting time is long. So in order to eliminate the anxiety of the children's boots waiting, we need to show a progress dialog to remind the children boots, the data is getting in, please wait a moment.


First up, as follows:



So how does it work? Directly on the code


1. Create the Sf_progress_dialog_anim.xml in the Anim folder to achieve the effect of rotation:

<?xml version= "1.0" encoding= "Utf-8"? ><animation-list xmlns:android= "http://schemas.android.com/apk/res/ Android "android:oneshot=" false "> <item android:drawable=" @drawable/sf_progress_1 "Android:dura tion= "/> <item android:drawable=" @drawable/sf_progress_2 "android:duration="/> <it Em android:drawable= "@drawable/sf_progress_3" android:duration= "/> <item android:drawabl E= "@drawable/sf_progress_4" android:duration= "/> <item android:drawable=" @drawable/sf_progress_ 5 "android:duration="/> <item android:drawable= "@drawable/sf_progress_6" Android:duratio        n= "/> <item android:drawable=" @drawable/sf_progress_7 "android:duration="/> <item android:drawable= "@drawable/sf_progress_8" android:duration= "/></animation-list>
Where: Android:oneshot indicates that the animation plays only once on the last frame, and when set to false, it is played on behalf of the animation loop; otherwise, the animation plays only once.

The resource file can be downloaded at the following link:

http://download.csdn.net/detail/shenjichao2008/8248073


2. Create Style.xml in the values folder to customize the style of the progress dialog:

<style name= "Sf_dialogcustom" 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>    </style>    <style name= "Sf_pressdialogcustom" parent= "@style/sf_ Dialogcustom ">        <item name=" Android:windowbackground "> @android:color/transparent</item>         <item name= "Android:windownotitle" >true</item>    </style>

3. Create Sf_view_custom_progress_dialog.xml in the Layout folder to customize the layout of the progress DILAOG:

<?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:o rientation= "Horizontal" >    <imageview        android:id= "@+id/sf_iv_progress_dialog_loading"        android: Layout_width= "Wrap_content"        android:layout_height= "wrap_content"        android:background= "@anim/sf_progress _dialog_anim "        android:contentdescription=" @string/sf_progress_dialog_image_loading "/>    <TextView        android:id= "@+id/sf_tv_progress_dialog_loading"        style= "@style/sf_mediumlightgreytxt"        android: Layout_width= "Wrap_content"        android:layout_height= "wrap_content"        android:layout_gravity= "Center_ Vertical "/></linearlayout>

4. Create the Sfprogrssdialog class, inherit the dialog, as follows:

Package Com.snapfish.view;import Com.snapfish.r;import Android.app.dialog;import android.content.context;import Android.graphics.drawable.animationdrawable;import Android.text.textutils;import Android.view.Gravity;import Android.widget.imageview;import Android.widget.textview;public class Sfprogrssdialog extends Dialog {private static Sfprogrssdialog m_progrssdialog;private Sfprogrssdialog (context context, int theme) {Super (context, theme);} public static Sfprogrssdialog Createprogrssdialog (context context) {M_progrssdialog = new Sfprogrssdialog (context, R.style.sf_pressdialogcustom); M_progrssdialog.setcontentview (r.layout.sf_view_custom_progress_dialog); m_ Progrssdialog.getwindow (). GetAttributes (). Gravity = Gravity.center;return m_progrssdialog;} @Overridepublic void Onwindowfocuschanged (Boolean hasfocus) {if (null = = M_progrssdialog) return;imageview Loadingimageview = (ImageView) M_progrssdialog.findviewbyid (r.id.sf_iv_progress_dialog_loading); Animationdrawable animationdrawable = (Animationdrawable) Loadingimageview.getbackground (); Animationdrawable.start ();} Public Sfprogrssdialog setmessage (String msg) {TextView Loadingtextview = (TextView) M_progrssdialog.findviewbyid ( r.id.sf_tv_progress_dialog_loading); if (! Textutils.isempty (msg)) Loadingtextview.settext (msg); Elseloadingtextview.settext (R.string.sf_progress_dialog_ image_loading); return m_progrssdialog;}}

5. Write a method to show/Hide Progress Dialog:

Private Sfprogrssdialog m_customprogrssdialog;final void Showcustomprogrssdialog (String msg) {if (null = = M_ Customprogrssdialog) M_customprogrssdialog = Sfprogrssdialog.createprogrssdialog (m_parent); if (null! = M_ Customprogrssdialog) {m_customprogrssdialog.setmessage (msg); M_customprogrssdialog.show (); m_ Customprogrssdialog.setcancelable (FALSE);}} final void Hidecustomprogressdialog () {if (null! = M_customprogrssdialog) {M_customprogrssdialog.dismiss (); m_ Customprogrssdialog = null;}}

6. Before the network request, call the Showcustomprogrssdialog method, pass in the displayed message, and after the network response, call the Hideprogressdialog method to eliminate the progress dialog.


Android ProgressDialog Control Customization

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.