0

I'm trying to customize layout of a DialogFragment my idea was to create a rounded corners dialog with buttons text color set to colorPrimary:

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.DialogFragment;

public class ExitDialogFragment extends DialogFragment {
    ExitDialogListener listener;

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(requireActivity());
        LayoutInflater inflater = requireActivity().getLayoutInflater();

        builder.setView(inflater.inflate(R.layout.dialog_exit, null))
                .setMessage(R.string.exit_dialog_title)
                .setPositiveButton(R.string.confirm_label, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        listener.onDialogPositiveClick(ExitDialogFragment.this);
                    }
                })
                .setNegativeButton(R.string.back_label, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        listener.onDialogNegativeClick(ExitDialogFragment.this);
                    }
                });

        return builder.create();
    }

    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);

        try {
            listener = (ExitDialogListener) context;
        } catch (Throwable e) {
            throw new ClassCastException("Must implement NoticeDialogListener");
        }
    }

    public interface ExitDialogListener {
        void onDialogPositiveClick(DialogFragment dialog);

        void onDialogNegativeClick(DialogFragment dialog);
    }
}

The dialog layout (dialog_exit.xml) is defined in this way:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/rounded_dialog"
    android:orientation="vertical"
    android:theme="@style/DialogTheme">
    
</LinearLayout>

Here I defined DialogTheme (styles.xml):

<resources>
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowActionBar">false</item>
        <item name="android:windowFullscreen">true</item>
    </style>

    <style name="Launcher" parent="AppTheme">
        <item name="android:windowBackground">@drawable/launch_screen</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowActionBar">false</item>
        <item name="android:windowFullscreen">true</item>
        <item name="android:windowContentOverlay">@null</item>
    </style>

    <style name="DialogTheme" parent="Theme.AppCompat.Light.Dialog">
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorPrimary</item>
        <item name="colorControlNormal">@color/colorPrimary</item>
        <item name="colorControlActivated">@color/colorPrimary</item>
    </style>

    <style name="UriEditText" parent="Theme.AppCompat.Light">
        <item name="colorControlNormal">@color/text_color_clear</item>
        <item name="colorControlActivated">@color/text_color_clear</item>
    </style>
</resources>

And here's the dialog background (rounded_dialog.xml):

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="@android:color/white" />
    <corners android:radius="20dp" />
    <padding
        android:bottom="10dp"
        android:left="10dp"
        android:right="10dp"
        android:top="10dp" />
</shape>

Nothing is applied. The dialog is just rectangular and buttons color is set to AppTheme colorAccent.

weirdgyn
  • 886
  • 16
  • 47

1 Answers1

0

Use CardView as container layout in rounded_dialog.xml.

Here is rounded_dialog.xml

    <?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView 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="wrap_content"
    android:layout_height="wrap_content"
    app:cardBackgroundColor="@color/colorPrimary"
    app:cardCornerRadius="16dp"
    app:cardElevation="4dp"
    app:cardUseCompatPadding="true">

    <!--Put a Constraint Layout(or any other container) here with required views-->

</androidx.cardview.widget.CardView>
Vishnu
  • 663
  • 3
  • 7
  • 24