Example code for the custom dialog box (Dialog) in Android _android

Source: Internet
Author: User
Tags transparent color
1. Modify the System default dialog style (style, theme)

2. custom dialog Layout file

3. You can encapsulate a class, inherit from dialog or directly using the dialog class to achieve, in order to facilitate re-use later, we recommend that you encapsulate a dialog class


First step:

We know that Android defines a control or view style by defining its style, looking at the theme files in the Android framework, the path in the source code:/frameworks/base/core/res/res/values/ Themes.xml, we can see that Android defines a style for dialog,
Copy Code code as follows:

<style name= "Theme.dialog" >
<item name= "Android:windowframe" > @null </item>
<item name= "Android:windowtitlestyle" > @android:style/dialogwindowtitle</item>
<item name= "Android:windowbackground" > @android:d rawable/panel_background</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>

We can see the dialog style defined in Themes.xml, which defines the window's heading style, window's background, whether it's suspended, and so on.

So, to create a dialog with a custom style, we can create a styles.xml that defines our own dialog style, inherits from the Theme.dialog style, and modifies some of its properties.

Define our own dialog style:

A. Create a styles.xml file, put it under the Res/values folder (of course, it goes without saying ...) Wordy, please.

B. Define the dialog style in Styles.xml with the following code:
Copy Code code as follows:

<style name= "Theme_dialog" parent= "@android: Style/theme.dialog" >
<item name= "Android:windowbackground" > @android:color/transparent</item>
<item name= "Android:windownotitle" >true</item>
</style>

In the code above, a style theme_dialog is defined, inherits from @android:style/theme.dialog, and then defines the background of the dialog window where the transparent color is used. 00000000 and then defines the window hidden caption where dialog is located (the System-defined dialog style is titled, set this property to True to hide the caption), and the custom dialog style is complete.

Step Two:

Define the layout of the dialog:

Create a layout file Layout_dialog.xml with the following code:
Copy Code code as follows:

<?xml version= "1.0" encoding= "Utf-8"?>
<linearlayout
Xmlns:android= "Http://schemas.android.com/apk/res/android"
android:orientation= "Vertical"
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent"
android:background= "@drawable/pp_bg_dialog"
android:gravity= "Center" >

<progressbar android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
style= "@style/progressbar_normal"/>

<textview android:layout_width= "wrap_content" android:layout_height= "Wrap_content"
style= "@style/text_white_small" android:layout_margintop= "5DP"
Android:text= "is deleting ..." android:id= "@+id/message"/>
</LinearLayout>

A linear layout with a vertical orientation is used, and all child elements are centered, the child element is a progress bar ProgressBar and a textview.

Here, ProgressBar takes a custom style and uses the default ProgressBar of the system to achieve the same effect (similar). The background of LinearLayout

android:background= "@drawable/pp_bg_dialog" (that is, the black transparent background box centered in the above effect picture) is a custom picture resource shape:
Copy Code code as follows:

<?xml version= "1.0" encoding= "Utf-8"?>
<shape
Xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:shape= "Rectangle" >
<corners android:radius= "10DP"/>
<solid android:color= "#55000000"/>

</shape>

A rectangle is defined in the code and the rounded corners and colors are set. By this, the layout of the dialog is complete.
Step Three:
Custom Customdialog class, inherited from dialog, code as follows:
Copy Code code as follows:

public class Customdialog extends Dialog {2
private static int default_width = 160; Default width
private static int default_height = 120;//default height
Public Customdialog (context, int layout, int style) {
This is (context, default_width, default_height, layout, style);
}

Public Customdialog (context context, int width, int height, int layout, int style) {
Super (context, style); 12
Set content
Setcontentview (layout);
Set window params
window window = GetWindow ();
Windowmanager.layoutparams params = Window.getattributes ();
Set width,height by density and gravity
float density = getdensity (context);
Params.width = (int) (width*density);
Params.height = (int) (height*density);
params.gravity = Gravity.center;
Window.setattributes (params);
}
Private float getdensity (context context) {
Resources of resources = context.getresources ();
Displaymetrics DM = Resources.getdisplaymetrics ();
return dm.density;
}
}

The dialog contentview is set in the construction method, and the width, height, and center display of the window is set.

Customdialog use is as follows:
Copy Code code as follows:

public class Customdialogdemoactivity extends activity {
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (r.layout.test);
Customdialog dialog1 = new Customdialog (this, R.layout.common_dialog, r.style.theme_dialog);//dialog Use default size (160)
Customdialog dialog2 = new Customdialog (this, 180, 180, R.layout.common_dialog, R.style.theme_dialog);
Dialog2.show ()//Display Dialog
If you want to modify a view in dialog, such as "deleting ..." instead of "Loading ..."
TextView mmessage = (TextView) Dialog2.findviewbyid (r.id.message);
Mmessage.settext ("Load ...");
}
}


The general process is like this, according to the above we can free play it, hoping to design their own satisfaction with the dialog.
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.