Android: make everything around Dialog darker than default - android

i have a custom dialog with following style:
<style name="webtogo_app_style" parent="#android:style/Theme.Dialog">
<item name="android:windowNoTitle">true</item>
<item name="android:windowBackground">#android:color/transparent</item>
</style>
It shows a borderless dialog, and anything behind gets (slightly) darker. My designer wants that everything behind got ever more dark than Android's default, but not completely black.
Is there a setting for this at all?
The only workaround I can think of is to use a full-screen activity instead of a dialog and just fill up the whole screen with semitransparent color (e.g. #99000000) and then draw my dialog over it. Is there an easier way?
Thanks!

All you need to do is play around with the dimAmount field in the WindowManager.LayoutParams:
WindowManager.LayoutParams lp = myDialog.getWindow().getAttributes();
lp.dimAmount = 0.7f

If you are creating custom dialog with theme translucent, you have to add below line as well.
and you can control dim amount using above answer's code.
myDialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
For me it looks like below:
WindowManager.LayoutParams lp = myDialog.getWindow().getAttributes();
lp.dimAmount = 0.7f
myDialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);

WindowManager.LayoutParams lp=getWindow().getAttributes();
//set transparency of background
lp.dimAmount=0.6f; // dimAmount between 0.0f and 1.0f, 1.0f is completely dark
//lp.width = 200;
//lp.height = 300;
myDialog.getWindow().setAttributes(lp);
myDialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);

try doing this
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
where dialog is the name of the dialogue box created.

Related

Fullscreen DialogFragment with softInputMode adjustResize

So I have a DialogFragment that i expand to the whole screen in onResume with the following code:
val params = dialog?.window?.attributes
params?.width = ViewGroup.LayoutParams.MATCH_PARENT
params?.height = ViewGroup.LayoutParams.MATCH_PARENT
dialog?.window?.attributes = params as WindowManager.LayoutParams
I also use the following style:
<style name="AppTheme.FullscreenDialogFragment" parent="Theme.MaterialComponents.Light.Dialog">
<item name="android:windowIsFloating">false</item>
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
<item name="android:windowBackground">#color/background1</item>
</style>
Now I also want this to extend under the status bar and I do this via the following (I also set the status bar color to transparent):
dialog?.window?.decorView?.systemUiVisibility =
View.SYSTEM_UI_FLAG_LAYOUT_STABLE.addFlag(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN)
This all works completely fine, but now I want to adjust the softInputMode to adjustResize. And I just can't get it to work. It only works if I remove the Layout flags above. It works fine in normal fragments. Does anybody have an idea what I'm missing here or is it just not possible with a DialogFragment?
So my problem was not the DialogFragment but the fact that Android does not honor adjustResize when you make your layout draw under the system bars. To prevent this you need to listen to the Window insets and apply them as padding to your layout so that when the keyboard is open, the bottom inset makes your whole content smaller for that amount and the ScrollView can be scrolled to the bottom. I use the following library for insets:
https://github.com/chrisbanes/insetter

DialogFragment gravity not showing dialog completely in corner

I want dialog to appear completely in corner. I am setting gravity this way:
Window win = dialog.getWindow();
WindowManager.LayoutParams wmlp = win.getAttributes();
wmlp.gravity = Gravity.TOP | Gravity.RIGHT;
wmlp.horizontalMargin = 0;
wmlp.verticalMargin = 0;
Margins don't affect anything here. Layout inspector also doesn't work on dialog so I can't really inspect what is setting margin/padding.
Can I show this dialog fragment completely in corner (without recreating custom overlay fragment over whole screen that acts as a dialog)?
That extra padding/margin may have something to do with the background drawable. Change the style of your dialog to set android:windowBackground and android:background to be transparent by doing something like this:
<style name="TransparentDialog" parent="#style/Theme.AppCompat.Light.Dialog">
<item name="android:windowBackground">#android:color/transparent</item>
<item name="android:background">#android:color/transparent</item>
</style>
(I believe that android:windowBackground is important here, and you may not need to set android:background.)
Thanks goes to andrew who answered this question for helping to provide a solution.

How to NOT center android dialog?

I want to use a dialog but I don't want it to be centered vertically on the screen.
I created a Dialog and used SetContentView() to apply the layout.
I also applied style and no title option. Here is what I did.
mydialog = new Dialog (context, Resource.Style.myDialogAnim);
mydialog.RequestWindowFeature ((int)WindowFeatures.NoTitle);
mydialog.SetContentView (myDialogView);
mydialog.SetCanceledOnTouchOutside (false);
The style I applied to this dialog is here:
<style name="myDialogAnim" parent="#android:style/Theme.Dialog">
<item name="android:windowAnimationStyle">#style/slideInAndOut</item>
<item name="android:windowIsFloating">false</item>
<item name="android:windowBackground">#android:color/transparent</item>
</style>
<style name="slideInAndOut">
<item name="android:windowEnterAnimation">#anim/anim_animatein</item>
<item name="android:windowExitAnimation">#anim/anim_animateaway</item>
</style>
When I use this dialog, the dialog animates in from the fromYDelta location to the toYDelta location in anim_animatein but as soon as the animation is done, it centers itself vertically.
I need it to be centered horizontally on the screen but NOT vertically.
How can I do this?
Thank you for your time.
I figured it out.
WindowManagerLayoutParams dialogParams = mydialog.Window.Attributes;
dialogParams.Gravity = GravityFlags.Bottom;
int dialogYPos = 69 * (int)activity.Resources.DisplayMetrics.Density;
dialogParams.Y = dialogYPos;
dialogYPos returns how many pixels you want it to move up from it's location. So I set it's Gravity to Bottom so it's that many pixels from the bottom of the screen.
Extra:
Since I am using the dialog as just a floating message, I wanted the user to be able to use the current activity when the dialog is showing. I added the following flag and it worked perfecly.
mydialog.Window.AddFlags (WindowManagerFlags.NotTouchModal);
Now I am using a Dialog as an android Toast Widget with my animation and complete customization.
I hope this helps someone.

Changing Background dim color for dialog appears in android

Hi I have to chage the dim color of dialog in my theme to white, so that by setting layoutParams.dimAmount = 0.5f; i can get blur white in dialog background.
I am using
<style name="MyDialog" parent="#android:style/Theme.Holo.Light.Dialog">
<item name="android:windowBackground">#color/dialog_dim_background</item>
</style>
and following below references
How do I create a transparent Activity on Android?
Custom screen dim with Dialog
It is a very old question, but i'd like to add n answer to it. I faced the same problem and googled a lot. Then came up with my own solution.
First of all, I removed the dim at all with this
dialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
And then I opened my layout(it was RelativeLayout) and put this view in it as the last element
<View
android:id="#+id/shade"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/primaryShadow"
android:visibility="invisible"/>
The background color is the color you want to use as your dim color. As you can see, it is invisible when created. So you'll need to find it in you Activity and set it's visibility before you launch the dialog to View.VISIBLE. When you cancel dialog, set it to View.INVISIBLE again. And that's it.
I know it's very old question but this will help out surely to someone who is using AppCompatActivity or ActionBarActivity. If you are setting a dialog to your activity or say DialogActivity then also the below will work!
dialog = new Dialog(ActivityName.this);
dialog .setCancelable(false);
dialog .setContentView(R.layout.dialog_layout);
Window window = dialog.getWindow();
if(window != null){
window.addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
window.setDimAmount(0.5f);
//If you are setting a dialog activity
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
Double dialogWidth = displayMetrics.widthPixels * 0.50;
window.setLayout(dialogWidth.intValue(),
WindowManager.LayoutParams.WRAP_CONTENT); //Setting it cover half of the screen width
}
dialog.show();
Just before you dismiss the dialog,
dialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
dialog.dismiss();
//If you are setting a dialog activity, create a style in styles.xml:
<style name="MyTheme" parent="#style/Theme.AppCompat.Light.Dialog">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="android:backgroundDimEnabled">false</item>
</style>
And in your Manifest, set the theme:
<activity android:name=".view.POSSelectCustomerDialogActivity"
android:theme="#style/MyTheme"
android:windowSoftInputMode="stateAlwaysHidden|adjustResize"/>
Ok here is how I do that "I don't know how will these behave with the blur flag though"
I create a custom layout for the dialog with a background color of my choosing .
set the dialog to fill screen
remove the dim behind flag
Code snippet
Dialog dialog = new Dialog(this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.dailog_layout);
dialog.getWindow().setBackgroundDrawable(
new ColorDrawable(0));
dialog.getWindow().setLayout(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT);
dialog.getWindow().clearFlags(
WindowManager.LayoutParams.FLAG_DIM_BEHIND);
Best of luck

android dialog transparent

I want to get rid of the border in my dialog box and make it look absolutly transparent, as if the image is on the top of screen.
My dialog xml is -
<?xml version="1.0" encoding="utf-8"?>
<ImageView android:id="#+id/ImageView01"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_centerHorizontal="true" android:visibility="invisible"/>
Try below code
Dialog mDialog = new Dialog(mContext, android.R.style.Theme_Translucent_NoTitleBar_Fullscreen);
try this:
mDialog.getWindow().setBackgroundDrawable(new ColorDrawable(0));
To give a translucent effect, say 50% opacity, use:
Drawable d = new ColorDrawable(Color.BLACK);
d.setAlpha(130);
mDialog.getWindow().setBackgroundDrawable(d);
'130' can be changed (0-255) to acheive desired opacity.
try this:-
final Dialog dialog = new Dialog(this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
dialog.setContentView(R.layout.splash);
dialog.show();
For API 11+
Dialog mDialog = new Dialog(mContext, android.R.style.Theme_Holo_Light_Panel);
The simplest way of doing this is that in your DialogFragment's onCreate() method, call
setStyle(DialogFragment.STYLE_NO_FRAME, 0);
And if the view you returned in onCreateView does not have a background specified, the dialog's background will be just transparent.
Why? DialogFragment.STYLE_NO_FRAME means that OS will not do any drawing in the window of the dialog, and your view is 100% responsible for drawing everything about the dialog.
The accepted answer causes issue with devices that have a notch, as the statusbar completely disappears and shows an ugly white background.
Instead, the right way for this is to define your own style.
<style name="FullScreenDialogTheme" parent="Theme.MaterialComponents.Dialog">
<item name="android:windowIsFloating">false</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:backgroundDimEnabled">false</item>
<item name="android:statusBarColor">#color/colorPrimary</item>
</style>
Now, this will make the window not have borders and be transparent over the screen.

Categories

Resources