Common dialog boxes in Android

Source: Internet
Author: User

dialog boxes are a common operation in Android, and there are several common dialog boxes:


The following is an XML layout file:

<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools "android:layout_width=" match_parent "android:layout_height=" Match_parent "Android:orien tation= "Vertical" tools:context= ".        Mainactivity "> <button android:layout_width=" match_parent "android:layout_height=" Wrap_content "         android:onclick= "Click1" android:text= "Show notification dialog box"/> <button android:layout_width= "Match_parent" android:layout_height= "Wrap_content" android:onclick= "Click2" android:text= "Show Radio dialog box"/> <bu Tton android:layout_width= "match_parent" android:layout_height= "wrap_content" android:onclick= "Click3 "Android:text=" Displays the Multi-select dialog box "/> <button android:layout_width=" Match_parent "Android:layout_heigh t= "Wrap_content" android:onclick= "Click4" android:text= "Show Run progress dialog box"/> <button android:layout _width= "MaTch_parent "android:layout_height=" wrap_content "android:onclick=" Click5 "android:text=" Show Run progress bar dialog box "/ ></LinearLayout>

Implementation code:

Show dialog box

public void Click1 (view view) {Alertdialog.builder builder = new Builder (this); Builder.settitle ("Show dialog Box"); Builder.seticon (R.drawable.ic_launcher); Builder.setmessage ("Are you sure?" Builder.setpositivebutton ("OK", new Onclicklistener () {@Overridepublic void OnClick (dialoginterface dialog, int which) {Toast.maketext (mainactivity.this, "OK clicked", Toast.length_long). Show ();}); Builder.setnegativebutton ("Cancel", new Onclicklistener () {@Overridepublic void OnClick (dialoginterface dialog, int which) {//TODO auto-generated Method stub}}); Builder.show ();}

Show Effect:


Display a Radio dialog box

public void Click2 (view view) {Alertdialog.builder builder = new Builder (this); Builder.settitle ("Radio dialog Box"); final string[]  Items = new string[] {"Entry 1", "Entry 2", "Entry 3"};builder.setsinglechoiceitems (items, 1, new Onclicklistener () {@Overridepublic void OnClick (Dialoginterface dialog, int which) {//TODO auto-generated method Stubtoast.maketext (Mainactivity.this, ite Ms[which] + "selected", Toast.length_long). Show ();d Ialog.dismiss ();}); Builder.show ();}

Show Effect:


Show multi-Select dialog box

public void Click3 (view view) {Alertdialog.builder builder = new Builder (this); Builder.settitle ("Show multiple selection Dialogs"); final String [] items = new string[] {"Entry 1", "Entry 2", "Entry 3", "Entry 4"};builder.setmultichoiceitems (items, new boolean[] {true, FALSE, FAL Se,true}, new Onmultichoiceclicklistener () {@Overridepublic void OnClick (dialoginterface dialog, int which,boolean isChecked) {//TODO auto-generated method Stubtoast.maketext (Mainactivity.this, Items[which] + ischecked,toast.length_ LONG). Show ();}}); Builder.setnegativebutton ("Cancel", new Onclicklistener () {@Overridepublic void OnClick (dialoginterface dialog, int which) {//TODO auto-generated Method stub}}); Builder.show ();}

Show Effect:

Show Run Progress dialog box

public void Click4 (view view) {progressdialog dialog = new ProgressDialog (this);d Ialog.settitle ("Reminder"); Dialog.setmessage ("Uploading in");d ialog.show ();}

Show Effect:


Show run progress bar dialog box

public void Click5 (view view) {Final progressdialog dialog = new ProgressDialog (this);d Ialog.settitle ("Loading Progress"); Dialog.setprogressstyle (progressdialog.style_horizontal);d Ialog.setmax (+);d ialog.show (); new Thread () {public void Run () {for (int i = 0; i <; i++) {dialog.setprogress (i); try {thread.sleep ()} catch (Interruptedexception E {//TODO auto-generated catch Blocke.printstacktrace ();}} Dialog.dismiss ();};}. Start ();}

Show Effect:


The last thing to add is the assumption that you're defining dialog, assuming you're using

Dialog.setcontentview (r.layout.dialog_wait); Then there's no problem.

Let's say you created a view by Inflater.

Dialog.setcontentview (view)

This way to set the view words. The dialog size of the setting is not valid and the view fills the entire screen.

At this point, you need to set the size of the view in your code.

Windowmanager.layoutparams Params=dialog.getwindow (). GetAttributes ();        params.width=400;        params.height=400;        Dialog.getwindow (). SetAttributes (params);


Define your own dialog box displayed at the bottom

See a lot of apps when it comes to sharing third-party platforms. A dialog box appears at the bottom of the screen


This will require you to define the properties of the dialog and let dialog appear at the bottom of the screen.

    public static void Showsharedialog (final activity activity) {Final alertdialog dialog = new Alertdialog.builde        R (Activity). Create ();        Dialog.show ();        View view = View.inflate (activity, r.layout.view_dialog_share, NULL);        TextView tvweixinfriend = (TextView) View.findviewbyid (r.id.share_wexin);  Tvweixinfriend.setonclicklistener (New View.onclicklistener () {@Override public void OnClick (View v)                {Dialog.dismiss ();            Sharemanager.getinstance (). Sharetoweixinfriend (activity);        }        });        TextView tvweixincircle = (TextView) View.findviewbyid (R.ID.SHARE_PYQ);  Tvweixincircle.setonclicklistener (New View.onclicklistener () {@Override public void OnClick (View v)                {Dialog.dismiss ();            Sharemanager.getinstance (). Sharetoweixincircle (activity);        }        }); TextView Tvweibo = (TextView) View.findviewbyid (R.id.share_weibo);        Tvweibo.setonclicklistener (New View.onclicklistener () {@Override public void OnClick (View V                ) {Dialog.dismiss ();            Sharemanager.getinstance (). Sharetoweibo (activity);        }        });        Windowmanager.layoutparams LP = Dialog.getwindow (). GetAttributes ();        Lp.width = ViewGroup.LayoutParams.MATCH_PARENT;        lp.gravity = Gravity.bottom;        Dialog.getwindow (). SetAttributes (LP);            Dialog.setcontentview (view); }

Below are

View_dialog_share
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "Match_parent" android:layout_height= "Wrap_content" android:layout_alignparentbottom= "true" android:background= "@color/white" an droid:orientation= "Horizontal" android:paddingbottom= "16DP" android:paddingtop= "16DP" > <textview and Roid:id= "@+id/share_wexin" android:layout_width= "0DP" android:layout_height= "Wrap_content" Android:la        yout_weight= "1" android:drawablepadding= "4DP" android:drawabletop= "@drawable/icon_share_weixin_friend"        Android:gravity= "Center_horizontal" android:text= "Friends"/> <textview android:id= "@+id/share_pyq" Android:layout_width= "0DP" android:layout_height= "Wrap_content" android:layout_weight= "1" Androi d:drawablepadding= "4DP" android:drawabletop= "@drawable/icon_share_weixin_circle" android:gravity= "Center_hor Izontal "Android:text= "Friends Circle"/> <textview android:id= "@+id/share_weibo" android:layout_width= "0DP" Android:layout_ height= "Wrap_content" android:layout_weight= "1" android:drawablepadding= "4DP" android:drawabletop= "@d Rawable/icon_share_weibo "android:gravity=" Center_horizontal "android:text=" Weibo "/></linearlayout>





Common dialog boxes in Android

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.