Custom dialog with strong extensibility

Source: Internet
Author: User

In the project, many times will use the dialog box, Android Default dialog box is too ugly to meet our needs, below I introduce a custom Dilog

Let's take a look at the effect


You can see the three sections above, the Tip section, the Middle Content section, and the following two button sections.

I make this part controllable, the general "hint" this need also have not need, then can hide when need, can show when need


Then this part of the middle is the information you want to prompt, this layout is also set to a custom layout, this layout you can like me, as long as a textview, show the message can be, you can also add additional controls according to their needs, such as edittext, etc., is set in one layout just OK


The button section below is also a custom two button layout, placed in the drawable file Plus, this button you can manually modify his style, here I use one, and then how to modify


Okay next do the main part of the nonsense not much to say on the code

Package com.mydialog.widget;

Import Android.app.AlertDialog;
Import Android.content.Context;
Import Android.content.DialogInterface;
Import Android.graphics.Color;
Import Android.util.TypedValue;
Import android.view.*;
Import Android.widget.LinearLayout;
Import Android.widget.LinearLayout.LayoutParams;
Import Android.widget.TextView;
Import COM.MYDIAOLG.ACTIVITY.R;


/**
* Custom Dialog
*/
public class Customdialog {
Private context context;
Private Alertdialog AD;
Private TextView title;
Private LinearLayout titlelayout;
Private LinearLayout contentlayout;
Private LinearLayout buttonlayout;

Public Customdialog (Context context) {
This.context = context;
AD = new Alertdialog.builder (context). Create ();
Ad.setcancelable (FALSE);
Ad.show ();
window window = Ad.getwindow ();
Window.setcontentview (R.layout.custom_dialog);
Set the width of the dialog
Windowmanager.layoutparams LP = Window.getattributes ();

       //get screen width, high use
        Display d = ((Windo Wmanager) Context.getsystemservice (Context.window_service)). Getdefaultdisplay ();  
        Lp.width = (int) (D.getwidth () * 0.85);
        WINDOW.SETATTRIBUTES (LP);
       //resolve Alertdialog issues with input method
        Window.clearflags ( WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);

       //Initialize control
        title = (TextView) Window.findviewbyid (r.id.re Minder_title);
        titlelayout = (linearlayout) Window.findviewbyid (r.id.dialog_title);
        contentlayout = (linearlayout) Window.findviewbyid (r.id.dialog_content);
        buttonlayout = (linearlayout) Window.findviewbyid (R.ID.DIALOG_BTN);
   }


/**
* Set Title ID
*/

public void Settitlename (int resId) {
Title.settext (RESID);
}

/**
* Set Title Content
* @param titlename
*/
public void Settitlename (String titlename) {
Title.settext (TitleName);
}

/** Get titile Layout view*/

Public TextView Gettitleview () {
return title;
}

/**
* Show or hide the head
*
* @param isshow If the head is displayed
*/
public void Showtitle (Boolean isshow) {
if (isshow) {
Titlelayout.setvisibility (view.visible);
} else {
Titlelayout.setvisibility (View.gone);
}
}

/**
* Set Content
*
* @param layoutid Layout ID
*/
public void Setcontentview (int layoutid, layoutparams params) {
Layoutinflater inflaterdl = layoutinflater.from (context);
View view = Inflaterdl.inflate (LayoutID, NULL);
Contentlayout.addview (view, params);
Contentlayout.setvisibility (view.visible);
}

/**
* Set Content
*
* @param view
* @param params layout parameters
*/
public void Setcontentview (view view, Layoutparams params) {
Contentlayout.removeallviews ();
Contentlayout.addview (view, params);
Contentlayout.setvisibility (view.visible);
}

Public View Getcontentview () {
return contentlayout;
}

/**
* Set button
*
* Text @param text button display
* @param resId button background picture layout
* @param listener button click event
*/
public void Setbutton (String text, int resId, final View.onclicklistener listener) {
TextView button = new TextView (context);
Layoutparams params = new Layoutparams (layoutparams.match_parent,

Layoutparams.wrap_content, 1);
Button.setlayoutparams (params);
Button.setgravity (Gravity.center);
Button.setbackgroundresource (RESID);
Button.settext (text);
Button.settextcolor (Color.White);
Button.settextsize (TYPEDVALUE.COMPLEX_UNIT_SP, 16);
Button.setonclicklistener (listener);
Params.setmargins (context, dip2px), 0, dip2px (context, 15), 0);
Button.setlayoutparams (params);
Buttonlayout.addview (button);
Buttonlayout.setvisibility (view.visible);
}

/**
* Close dialog box
*/
public void Dismiss () {
Ad.dismiss ();
}

/**
* Display dialog box
*/
public void Show () {
Ad.show ();
}

/**
* Set Whether you can turn off
*
* @param cancelable True or False
*/
public void Setcancelable (Boolean cancelable) {
Ad.setcancelable (cancelable);
}

/**
* Set Whether you can click Dialog outside to close dialog
*
* @param canceledontouchoutside True or False
*/
public void Setcanceledontouchoutside (Boolean canceledontouchoutside) {
Ad.setcanceledontouchoutside (Canceledontouchoutside);
}
public static int dip2px (context context, float Dpvalue) {
Final float scale = context.getresources (). Getdisplaymetrics (). density;
return (int) (Dpvalue * scale + 0.5f);
}
/**
* Set Dialog Vanishing Event
*
* @param Ondismisslistener Dialog Vanishing Event
*/
public void Setondismisslistener (Dialoginterface.ondismisslistener ondismisslistener) {
Ad.setondismisslistener (Ondismisslistener);
}
}

Here's a look at the Custom_dialog.xml layout file

<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
android:orientation= "Vertical"
android:background= "@drawable/bg_dialog"
Android:paddingbottom= "15DP"
android:paddingtop= "15DP" >

<linearlayout
Android:id= "@+id/dialog_title"
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
Android:layout_marginbottom= "15DP"
android:orientation= "Vertical"
android:paddingleft= "15DP"
android:paddingright= "15DP" >

<textview
Android:id= "@+id/reminder_title"
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
Android:layout_marginbottom= "10DP"
Android:textcolor= "#ff323232"
Android:textsize= "22SP"/>

<imageview
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
android:src= "@drawable/bg_public_line"
android:contentdescription= "@null"
Android:scaletype= "Fitxy"/>
</LinearLayout>

<linearlayout
Android:id= "@+id/dialog_content"
Android:layout_width= "Match_parent"
android:layout_height= "0DP"
android:layout_weight= "1"
android:orientation= "Horizontal"
android:paddingleft= "15DP"
android:paddingright= "15DP"
android:visibility= "Gone"/>


<linearlayout
Android:id= "@+id/dialog_btn"
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
android:orientation= "Horizontal"
android:layout_margintop= "15DP"
android:visibility= "Gone"/>
</LinearLayout>

The main thing is up there, let's call it, here I'll put the call process in a method

You can call it according to your needs.

/**
* Popup dialog box
*/
private void ShowDialog () {
Linearlayout.layoutparams params = new

        Linearlayout.layoutparams ( LinearLayout.LayoutParams.MATCH_PARENT,

        LinearLayout.LayoutParams.WRAP_CONTENT);
        Layoutinflater inflater = Layoutinflater.from (mainactivity.this);
        View Mdialogview = inflater.inflate (r.layout.dialog_public_content, NULL);
        TextView TextView = (TextView) Mdialogview.findviewbyid (R.id.my_content_text);
        Textview.settext ("Are you sure it's good to do xxx?");
        final Customdialog customdialog = new Customdialog (mainactivity.this);
        Customdialog.showtitle (true);
        customdialog.settitlename (r.string.pubilc_dialog_title);
        Customdialog.setcontentview (Mdialogview, params);
       //Cancel
        Customdialog.setbutton (getString (r.string.public_ Cancel),  

         r.drawable.btn_cancel_selector, new View.onclicklistener () {
            @Override
          &NBS P public void OnClick (View v) {
                Customdialog.dismiss ();
           }
       });
       //OK
        Customdialog.setbutton (getString (r.string.public_ OK),  

R.drawable.btn_ok_selector, New View.onclicklistener () {
@Override
public void OnClick (View v) {
Identify events where you can listen for things you want to identify, such as submitting data
Customdialog.dismiss ();
}
});
}


Here are three layouts Dialog_public_content.xml show you the content information of the hint, good there is put in drawable under the Btn_cancel_selector.xml, and Btn_ok_selector.xml, two buttons ,

Here's a look at Dialog_public_content.xml, the layout

<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
android:orientation= "Horizontal"
android:gravity= "Center_vertical" >


<textview
Android:id= "@+id/my_content_text"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
Android:textcolor= "#ff333333"
Android:textsize= "16sp"/>
</LinearLayout>

This textview is the first picture to see "Are you sure you want to do xxx things?" The corresponding TextView, in this layout you can modify the layout of the control according to your needs, and then call on OK

To do this for extensibility, for example I can set edittext in this layout, prompting the user to enter some information, etc.


So Btn_cancel_selector.xml, and Btn_ok_selector.xml, two buttons the layout is the same, just inside the picture is not the same

You can write as you want, you can just use one, okay, look at what's in Btn_ok_selector.xml.

<?xml version= "1.0" encoding= "Utf-8"?>
<selector xmlns:android= "Http://schemas.android.com/apk/res/android" >
<item android:drawable= "@drawable/bg_btn_ok_press" android:state_pressed= "true"/>
<item android:drawable= "@drawable/bg_btn_ok_normal"/>
</selector>


All right, you're ready to call.


This is a simple call process, you know how to use it OK,

Some of the resource file pictures and stuff in the Drawable-hdip folder,

Below I put the demo address to everyone, there is a need to download to see

Yes: http://download.csdn.net/detail/shaozucheng/8243089




Custom dialog with strong extensibility

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.