Implementation of custom dialog in Android Development (source code sharing)

Source: Internet
Author: User

If the system's built-in dialog cannot meet our daily development needs, we must build our own custom dialog. Especially for an app, a unified style will give us a comfortable feeling, therefore, the style and tone of the dialog are generally consistent with the app topic. This blog mainly introduces two ways to customize the dialog.

MainActivity code

Package com. example. e01_consumerdialog; import android. OS. bundle; import android. app. activity; import android. app. alertDialog; import android. app. alertDialog. builder; import android. content. dialogInterface; import android. content. dialogInterface. onClickListener; import android. view. layoutInflater; import android. view. menu; import android. view. view; import android. widget. button; public class MainActivity extends Activity {private Button button; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); button = (Button) this. findViewById (R. id. button1); button. setOnClickListener (new View. onClickListener () {@ Overridepublic void onClick (View arg0) {// TODO Auto-generated method stub AlertDialog. builder builder = new Builder (MainActivity. this); builder. setTitle ("Logon interface"); View view = LayoutInflater. from (MainActivity. this ). inflate (R. layout. dialog, null); builder. setView (view); // set the custom Layout view builder. setPositiveButton ("OK", new OnClickListener () {@ Overridepublic void onClick (DialogInterface arg0, int arg1) {// TODO Auto-generated method stub}); builder. setNegativeButton ("cancel", new OnClickListener () {@ Overridepublic void onClick (DialogInterface arg0, int arg1) {// TODO Auto-generated method stub}); builder. show () ;}}) ;}@ Override public boolean onCreateOptionsMenu (Menu menu) {// Inflate the menu; this adds items to the action bar if it is present. getMenuInflater (). inflate (R. menu. main, menu); return true ;}}

Layout file of dialog. xml

<? Xml version = "1.0" encoding = "UTF-8"?> <RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: layout_width = "match_parent" android: layout_height = "match_parent"> <TextView android: id = "@ + id/textView2" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_below = "@ + id/editText1" android: layout_marginTop = "27dp" android: layout_toLeftOf = "@ + id/editText1" android: text = "Password:"/> <EditText android: id = "@ + id/editText2" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_alignBaseline = "@ + id/textView2" android: layout_alignBottom = "@ + id/textView2" android: layout_alignLeft = "@ + id/editText1" android: layout_alignRight = "@ + id/editText1" android: EMS = "10" android: inputType = "textPassword"/> <EditText android: id = "@ + id/editText1" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_alignBaseline = "@ + id/textView1" android: layout_alignBottom = "@ + id/textView1" android: layout_toRightOf = "@ + id/textView1" android: EMS = "10"/> <TextView android: id = "@ + id/textView1" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_alignParentLeft = "true" android: layout_alignParentTop = "true" android: text = "username:"/> </RelativeLayout>

The preceding custom dialog simulates the function of implementing a login box option, but its disadvantages are also obvious. The buttons must use the PositiveButton style of dialog, is there any custom method that makes our permissions more powerful? We can see from the official API

Tip:If you want a custom dialog, you can instead displayActivityAs a dialog instead of usingDialogAPIs. Simply create an activity and set its themeTheme.Holo.DialogIn<activity>Manifest element:

<activity android:theme="@android:style/Theme.Holo.Dialog" >

That's it. The activity now displays in a dialog window instead of fullscreen.

The above tip tells us that if you want to customize the dialog, you can also add the dialog attribute to your own activity in the list file to achieve the effect of the dialog form!

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.