6

I'm trying to make a custom dialog, following the tutorial on the Android developer site, but it crashes every time I try to show the dialog. Here's my code:

Context mContext = getApplicationContext();
Dialog dialog = new Dialog(mContext);

dialog.setContentView(R.layout.custom_dialog);
dialog.setTitle("Custom Dialog");
dialog.show();

And here's my XML for the layout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    android:id="@+id/layout_root"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="10dp"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <Button
        android:id="@+id/btnConfirm"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Add text"
        android:layout_below="@+id/txtNewText"
        android:layout_alignParentLeft="true">
    </Button>
    <EditText
        android:id="@+id/txtNewText"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="18sp"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true">
    </EditText>
</RelativeLayout>
Eric Platon
  • 9,819
  • 6
  • 41
  • 48
Elec0
  • 2,232
  • 3
  • 19
  • 25

3 Answers3

8

Consider the pattern:

private static final int MY_DIALOG= 0;

protected Dialog onCreateDialog(int id) {
    Dialog dialog;
    switch(id) {
        case MY_DIALOG:
            dialog= getInstanceMyDialog();
            break;
        default:
            dialog = null;
    }
    return dialog;
}

private Dialog getInstanceMyDialog() {
    final Dialog d= new Dialog(this); //<=====THIS
    d.setContentView(R.layout.custom_dialog);
    d.setTitle("Custom Dialog");
    return d;
}

JAL

Eric Platon
  • 9,819
  • 6
  • 41
  • 48
JAL
  • 3,319
  • 2
  • 20
  • 17
  • @ElecO The link that you supplied uses this pattern to create a custom dialog. The big difference is that the tutorial calls showDialog(MY_DIALOG) while the code that you posted uses dialog.show(). I have working code at: http://sites.google.com/site/jalcomputing/home/mac-osx-android-programming-tutorial/custom-password-dialog – JAL Apr 05 '11 at 04:22
3

This worked for me: problem-creating-a-custom-dialog

Use this instead of getApplicationContext() when instantiating the dialog:

Dialog dialog = new Dialog(this);
Community
  • 1
  • 1
user756427
  • 31
  • 2
  • Why does this happen? Sometimes I want to show a dialog after a click on a button. If I use "this" instead of "getApplicationContext()" doesn't work because "this" refers to a "OnClickListener". I have to do some ugly workaround to use "this"... – ffleandro Feb 13 '12 at 16:28
  • no, you don't just write ClassName.this of class that should be this – endryha Mar 28 '12 at 14:15
0

Kotlin way to create custom dialog in android:

Dialog(activity!!, R.style.LoadingIndicatorDialogStyle)
        .apply {
            // requestWindowFeature(Window.FEATURE_NO_TITLE)
            setCancelable(true)
            setContentView(R.layout.define_your_custom_view_id_here)

            //access your custom view buttons/editText like below.z
            val createBt = findViewById<TextView>(R.id.clipboard_create_project)
            val cancelBt = findViewById<TextView>(R.id.clipboard_cancel_project)
            val clipboard_et = findViewById<TextView>(R.id.clipboard_et)
            val manualOption =
                findViewById<TextView>(R.id.clipboard_manual_add_project_option)

            //if you want to perform any operation on the button do like this

            createBt.setOnClickListener {
                //handle your button click here
                val enteredData = clipboard_et.text.toString()
                if (enteredData.isEmpty()) {
                    Utils.toast("Enter project details")
                } else {
                    navigateToAddProject(enteredData, true)
                    dismiss()
                }
            }

            cancelBt.setOnClickListener {
                dismiss()
            }
            manualOption.setOnClickListener {
                navigateToAddProject("", false)
                dismiss()
            }
            show()
        }

Create LoadingIndicatorDialogStyle in style.xml

<style name="LoadingIndicatorDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:statusBarColor">@color/black_transperant</item>
<item name="android:layout_gravity">center</item>
<item name="android:background">@android:color/transparent</item>
<!--<item name="android:windowAnimationStyle">@style/MaterialDialogSheetAnimation</item>-->
syed dastagir
  • 419
  • 6
  • 8