How to NOT center android dialog? - android

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.

Related

Android bottom sheet dialog how to set scrim animation

I noticed that by default, when a bottom sheet dialog is shown, the scrim just pop up without any animation, which is not a good user experience. So I was wondering how to show the scrim with animation. Any suggestion?
You can use
getDialog().getWindow().getAttributes().windowAnimations = *Your animation here*(
For example, R.style.myAnimation
<style name="myAnimation">
<item name="android:windowEnterAnimation">#anim/up</item>
<item name="android:windowExitAnimation">#anim/down</item>
</style>
)

Changing default Android fade/scrim color when calling a Dialog

I've been working on an app and I've reaching the point where it requires me to display a menu window in the middle of the screen.
I've been using an AlertDialog object filled with a custom View but now it was required of me to "surround" the window with a semi-transparent white glow as opposed to the default grayish one. I did a similar with the fade-in color of some navigation drawers I have on my app but in that case I had a specific method to quickly help me solve that problem. So far I haven't found anything that helps me solve this one.
I tried creating a default style with a new "windowBackground" value but I encountered 3 problems from the get-go:
I'm no longer able to shut the AlertDialog down by clicking outside the layout (I'm guessing because by changing the color that way everything is now the layout)
The menu window is now surrounded by a black outline that wasn't there before
By using the filtering search inside the layout, which manipulates the members of a list, the window collapses on itself
Is there any way to accomplish what I want more or less directly?
I'm not really sure about it, but you can use this in your styles.xml
<style name="MyDialogTheme" parent="android:Theme.AppCompat.Light.Dialog">
<item name="android:windowFrame">#null</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowBackground">#color/your_light_color</item>
<item name="android:backgroundDimEnabled">false</item>
And if you want to dismiss the dialog when clicking outside, use this:
dialog.setCanceledOnTouchOutside(true);
or
<item name="windowCloseOnTouchOutside">true</item>
in your styles.xml

Create a transparent dialog on top of activity

Background
I'm trying to put a layer on top of the current activity which would have explanation of what is going on on the current screen, similar to what occurs on contact+ app .
I know there are some solutions for this (like the showCase library and the superToolTips library ) , and I also know that I can create a view and set it on top by adding it to the window of the activity , but I need put a whole dialog layer on top.
Problem
No matter what I try, each solution doesn't work the way I need it to work.
in short , what I need is:
full screen dialog.
no change (not visual and not logical) to the action bar, notification bar, and content of the activity behind, meaning that everything behind the dialog stays the same it was shown a moment before the dialog was shown.
be transparent except for the views I use for the dialog, which should be shown normally.
what I've tried
Sadly, I've always got only a part of the things I needed.
here's my code:
styles.xml:
<style name="full_screen_dialog">
<item name="android:windowFrame">#null</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:windowAnimationStyle">#android:style/Animation.Dialog</item>
<item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
</style>
MainActivity.java:
...
final Dialog dialog = new Dialog(this, R.style.full_screen_dialog);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.floating_tutorial);
dialog.getWindow().setLayout(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
dialog.getWindow().setFormat(PixelFormat.TRANSLUCENT);
dialog.show();
This code will put the layout on top of the activity, but sadly it doesn't have any transparency , even though I've set it . The layout I've used is very simple which is why I don't post it.
Question
what am I missing ? what should be done to fix the code?
how can I make the dialog both transparent, full screen AND that it won't change the action bar and notifications bar.
Working solution
EDIT: after finding a good solution, here's the working code:
#Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Dialog dialog = new Dialog(this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.floating_tutorial);
final Window window = dialog.getWindow();
window.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT);
window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
dialog.show();
}
Just change the background color of your Dialog:
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
Edit:
This prevents the dim effect:
dialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
Just add this, it really works!
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowCloseOnTouchOutside">true</item>
<item name="android:windowBackground">#android:drawable/alert_dark_frame</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsFloating">true</item>
<item name="android:backgroundDimEnabled">false</item>

windowIsFloating attribute in Android theme

What does this attribute really do?
I've read the documentation and I understand what it's supposed to be. However, when I use it in a theme (I created a style with the android:Theme.Dialog as the parent), changing the value for this attribute doesn't seem to have any effect.
I don't know all of the effects of setting windowIsFloating to true, but one thing that I noticed is that when it is set to true, the activity would not expand the width to fill the screen, even if you set the layout width to match_parent (i.e. android:layout_width="match_parent")
From my experiments, it seems to affect how the window reacts to the soft input. It you set this to false, the window won't slide up when the keyboard becomes visible.
When you use Dialog Theme, this attribute default is true.
<style name="Base.V7.Theme.AppCompat.Light.Dialog" parent="Base.Theme.AppCompat.Light">
...
<item name="android:windowIsFloating">true</item>
...
</style>
And in this case, the Dialog will create PhoneWindow with this code:
if (mIsFloating) {
setLayout(WRAP_CONTENT, WRAP_CONTENT);
setFlags(0, flagsToUpdate);
}
It will cause contentView always wrap content as small as. You could set it to false or Manual update the Dialog window layout to MATCH_PARENT like this:
override fun onStart() {
super.onStart()
dialog?.window?.setLayout(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)
}
I was using really naïve code to show a bottom sheet dialog fragment. However, my dialog was covering bottom navigation bar in light mode. See the picture below:
After adding the item to my dialog theme, this issue got resolved.
<style name="myTheme" parent="Theme.MaterialComponents.BottomSheetDialog">
<item name="android:windowSoftInputMode">stateAlwaysHidden|adjustNothing</item>
<item name="android:statusBarColor">#android:color/transparent</item>
<item name="android:windowIsFloating">false</item>
</style>

How to create a fully customized Alert Dialog?

Is there a solution to create an Alert Dialog with no borders around? If I create a layout and attach it to the Alert Dialog, I have my layout shown, but it's surrounded by the default dialog borders. Any way to cut them off? Thanks in advance.
Here is one way (if you don't like this particular example with the green border, scroll below to see a second one I found).
Here is another example (this one gives you an example without a border, and one with a border. The explanations are in Japanese, but the code is given for both.)
Hi if you use your own layout then set this in your styles.xml
<style name="Theme.Transparent_layout" parent="android:style/Theme.Dialog">
<item name="android:windowNoTitle">true</item>
<item name="android:windowFullscreen">true</item>
<item name="android:windowBackground">#android:color/transparent</item>
</style>
and set this style in your manifest file
<activity android:name=".Main" android:theme="#style/Theme.Transparent_layout" />

Categories

Resources