Android Customization dialog box

Source: Internet
Author: User

Android comes with N dialog boxes and styles, but sometimes does not meet our needs, we can customize the dialog box, cut the map, put in the layout file can be, directly paste code:

Androidmanifest.xml

<manifest xmlns:android= "Http://schemas.android.com/apk/res/android"
Package= "Ldm.dialog"
Android:versioncode= "1"
Android:versionname= "1.0" >


<uses-sdk
Android:minsdkversion= "10"
Android:targetsdkversion= "/>"


<application
android:icon= "@drawable/ic_launcher"
Android:label= "@string/app_name"
Android:theme= "@style/apptheme" >
<activity
Android:name= ". Customactivity "
Android:label= "@string/title_activity_main" >
<intent-filter>
<action android:name= "Android.intent.action.MAIN"/>
<category android:name= "Android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>

Activity_main.xml:

<relativelayout 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" >


<textview
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
Android:layout_centerhorizontal= "true"
Android:layout_centervertical= "true"
android:text= "@string/hello_world"
Tools:context= ". Customactivity "/>

</RelativeLayout>

Custom_dialog.xml:

<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Xmlns:tools= "Http://schemas.android.com/tools"
Android:id= "@+id/linearlayout1"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:background= "@drawable/DIALOG_BG"
android:orientation= "Vertical" >


<relativelayout
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content" >
<imageview
Android:id= "@+id/ico"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:layout_marginleft= "50DP"
android:src= "@drawable/dialog_title_image"/>
<textview
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:layout_margintop= "20DP"
android:layout_torightof= "@+id/ico"
android:text= "Title"
Android:textsize= "42DP"/>
</RelativeLayout>

<!--Middle Split line--
<textview
Android:layout_width= "Wrap_content"
android:layout_height= "1DP"
android:background= "@drawable/lins"/>


<!--message body--
<textview
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:layout_gravity= "Center"
Android:text= "This is a custom dialog"/>


<relativelayout
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
Android:layout_marginbottom= "30DP"
android:layout_marginleft= "50DP"
android:layout_marginright= "50DP"
android:layout_margintop= "20DP" >
<button
Android:id= "@+id/ok"
Android:layout_width= "100DP"
android:layout_height= "Wrap_content"
android:layout_marginright= "20DP"
android:text= "OK"/>
<button
Android:id= "@+id/btn_canal"
Android:layout_width= "100DP"
android:layout_height= "Wrap_content"
android:layout_torightof= "@id/ok"
android:text= "Cancel"/>
</RelativeLayout>
</LinearLayout>

Values/mydialog.xml:

<resources xmlns:android= "Http://schemas.android.com/apk/res/android" >


<style name= "Mydialog" parent= "Android:Theme.Dialog" >
<item name= "Android:windowframe" > @null </item>
<!--Untitled--
<item name= "Android:windownotitle" >true</item>
<item name= "Android:windowbackground" > @drawable/dialog_bg</item>
<!--suspension effect--
<item name= "Android:windowisfloating" >true</item>
<!--Matte Effect-
<item name= "Android:windowcontentoverlay" > @null </item>
</style>


</resources>

Then there is the custom dialog box:

public class Customdialog extends Dialog {


/* OK button */
Private Button BTN_OK;
/* Context */
Private Context Mcontext;


Public Customdialog (Context context) {
Super (context);
Mcontext = context;
}


Public Customdialog (context context, int theme) {
Super (context, theme);
Mcontext = context;
}


@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.custom_dialog);
Findviews ();
Setlisteners ();
}


/* Instantiate UI */
private void Findviews () {
BTN_OK = (Button) Findviewbyid (R.id.ok);
}


/* Set OK button listener */
private void Setlisteners () {
Btn_ok.setonclicklistener (Onclicklistener);
}


Private View.onclicklistener Onclicklistener = new View.onclicklistener () {


@Override
public void OnClick (View v) {
Showresults ();
}


};

/* Show Results */
private void Showresults () {
Toast.maketext (Mcontext, "Customize dialog box click", Toast.length_long). Show ();
}
}

The end is activity:

public class Customactivity extends Activity {


@Override
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);

/* Instantiate a custom dialog box */
Customdialog dialog = new Customdialog (this, r.style.mydialog);
/* Show dialog box */
Dialog.show ();
}
}

Pictures can find their own favorite, I run the results of the main look like:


Android Customization dialog box

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.