Simple Android custom Dialog instance

Source: Internet
Author: User

Simple Android custom Dialog instance

In Android applications, the most indispensable thing is the custom Dialog. The default Dialog style provided by the system generally does not compose the style of our applications.
Custom Dialog requires three steps:
1. Mainly rewrite the Java class of Dialog
2. Customize the layout file and set the Dialog Theme. Add one to the style. xml file.
3. Usage

1. Create the CustomPopDialog2.java class

Import android. app. dialog; import android. content. context; import android. graphics. bitmap; import android. view. layoutInflater; import android. view. view; import android. view. windowManager. layoutParams; import android. widget. imageView;/*** the custom Dialog application displays the image in the center of the pop-up box and click other areas to automatically close the Dialog ** @ author SHANHY (365384722@QQ.COM) * @ date August 1, December 4, 2015 */public class CustomPopDialog2 extends Dialog {public CustomPopDialog2 (Context context) {super (context);} public CustomPopDialog2 (Context context, int theme) {super (context, theme);} public static class Builder {private Context context; private Bitmap image; public Builder (Context context) {this. context = context;} public Bitmap getImage () {return image;} public void setImage (Bitmap image) {this. image = image;} public CustomPopDialog2 create () {LayoutInflater inflater = (LayoutInflater) context. getSystemService (Context. LAYOUT_INFLATER_SERVICE); final CustomPopDialog2 dialog = new CustomPopDialog2 (context, R. style. dialog); View layout = inflater. inflate (R. layout. dialog_share_qrcode, null); dialog. addContentView (layout, new LayoutParams (android. view. viewGroup. layoutParams. WRAP_CONTENT, android. view. viewGroup. layoutParams. WRAP_CONTENT); dialog. setContentView (layout); ImageView img = (ImageView) layout. findViewById (R. id. img_qrcode); img. setImageBitmap (getImage (); return dialog ;}}}

Here is a simple description. We need to prepare a View layout file for custom Dialog, which focuses on the create () method. In this example, an image is directly displayed.

Ii. Customize the layout file of the View and add theme to style. xml


  
      
   
  

3. Use custom Dialog

Bitmap bitmap = xxxxx; // here is the Bitmap for obtaining images. You can also input other parameters to the Dialog custocustocustocustocustodidialog2.builder dialogBuild = new CustomPopDialog2.Builder (context); dialogBuild. setImage (bitmap); CustomPopDialog2 dialog = dialogBuild. create (); dialog. setCanceledOnTouchOutside (true); // click the external area to close the dialog. show ();

Finally:

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.