0

I have a problem with this specific dialog, I implemented it as an option to be shown in the Options Menu in my toolbar but it doesn't show the dialog's contents when I clicked on the option. I have tried these with other dialog layouts I have implemented and all of them work when I implemented them here so I have a hunch that the problem is with the XML layout but I can't seem to find the problem.

Code:

else if(id == R.id.action_setting)
            {
                Dialog dialog = new Dialog(this);
                dialog.setContentView(R.layout.dialog_settings);
                dialog.setTitle("Settings");
                CheckBox set_splash = dialog.findViewById(R.id.setting_check);
                boolean have_splash =pref.getBoolean(SplashActivity.HAVE_SPLASH,false);
                if(have_splash)
                    set_splash.setChecked(true);
                else
                    set_splash.setChecked(false);
                ImageButton confirm_btn = dialog.findViewById(R.id.setting_confirm);
                ImageButton cancel_btn = dialog.findViewById(R.id.setting_cancel);
                confirm_btn.setOnClickListener(v -> {
                    boolean new_splash = set_splash.isChecked();
                    edit.putBoolean(SplashActivity.HAVE_SPLASH,new_splash);
                    edit.commit();
                    dialog.dismiss();
                });
                cancel_btn.setOnClickListener(v -> dialog.dismiss());
                dialog.show();
            }

XML :

<?xml version="1.0" encoding="utf-8"?>

    <androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/linearLayout5">

            <ImageButton
                android:id="@+id/setting_confirm"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="@drawable/button_highlight_dark"
                android:paddingVertical="20dp"
                app:srcCompat="@drawable/ic_confirm" />

            <ImageButton
                android:id="@+id/setting_cancel"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="@drawable/button_highlight_dark"
                android:paddingVertical="20dp"
                app:srcCompat="@drawable/ic_cancel" />
        </LinearLayout>

        <TextView
            android:id="@+id/setting_text"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:background="@color/themeDark"
            android:paddingVertical="10dp"
            android:paddingLeft="10dp"
            android:text="@string/settings"
            android:textColor="@color/white"
            android:textSize="10pt"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <LinearLayout
            android:id="@+id/linearLayout5"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:background="@color/themeLight"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/setting_text">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="6"
                android:textColor="@color/white"
                android:paddingVertical="20dp"
                android:paddingLeft="10dp"
                android:text="@string/splash_setting"
                android:textSize="10pt" />

            <CheckBox
                android:id="@+id/setting_check"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1" />
        </LinearLayout>

    </androidx.constraintlayout.widget.ConstraintLayout>
boom
  • 184
  • 1
  • 9

1 Answers1

0

The match_parent parameters in your dialog are ignored. Therefore, the dialog has a width of 0.

The easiest way to get the correct height and width, is by using an AlertDialog.Builder. If a customized AlertDialog isn't enough, you can make the dialog full size by following this answer: Android get full width for custom Dialog

Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.dialog_settings);
Window window = dialog.getWindow();
window.setLayout(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
Alexander Hoffmann
  • 5,104
  • 3
  • 17
  • 23