(This is a random image of showing a Dialog
found on the Internet
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.
The following custom DatePickerDoalog class not only makes dim color customizable but also it makes dim appearing animated
/**
* @author Taras Yurkiv @Devlight
*/
public class DatePickerDialogCustomDim extends DatePickerDialog {
private final long animDuration = 100;
private float dimAmount = 0.7f;
private Drawable dimDrawable;
private ViewGroup root;
private OnDismissListener outsideDismissListener;
private final OnDismissListener dismissListener = new OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
final ObjectAnimator animator = ObjectAnimator.ofPropertyValuesHolder(dimDrawable,
PropertyValuesHolder.ofInt("alpha", (int) (255 * dimAmount), 0));
animator.setTarget(dimDrawable);
animator.setDuration(animDuration);
animator.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
ViewGroupOverlay overlay = root.getOverlay();
overlay.remove(dimDrawable);
}
});
animator.start();
if (outsideDismissListener != null)
outsideDismissListener.onDismiss(dialog);
}
};
@TargetApi(Build.VERSION_CODES.N)
public DatePickerDialogCustomDim(@NonNull Context context) {
this(context, 0);
}
@TargetApi(Build.VERSION_CODES.N)
public DatePickerDialogCustomDim(@NonNull Context context, @StyleRes int themeResId) {
this(context, themeResId, null, -1, -1, -1);
init(context);
}
public DatePickerDialogCustomDim(@NonNull Context context,
@Nullable OnDateSetListener listener,
int year,
int month,
int dayOfMonth) {
this(context, 0, listener, year, month, dayOfMonth);
}
public DatePickerDialogCustomDim(@NonNull Context context,
@StyleRes int themeResId,
@Nullable OnDateSetListener listener,
int year,
int monthOfYear,
int dayOfMonth) {
super(context, themeResId, listener, year, monthOfYear, dayOfMonth);
init(context);
}
private void init(Context context) {
root = ((Activity) context).getWindow().getDecorView().findViewById(android.R.id.content);
super.setOnDismissListener(dismissListener);
}
public void setDimAmount(@FloatRange(from = 0, to = 1f) float dim) {
dimAmount = dim;
}
@Override
public void show() {
super.show();
dimDrawable = new ColorDrawable(Color.WHITE); // a dim color
dimDrawable.setBounds(0, 0, root.getWidth(), root.getHeight());
ViewGroupOverlay overlay = root.getOverlay();
overlay.add(dimDrawable);
ObjectAnimator animator = ObjectAnimator.ofPropertyValuesHolder(dimDrawable,
PropertyValuesHolder.ofInt("alpha", 0, (int) (255 * dimAmount)));
animator.setTarget(dimDrawable);
animator.setDuration(animDuration);
animator.start();
}
@Override
public void setOnDismissListener(@Nullable OnDismissListener listener) {
outsideDismissListener = listener;
}
}
it works in conjunction of a style
<style name="DatePickerDialogTheme" parent="Theme.AppCompat.Light.Dialog">
<item name="colorAccent">@color/accent</item>
<item name="android:textColorLink">@color/primary</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowNoTitle">true</item>
<item name="android:backgroundDimEnabled">false</item>
</style>
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.
<style name="CustomDialogTheme" parent="android:Theme.Dialog">
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsFloating">false</item>
<item name="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.
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);
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/main_solid_80">
<RelativeLayout
android: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.
I mosaiced the result a bit.
try the code,
View checkBoxView = View.inflate(context, R.layout.alertbox, null);
final AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setView(checkBoxView);
builder.setCancelable(false);
Dialog d = builder.create();
d.getWindow().setBackgroundDrawable(new ColorDrawable(0));
d.setView(checkBoxView, 0, 0, 0, 0);
d.show();
nb: the line d.setView(checkBoxView, 0, 0, 0, 0); will make it...
Try to set the style for your dialog windows,
Example:
Dialog dialog = new Dialog(context,android.R.style.Theme_Translucent_NoTitleBar);
use custom style.
<style name="transparent_dialog_borderless" parent="android:Theme.Dialog">
<item name="android:windowFrame">@null</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowNoTitle">true</item>
<item name="android:background">#FF333333</item>
<item name="android:windowBackground">@null</item>
<item name="android:backgroundDimEnabled">true</item>
</style>
android:backgroundDimEnabled:control the black dim background