Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Customizing dialog by extending Dialog or AlertDialog

I want to make a custom Dialog. Because I don't like its style, I want to have rounded rectangle rather than sharp corners. I know how to implement it by theme in AndroidManifest.xml, for example, I use:

android:theme="@style/Theme.CustomDialog"

And Theme.CustomDialog.xml:

<style name="Theme.CustomDialog" parent="android:style/Theme.Dialog">
        <item name="android:windowBackground">@drawable/filled_box</item>
        <item name="android:windowNoTitle">true</item>

filled_box.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#ffffffff"/>
    <stroke android:width="3dp" color="#ffff8080"/>
    <corners android:radius="30dp" />
    <padding android:left="10dp" android:top="10dp"
        android:right="10dp" android:bottom="10dp" />
</shape>

How can I implement a similar result by extending the Dialog or AlertDialog?

like image 837
pengwang Avatar asked Dec 29 '09 14:12

pengwang


People also ask

What is the difference between dialog and AlertDialog?

AlertDialog is a lightweight version of a Dialog. This is supposed to deal with INFORMATIVE matters only, That's the reason why complex interactions with the user are limited. Dialog on the other hand is able to do even more complex things .

What is the use of AlertDialog in android?

Android AlertDialog can be used to display the dialog message with OK and Cancel buttons. It can be used to interrupt and ask the user about his/her choice to continue or discontinue. Android AlertDialog is composed of three regions: title, content area and action buttons.

What is the difference between dialog and DialogFragment?

Dialog: A dialog is a small window that prompts the user to make a decision or enter additional information. DialogFragment: A DialogFragment is a special fragment subclass that is designed for creating and hosting dialogs.


1 Answers

In the constructor of your class that extends Dialog call super(context, R.style.CustomDialog); I've done this many times to create custom dialogs with specific themes.

However if the theme is the only thing about the Dialog that you want to change, you could try just instantiating an instance of the Dialog class and pass it the theme ID like Dialog dialog = new Dialog(context, R.style.CustomDialog);

An example of extending Dialog:

public class MyDialog extends Dialog
{
    public MyDialog(final Context context)
    {
        // Set your theme here
        super(context, R.style.MyDialogTheme);

        // This is the layout XML file that describes your Dialog layout
        this.setContentView(R.layout.myDialogLayout);  
    }
}

The rest of the code you will add to this class is going to be pretty much exactly like what you would write in an Activity class.

like image 81
Mark B Avatar answered Sep 28 '22 10:09

Mark B