Customize title for Android dialog box

Source: Internet
Author: User

Android comes with a dialog title that doesn't look good, and if we need to set a caption for a popup dialog box, you can use the Alertdialog.builder setcustomtitle () method.

Defines a title.xml file for a dialog box title:

<?XML version= "1.0" encoding= "UTF-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:background= "@color/white"android:gravity= "Center_vertical"android:orientation= "vertical" >        <LinearLayoutAndroid:id= "@+id/patient_top"Android:layout_width= "Match_parent"Android:layout_height= "50DP"Android:layout_alignparenttop= "true"Android:background= "@color/green"android:gravity= "Center_vertical|center_horizontal"android:orientation= "vertical" >        <TextViewAndroid:id= "@+id/txtpatient"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Select City"Android:textcolor= "@color/white"android:textsize= "20SP" />            </LinearLayout></LinearLayout>

Mainactivity Layout file:

<LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"android:gravity= "Center_vertical|center_horizontal"android:orientation= "vertical"Android:paddingbottom= "@dimen/activity_vertical_margin"Android:paddingleft= "@dimen/activity_horizontal_margin"Android:paddingright= "@dimen/activity_horizontal_margin"Android:paddingtop= "@dimen/activity_vertical_margin"Tools:context= "Com.hzhi.dialogtest.MainActivity" >    <ButtonAndroid:id= "@+id/btn01"Android:layout_width= "150DP"Android:layout_height= "Wrap_content"Android:text= "Select City 1" />    <ButtonAndroid:id= "@+id/btn02"Android:layout_width= "150DP"Android:layout_height= "Wrap_content"Android:text= "Select City 2" /></LinearLayout>

Mainactivity.java file:

 Packagecom.hzhi.dialogtest;Importandroid.support.v7.app.ActionBarActivity;ImportAndroid.app.AlertDialog;ImportAndroid.content.DialogInterface;ImportAndroid.os.Bundle;ImportAndroid.view.LayoutInflater;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener;ImportAndroid.widget.Button; Public classMainactivityextendsActionbaractivityImplementsonclicklistener{Finalstring[] Cities =NewString[6];    Button button_01, button_02; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main);    Initview (); }        Private voidInitview () {cities[0] = "Beijing"; cities[1] = "Shanghai"; cities[2] = "Shenzhen"; cities[3] = "Guangzhou"; cities[4] = "Hangzhou"; cities[5] = "Chengdu"; Button_01=(Button) Findviewbyid (R.ID.BTN01); Button_01.setonclicklistener ( This); Button_02=(Button) Findviewbyid (R.ID.BTN02); Button_02.setonclicklistener ( This); } @Override Public voidOnClick (View v) {//TODO auto-generated Method Stub        Switch(V.getid ()) { CaseR.id.btn01:alertdialog.builder Builder1=NewAlertdialog.builder (mainactivity. This); Builder1.setitems (Cities,NewDialoginterface.onclicklistener () {@Override Public voidOnClick (Dialoginterface Dialog,intwhich)                        {                }            }); Builder1.settitle ("Select City");            Builder1.show ();  Break;  CaseR.id.btn02:layoutinflater Layoutinflater= Layoutinflater.from (mainactivity. This); View Mtitleview= Layoutinflater.inflate (R.layout.title,NULL);Alertdialog.builder Builder2=NewAlertdialog.builder (mainactivity. This); Builder2.setitems (Cities,NewDialoginterface.onclicklistener () {@Override Public voidOnClick (Dialoginterface Dialog,intwhich)            {                }            }); Builder2.setcustomtitle (Mtitleview);builder2.show ();  Break; }            }    }

The effect is as follows, on the left is click the first button, pop up the Android system comes with the dialog box (directly with Settitle () set the title); Right is click on the second button, first inflate a view, and then with Setcustomtitle () method to set the view as the title of the dialog box.

Customize title for Android dialog box

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.