Android Custom Dialog

前端 未结 3 1491
[愿得一人]
[愿得一人] 2020-12-19 09:23

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:



        
相关标签:
3条回答
  • 2020-12-19 09:55

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

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

    Dialog dialog = new Dialog(this);
    
    0 讨论(0)
  • 2020-12-19 09:56

    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

    0 讨论(0)
  • 2020-12-19 09:58

    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>-->
    
    0 讨论(0)
提交回复
热议问题