Skip to content Skip to sidebar Skip to footer

How Can I Change Default Black Dim Background "color" (not The Amount Of Dim) Of Dialog?

(This is a random image of showing a Dialog found on the Internet.) I've been implementing a custom Dialog. I could handle almost everything on the dialog except for that default

Solution 1:

This is a workaround but it's not really a pure solution because background touch is disabled and should be configured manually.

First, set custom dialog theme like this.

styles.xml

<stylename="CustomDialogTheme"parent="android:Theme.Dialog"><itemname="android:windowNoTitle">true</item><itemname="android:windowIsFloating">false</item><itemname="android:windowBackground">@android:color/transparent</item></style>

Setting windowIsFloating to false forces Dialog view to be expanded to full screen. Setting windowBackground to transparent removes default black dim background under Dialog. windowNoTitle option gets rid of the upper title bar.

CustomDialog.java

Apply the theme and construct your custom_dialog view as follows.

public HTCustomDialog(Context context) {
    super(context, R.style.CustomDialogTheme);
    setContentView(R.layout.custom_dialog);
}

custom_dialog.xml

<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:background="@color/main_solid_80"><RelativeLayoutandroid:id="@+id/dialog_root"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerInParent="true"android:layout_marginLeft="50dp"android:layout_marginRight="50dp"android:background="@drawable/bg_popup"android:padding="16dp"></RelativeLayout>

Now that CustomDialog view is a full-screen view, set background of your root layout to whatever color you'd like.

Sample result

I mosaiced the result a bit.

Result

Solution 2:

use custom style.

<stylename="transparent_dialog_borderless"parent="android:Theme.Dialog"><itemname="android:windowFrame">@null</item><itemname="android:windowIsFloating">true</item><itemname="android:windowIsTranslucent">true</item><itemname="android:windowNoTitle">true</item><itemname="android:background">#FF333333</item><itemname="android:windowBackground">@null</item><itemname="android:backgroundDimEnabled">true</item></style>

android:backgroundDimEnabled:control the black dim background

Solution 3:

Try to set the style for your dialog windows,

Example:

Dialogdialog=newDialog(context,android.R.style.Theme_Translucent_NoTitleBar);

Solution 4:

This worked for me:

val dialog = AlertDialog.Builder(context)
                .setView(view)
                .setCancelable(true)
                .setPositiveButton(R.string.done_label, { dialog, _ -> dialog.dismiss() })
                .create()
        dialog.window.setDimAmount(0f)
        dialog.show()

dialog.window.setDimAmount(0f) is the key.

Solution 5:

The following custom DatePickerDoalog class not only makes dim color customizable but also it makes dim appearing animated

/**
 * @author Taras Yurkiv @Devlight
 */publicclassDatePickerDialogCustomDimextendsDatePickerDialog {

    privatefinallonganimDuration=100;
    privatefloatdimAmount=0.7f;

    private Drawable dimDrawable;
    private ViewGroup root;

    private OnDismissListener outsideDismissListener;

    privatefinalOnDismissListenerdismissListener=newOnDismissListener() {

        @OverridepublicvoidonDismiss(DialogInterface dialog) {
            finalObjectAnimatoranimator= ObjectAnimator.ofPropertyValuesHolder(dimDrawable,
                    PropertyValuesHolder.ofInt("alpha", (int) (255 * dimAmount), 0));
            animator.setTarget(dimDrawable);
            animator.setDuration(animDuration);
            animator.addListener(newAnimatorListenerAdapter() {
                @OverridepublicvoidonAnimationEnd(Animator animation) {
                    ViewGroupOverlayoverlay= root.getOverlay();
                    overlay.remove(dimDrawable);
                }
            });
            animator.start();
            if (outsideDismissListener != null)
                outsideDismissListener.onDismiss(dialog);
        }
    };


    @TargetApi(Build.VERSION_CODES.N)publicDatePickerDialogCustomDim(@NonNull Context context) {
        this(context, 0);
    }

    @TargetApi(Build.VERSION_CODES.N)publicDatePickerDialogCustomDim(@NonNull Context context, @StyleResint themeResId) {
        this(context, themeResId, null, -1, -1, -1);
        init(context);
    }

    publicDatePickerDialogCustomDim(@NonNull Context context,
                                     @Nullable OnDateSetListener listener,
                                     int year,
                                     int month,
                                     int dayOfMonth) {
        this(context, 0, listener, year, month, dayOfMonth);
    }

    publicDatePickerDialogCustomDim(@NonNull Context context,
                                     @StyleResint themeResId,
                                     @Nullable OnDateSetListener listener,
                                     int year,
                                     int monthOfYear,
                                     int dayOfMonth) {
        super(context, themeResId, listener, year, monthOfYear, dayOfMonth);
        init(context);
    }

    privatevoidinit(Context context) {
        root = ((Activity) context).getWindow().getDecorView().findViewById(android.R.id.content);
        super.setOnDismissListener(dismissListener);
    }

    publicvoidsetDimAmount(@FloatRange(from = 0, to = 1f)float dim) {
        dimAmount = dim;
    }

    @Overridepublicvoidshow() {
        super.show();
        dimDrawable = newColorDrawable(Color.WHITE); // a dim color
        dimDrawable.setBounds(0, 0, root.getWidth(), root.getHeight());

        ViewGroupOverlayoverlay= root.getOverlay();
        overlay.add(dimDrawable);

        ObjectAnimatoranimator= ObjectAnimator.ofPropertyValuesHolder(dimDrawable,
                PropertyValuesHolder.ofInt("alpha", 0, (int) (255 * dimAmount)));
        animator.setTarget(dimDrawable);
        animator.setDuration(animDuration);
        animator.start();
    }

    @OverridepublicvoidsetOnDismissListener(@Nullable OnDismissListener listener) {
        outsideDismissListener = listener;
    }
}

it works in conjunction of a style

<stylename="DatePickerDialogTheme"parent="Theme.AppCompat.Light.Dialog"><itemname="colorAccent">@color/accent</item><itemname="android:textColorLink">@color/primary</item><itemname="android:windowIsFloating">true</item><itemname="android:windowIsTranslucent">true</item><itemname="android:windowNoTitle">true</item><itemname="android:backgroundDimEnabled">false</item></style>

Post a Comment for "How Can I Change Default Black Dim Background "color" (not The Amount Of Dim) Of Dialog?"