Android Alertdialog Implementation Sharing dialog box/exit Dialog/download dialog box _android

Source: Internet
Author: User

I. Summary
The window is usually used to prompt the user for some sort of action, for example: Click the Share button, window Sharing dialog box, double-click the Return button, window exit dialog box, download files, prompts download dialog box, and so on, Sharing Dialog/Exit Dialog/Download dialog box, can be directly used Alertdialog implementation, A similar effect is shown below:

Two. Alertdialog Basic knowledge
Alertdialog can't get the object directly through the new keyword, invoke the method: New AlertDialog.Builder.create () to get the Alertdialog object, this time it is easy to wonder: How to set the properties of the dialog box? For example: dialog box title, dialog box message, dialog box button, etc.

Two ways to Set dialog box properties

The first: Set the Alertdialog object properties, the specific code is as follows:

private void ShowDialog () { 
    Alertdialog mdialog = null; 
    Mdialog = new Alertdialog.builder (this). Create ();; 
     
    Mdialog.seticon (r.drawable.ic_launcher); 
    Mdialog.settitle ("system hint"); 
    Mdialog.setmessage ("Are you sure you want to quit?") "); 
    Mdialog.setbutton (dialoginterface.button_positive, OK, new Dialoginterface.onclicklistener () { 
       
      @Override Public 
      void OnClick (dialoginterface dialog, int which) { 
         
        finishmyself (); 
      } 
       
    }); 
    Mdialog.setbutton (Dialoginterface.button_negative, "Cancel", new Dialoginterface.onclicklistener () { 
       
      @Override Public 
      void OnClick (dialoginterface dialog, int which) { 
        toast.maketext (mainactivity.this, "Click Exit Program Again", (int ) touchtime) 
        . Show (); 
         
      } 
    }; 
     
    Mdialog.show (); 
     
  } 

The second type: Set the Builder object properties, the specific code is as follows:

private void ShowDialog () { 
    Alertdialog mdialog = null; 
    Builder Mbuilder = new Alertdialog.builder (this); 
     
    Mbuilder.seticon (r.drawable.ic_launcher); 
    Mbuilder.settitle ("system hint"); 
    Mbuilder.setmessage ("Are you sure you want to quit?") "); 
    Mbuilder.setpositivebutton (OK), new Dialoginterface.onclicklistener () { 
 
      @Override public 
      void OnClick ( Dialoginterface dialog, int which) { 
 
        finish (); 
      } 
 
    }); 
    Mbuilder.setnegativebutton ("Cancel", new Dialoginterface.onclicklistener () { 
 
      @Override public 
      void OnClick ( Dialoginterface dialog, int which) { 
        toast.maketext (mainactivity.this, "Exit program Again", (int) touchtime). Show 
            () ; 
 
      } 
    }); 
 
    Mdialog = Mbuilder.create ()///Create Alertdialog Object 
 
    mdialog.show ();//Display created Alertdialog 
 
  } 

The dialog box in both of these ways shows the default properties--the dialog box is centered vertically, with a short distance between the dialog box and the left and right form, and the effect is as follows:

How do I modify the default dialog box properties?

How to modify the default properties of the Alertdialog dialog box, and then implement the dialog box content width is covered with the screen, highly based on content adaptive, similar to the beginning of the article click on the Share button, pop-up window from the bottom of the effect. First create the Alertdialog dialog box, and then customize the Layout view of the dialog box, and finally set the Window object properties.

Three ways to set Window object screen width/height

The first way: SetLayout ()

After you get the Window object, set the layout parameters for the window object, that is, call the setlayout (int width,int height) method, Width Value: android.view.windowmanager.layoutparams.match_parent/android.view.windowmanager.layoutparams.wrap_ CONTENT, same height value: android.view.windowmanager.layoutparams.match_parent/ Android.view.WindowManager.LayoutParams.WRAP_CONTENT, the specific code is as follows:

View view = Getlayoutinflater (). Inflate (r.layout.popup_dialog, null); 
Alertdialog Mdialog = new Alertdialog.builder (this). Create (); 
    Mdialog.show (),//Displays the created Alertdialog, and displays that must be placed before the Window settings property 
 
/** 
 * Set mdialog window properties: match_parent/wrap_content 
 * * * * 
/Window window =mdialog.getwindow (); 
    Window.setgravity (Gravity.bottom); Here you can set the location of the dialog display 
    window.setlayout (Android.view.WindowManager.LayoutParams.MATCH_PARENT, 
        Android.view.WindowManager.LayoutParams.WRAP_CONTENT); 

The second way: SetAttributes ()

After you get the Window object, set the property value for the Window object, that is, call the SetAttributes (Layoutparams) method. Layoutparams's width variable value: android.view.windowmanager.layoutparams.match_parent/ Android.view.WindowManager.LayoutParams.WRAP_CONTENT, Same height variable value: android.view.windowmanager.layoutparams.match_parent/android.view.windowmanager.layoutparams.wrap _content, the specific code is as follows:

View view = Getlayoutinflater (). Inflate (r.layout.popup_dialog, null); 
Alertdialog Mdialog = new Alertdialog.builder (this). Create (); 
    Mdialog.show ();//Displays the created Alertdialog and shows that the Window 
 
window =mdialog.getwindow () must be placed before the window settings property. 
     Window.setgravity (Gravity.bottom); Here you can set the location of the dialog display 
windowmanager.layoutparams mparams = Window.getattributes (); 
     Mparams.width = Android.view.WindowManager.LayoutParams.MATCH_PARENT; 
     Mparams.height = Android.view.WindowManager.LayoutParams.WRAP_CONTENT; 
     Window.setgravity (Gravity.bottom); Here you can set the location of the dialog display 
     window.setattributes (mparams); 

The Third Way: SetLayout ()

The specific code is as follows:

View view = Getlayoutinflater (). Inflate (r.layout.popup_dialog, null); 
Alertdialog Mdialog = new Alertdialog.builder (this). Create (); 
    Mdialog.show ();//Displays the created Alertdialog and shows that the Window 
 
window =mdialog.getwindow () must be placed before the window settings property. 
    Window.setgravity (Gravity.bottom); Here you can set the location of the dialog display 
WindowManager manager = Getwindowmanager (); 
     Display display = Manager.getdefaultdisplay (); 
     int width = display.getwidth ()//Get current screen width 
     int height = 300;//custom height value, such as: 300DP 
     window.setgravity ( Gravity.bottom); Here you can set the position of the dialog display 
     window.setlayout (width, height); 

Three. Window Animation basics
Android's basic animations include: Gradient animation/Translation animation/Zoom animation/Rotating animation/combination animation, click the "Share" button, window flick from the bottom window, click the window again to disappear, set the animation--translation animation, the code is as follows:

<?xml version= "1.0" encoding= "Utf-8"?> 
<!--enter_dialog_anim.xml, the spring window into the animation--> 
<translate Xmlns:android= "http://schemas.android.com/apk/res/android" 
  android:duration= " 
  android:fromydelta=" 100% "> 
 
</translate> 
<?xml version=" 1.0 "encoding=" Utf-8 "?> 
<!--exit_dialog_ Anim.xml, flick window exit animation--> 
<translate xmlns:android= "Http://schemas.android.com/apk/res/android" 
  android: duration= "Android:toydelta=" " 
  100%" > 
 
</translate> 

Add windows to the Style.xml file to enter and exit the animated types referenced separately, as follows:

<!--sharing function window animation--> 
<style name= "Popup_style" parent= "android:animation" > 
    <item name= "@ Android:windowenteranimation "> @anim/enter_dialog_anim</item> 
    <item name=" @android: Windowexitanimation "> @anim/exit_dialog_anim</item> 
</style> 

Call Setcontentview () in the Window property setting to specify the View object, and call Setwindowanimations () to specify the animation to add, as follows:

Window.setcontentview (view);//This step must be specified, or the window will not appear
Window.setwindowanimations (R.style.popup_style); Add animation
four. Custom Play window: mydialogactivity
Custom mydialogactivity Implementation Alertdialog the same function, click the "Share Button" pop-up window from the bottom, click the "Cancel" window news, the final effect and alertdialog to achieve the same effect of the window, the following figure:

Development steps:

1. Define layout popup_main.xml. Popup_main.xml definition of the final display of the window, you can place a number of platform sharing buttons, such as: micro-letter/microblogging/space/Everyone, the code is as follows:

<?xml version= "1.0" encoding= "Utf-8"?> <!--bottom window layout--> <linearlayout xmlns:android= "http:// Schemas.android.com/apk/res/android "android:layout_width=" match_parent "android:layout_height=" Match_parent "and roid:background= "@color/transparent" android:orientation= "vertical" > <linearlayout Android:layout_widt 
      H= "Match_parent" android:layout_height= "wrap_content" android:orientation= "Horizontal" > <TextView 
      Android:id= "@+id/share_weibo_tv" android:layout_width= "0DP" android:layout_height= "Wrap_content" android:layout_margin= "@dimen/share_padding" android:layout_weight= "1" android:gravity= "center_horizontal "Android:text=" @string/weibo "/> <textview android:id=" @+id/share_weixin_tv "Android:la Yout_width= "0DP" android:layout_height= "wrap_content" android:layout_margin= "@dimen/share_padding" droid:layout_weight= "1" AndroiD:gravity= "Center_horizontal" android:text= "@string/weixin"/> </LinearLayout> <linearlayout Android:layout_width= "Match_parent" android:layout_height= "wrap_content" android:orientation= "Horizontal" 
 
    ; <textview android:id= "@+id/share_kongjian_tv" android:layout_width= "0DP" android:layout_height= "WR" Ap_content "android:layout_margin=" @dimen/share_padding "android:layout_weight=" 1 "android:gravity=" C 
      Enter_horizontal "android:text=" @string/kongjian "/> <textview android:id=" @+id/share_qq_tv " Android:layout_width= "0DP" android:layout_height= "wrap_content" android:layout_margin= "@dimen/share_pad 
  Ding "android:layout_weight=" 1 "android:gravity=" Center_horizontal "android:text=" @string/qq "/> </LinearLayout> <button android:id= "@+id/cancel_btn" android:layout_width= "Match_parent" an Droid:layout_height= "Wrap_content" android:layout_margin= "@dimen/activity_vertical_margin" android:background= "@drawable/BTN_BG 
 "Android:text=" @string/cancel "/> </LinearLayout>

2. Define the theme style. The theme style definition is referenced in the android:theme= "" attribute of the label in the Androidmanifest.xml file in the Style.xml file, as follows:

<!--mydialogactivity custom threme--> <style name= "Theme.customdialog" 
@android: parent= Theme.dialog "> 
    <item name=" Android:windownotitle ">true</item> 
    <!--set title--> 
    <item name= "Android:windowbackground" > @android:color/transparent</item> 
    <item name= "Android: Windowframe "> @null </item> 
    <!--setting border--> 
    <item name=" Android:windowistranslucent "> True</item> 
    <!--set semitransparent--> 
    <item name= "Android:windowfullscreen" >true</item> 
    <!--set Full-screen--> 
</style> 
<activity android:name= "mydialogactivity android:theme=" @ Style/theme.customdialog "/> 

3. Realize mydialogactivity specific function.

Package cn.teachcourse.main; 
Import Android.annotation.SuppressLint; 
Import android.app.Activity; 
Import Android.graphics.Bitmap; 
Import Android.graphics.BitmapFactory; 
Import android.graphics.drawable.BitmapDrawable; 
Import android.graphics.drawable.Drawable; 
Import Android.os.Bundle; 
Import android.view.Gravity; 
Import Android.view.View; 
Import Android.view.View.OnClickListener; 
Import Android.view.Window; 
Import Android.widget.Button; 
 
Import Android.widget.TextView; /* @author postmaster@teachcourse.cn @date created in: 2016-4-14 * * public class Mydialogactivity extends activity implemen 
 
  TS Onclicklistener {private view view; @Override protected void OnCreate (Bundle savedinstancestate) {//TODO auto-generated method stub Super.oncre 
    Ate (savedinstancestate); 
    view = Getlayoutinflater (). Inflate (R.layout.popup_main, NULL, FALSE); 
    Setcontentview (view); 
  Initview (); 
private void Initview () {window window = GetWindow ();    Window.setlayout (Android.view.ViewGroup.LayoutParams.MATCH_PARENT, Android.view.ViewGroup.LayoutParams.WRAP_C 
    Ontent); 
    Window.setgravity (Gravity.bottom); Window.setwindowanimations (R.style.popup_style); 
    Add animation TextView Weibo_tv = (TextView) View.findviewbyid (R.ID.SHARE_WEIBO_TV); 
    TextView WEIXIN_TV = (TextView) View.findviewbyid (R.ID.SHARE_WEIXIN_TV); 
    TextView QQ_TV = (TextView) View.findviewbyid (R.ID.SHARE_QQ_TV); 
    TextView KONGJIAN_TV = (TextView) view. Findviewbyid (R.ID.SHARE_KONGJIAN_TV); 
    Button cancel_btn = (button) View.findviewbyid (R.ID.CANCEL_BTN); 
    Add control Event Weibo_tv.setonclicklistener (this); 
    Weixin_tv.setonclicklistener (this); 
    Qq_tv.setonclicklistener (this); 
    Kongjian_tv.setonclicklistener (this); 
     
    Cancel_btn.setonclicklistener (this); 
    Resize the picture/position Abjustdrawablepos (WEIBO_TV, R.drawable.share_weibo); 
    Abjustdrawablepos (WEIXIN_TV, r.drawable.share_weixin); AbjustdrawablEPos (KONGJIAN_TV, R.drawable.share_kongjian); 
 
  Abjustdrawablepos (QQ_TV, R.DRAWABLE.SHARE_QQ); /** * Add icon and adjust position * * @param TV * @param draw/@SuppressLint ("Resourceascolor") Private V 
    OID Abjustdrawablepos (TextView TV, int draw) {Bitmap Mbitmap = Bitmapfactory.decoderesource (Getresources (), draw); 
    Mbitmap = Centersquarescalebitmap (Mbitmap, 250); 
    drawable drawable = new bitmapdrawable (MBITMAP); 
    Drawable.setbounds (0, 48, 0, 48);//Set the border of the picture Tv.settextcolor (R.color.fontcolor); 
    Tv.setcompounddrawables (NULL, drawable, NULL, NULL),//Setcompounddrawables () and SetBounds () method to add TextView icon with// 
    Tv.setcompounddrawableswithintrinsicbounds (NULL, drawable, NULL, NULL); 
      Tv.setcompounddrawablepadding (10);//Set the spacing between picture and text if (Mbitmap!= null) {mbitmap = null; 
    drawable = null; }/** * * @param bitmap * Original * @param edgelength * The side length of the desired square part * @return ShrinkPlace the bitmap after the median part of the intercept. */public static Bitmap Centersquarescalebitmap (Bitmap Bitmap, int edgelength) {if (null = = Bitmap | | edgelength 
    <= 0) {return null; 
    } Bitmap result = Bitmap; 
    int widthorg = Bitmap.getwidth (); 
 
    int heightorg = Bitmap.getheight (); if (widthorg >= edgelength && heightorg >= edgelength) {//compressed to a minimum length edgelength int lo 
 
      Ngeredge = (int) (Edgelength * Math.max (widthorg, heightorg)/Math. Min (widthorg, heightorg)); int scaledwidth = widthorg > heightorg? 
      Longeredge:edgelength; int scaledheight = widthorg > heightorg? 
      Edgelength:longeredge; 
 
      Bitmap Scaledbitmap; 
      try {scaledbitmap = Bitmap.createscaledbitmap (Bitmap, Scaledwidth, Scaledheight, true); 
      catch (Exception e) {return null; 
      ///To intercept the square part of the middle from the diagram. 
      int xtopleft = (scaledwidth-edgelength)/2; int ytopleft = (scaledHeight-edgelength)/2; 
        try {result = Bitmap.createbitmap (Scaledbitmap, Xtopleft, Ytopleft, Edgelength, edgelength); 
      Scaledbitmap.recycle (); 
      catch (Exception e) {return null; 
  } return result; @Override public void OnClick (View v) {switch (V.getid ()) {/** * Click share icon, pop-up sharing interface * * 
    ASE R.id.share_to_btn:break; 
    Case R.id.share_weibo_tv:break; 
    Case R.id.share_weixin_tv:break; 
    Case R.id.share_qq_tv:break; 
    Case R.id.share_kongjian_tv:break; 
       
      Case R.id.cancel_btn:finish (); 
 
    Break 
    Default:break;  } 
 
  } 
}

4. pop-up window, call StartActivity (This,mydialogactivity.class).

This is the entire content of this article, I hope to learn more about Android software programming help.

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.