Custom dialog usages in Android programming _android

Source: Internet
Author: User

This example describes the custom dialog usage in Android programming. Share to everyone for your reference, specific as follows:

Dialog is a set of pop-up prompts in Android that works very well, but its style is a stereotype, and sometimes we need to define our own style.

1, the definition of a style file, this file inherits from Theme.dialog, in the Style.xml file to create a style of their own

<style name= "Addnotetype_error_dialog" parent= "@android: Theme.dialog" >
  <item name= "Android: Windowframe "> @null </item>
  <item name=" Android:windownotitle ">true</item>
  <item Name= "Android:windowbackground" > @color/color_shenhui</item>
  <item name= "Android: Windowisfloating ">true</item>
  <item name=" Android:windowcontentoverlay "> @null </item>
</style>

2, a new layout, as a pop-up box display

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android= "http://schemas.android.com/apk/res/"
 Android "Android:layout_width=" Wrap_content "android:layout_height=" wrap_content "android:orientation=" vertical " android:background= "#DFDFDF" > <textview android:layout_width= "fill_parent" android:layout_height= "50DP" Andr
  Oid:gravity= "center_vertical" android:layout_marginleft= "5DP" android:text= "@string/txt_addnotetype_error_title" Android:textcolor= "#00CCFF" android:textsize= "18sp"/> <view android:layout_width= "Fill_parent" android:l ayout_height= "1px" android:background= "#00CCFF"/> <textview android:layout_width= "Fill_parent" Android:lay out_height= "50DP" android:gravity= "Left|center" android:layout_margintop= "5DP" android:text= "@string/txt_
   Addnotetype_error_content_null "/> <view android:layout_width=" fill_parent "android:layout_height=" 1px " Android:background= "#BBB9BA" Android:layout_margintop= "5DP"/> <button android:id= "@+id/btn_add_note_addnotetype_error_ok" android:layout_width= "Fill_parent" android:layout_height= "50DP" android:text= "@string/txt_ok" android:background= "@null"/> </linearlayout

 >

3. Call this dialog

Here is a direct new dialog object, which is passed into the theme
Dialog = New dialog (Sel_notetypeactivity.this, R.style.addnotetype_error_ Dialog);
Set its Contentview
Dialog.setcontentview (r.layout.dialog_addnotetype_error);
Button BTN_ADD_NOTE_ADDNOTETYPE_ERROR_OK = (button) Dialog.findviewbyid (R.ID.BTN_ADD_NOTE_ADDNOTETYPE_ERROR_OK);
Btn_add_note_addnotetype_error_ok.setonclicklistener (New Addnotetypeerroclicklistener ());
Dialog.show ();

button click event:

Class Addnotetypeerroclicklistener implements onclicklistener{
  @Override public
  void OnClick (View v) {
   TODO auto-generated Method Stub
   dialog.cancel ();
  }


Sometimes we want to set the width or height of the dialog, which is relatively simple, add the following code directly under Dialog.show ():

Windowmanager.layoutparams layoutparams = Dialog.getwindow (). GetAttributes ();
Layoutparams.width = (int) (Mscreenwidth *9/10); Set width
dialog.getwindow (). SetAttributes (Layoutparams);

I hope this article will help you with the Android program.

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.