Android dialog box popup position and transparency settings

Source: Internet
Author: User

In Android we often use Alertdialog to display dialog boxes. Through this dialog box is displayed in the center of the screen. In some programs, however, the Requirement dialog box can be displayed in a different location. For example, above or below the screen. To achieve this effect. You need to get the Window object for the dialog box, there are several ways to get the Window object. The easiest thing to do is to get the window object directly through the GetWindow method of the Alertdialog class.

    New Alertdialog.builder (this). Settitle ("title")                           . Setmessage ("  message"). Create ();       = Alertdialog.getwindow ();         Window.setgravity (gravity.top);    // window.setgravity (gravity.bottom);       Alertdialog.show ();       

Transparent dialog boxThe dialog box shown by default is opaque, but we can make it transparent or translucent by setting the Alpha value of the dialog box. We all know that. The colors consist of R (Red), G (green), B (blue). In addition, there will be a (transparency, Alpha) to describe the color. In the description of the color, if the value is 0 for full transparency, if the value is 255, the opacity is indicated. You can also set the transparency of a dialog box by setting the Alpha property of Windows. But the alpha value range is from 0 to 1.0. If the value of this property is 0, it is fully transparent, and if the value is 1.0, it is opaque (that is, the dialog box is displayed normally). The following code, by setting the value of alpha to 0.3, displays transparent dialog boxes and opaque dialogs more clearly. In this example, a background image is added, and two dialog boxes are displayed (one is translucent and the other is opaque).
    //Show Transparent dialog boxAlertdialog Alertdialog =NewAlertdialog.builder ( This). Setmessage ("Transparent dialog box"). Setpositivebutton ("Determine",NULL). Create (); Window Window=Alertdialog.getwindow (); Windowmanager.layoutparams LP=window.getattributes (); //set transparency to 0.3Lp.alpha =0.6f;           Window.setattributes (LP);     Alertdialog.show (); 

When we use some apps, we find that when a dialog box or some modal window pops up, the content behind it becomes blurry or unclear. In fact, these effects can also be easily implemented in OPhone. To implement this function, we only need to set the two flags of the Wndow object, the code is as follows:

    Window.setflags (WindowManager.LayoutParams.FLAG_BLUR_BEHIND,        WindowManager.LayoutParams.FLAG_BLUR_BEHIND);  

Android dialog box popup position and transparency settings

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.