Android bottom sheet dialog how to set scrim animation - android

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>
)

Related

How to set background color of exposed dropdown PopupWindow?

I'm using an exposed dropdown from material design components. It's an AutoCompleteTextView within a TextInputLayout which basically acts like a spinner. You give it an adapter and it pops up a PopupWindow on click.
The PopupWindow background is a dark gray. I'd like to change it to white. I've tried this via theming to no avail:
<style name="PopupWindow">
<item name="popupMenuBackground">#color/white</item>
<item name="android:popupBackground">#color/white</item>
<item name="android:windowBackground">#color/white</item>
</style>
I've also tried calling this on the AutoCompleteTextView:
autoCompleteTextView.setDropDownBackgroundResource(R.color.white);
This actually worked. However, if I open the dropdown when the keyboard is open, the background turns dark gray again. It's only when the keyboard is dismissed that the background is white.
Any idea how to solve this? Surely there has to be a theme attribute that I can override to make the PopupWindow background white?
Found the solution thanks to this post: https://medium.com/#rmirabelle/there-is-no-material-design-spinner-for-android-3261b7c77da8
Just override colorSurface in your theme:
<item name="colorSurface">#color/white</item>

How to create a dialog style that looks as snackbar style

I am trying to write a custom dialog style that will give me the styling of a snackbar where the dialog is anchored at the bottom of the screen where there is no gap between the box and the navigation control and takes up full width of the screen.
sample_look
How do i style this?
<style name="AlertDialogCustom" parent="#android:style/Theme.Dialog">
</style>
If you want dialog to appear at bottom , then you can set the gravity appropriately .
Window window = getDialog().getWindow();
window.setGravity(Gravity.BOTTOM);
You can also use bottomsheet dialog .

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

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.

PopupWindow update() animation

Is there a way to animate PopupWindow when issuing update() method? I'm changing my popup's position there, so it would be nice if I could smoothly slide the popup to its destination instead of just "jumping". I know I can easily animate the PopupWindow's entering and exiting via adding these items to its style:
<style name="MyPopup">
<item name="#android:windowEnterAnimation">#anim/slide_in_right</item>
<item name="#android:windowExitAnimation">#anim/slide_out_to_right</item>
</style>
But I'm interested in its position updating animation.

Categories

Resources