windowIsFloating attribute in Android theme - android

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>

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

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 fully transparent Navigation Bar?

I want to change the Navigation Bar fully transparent like on the picture below.
I tried <item name="android:windowTranslucentNavigation">true</item> but it is not fully transparent (more like 50%).
Is there a solution? Because i found nothing to it, but I saw some apps that used it like Nova. And its even in googles guidelines https://material.google.com/layout/structure.html#structure-system-bars
I was inspired by Google Keep app, which have similar implementation like shown below:
So i tried to find a proper post on how to achieve this thing but unfortunately found nothing and also above answers weren't working. So i decided to try out all the flags related to navigationBar Android Studio was suggesting.
Let me explain in detail:
Only android:navigationBarColor won't do any thing when you are using a light theme. But on dark theme, it will work.
Setting windowTranslucentNavigation to true will do 2 things:
draw the activity below the soft navbar
override navigationBarColor and force it to be transparent.
Which will look like below image:
If you use FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS (follow #Frankenxtein's answer) you will get below result:
Which is somehow close to desired result, but it isn't because it draws activity components below navbar. And also it mess your padding and margin (see full screenshot here). And more, you have to do it for each activity and also have to adjust current margin.
The Solution
This can be achieved without compromising current layout. To do this in Dark theme, you have no issue, just setting android:navigationBarColor will do the work.
However our goal here is to do it with a light theme let (#FFFFFF). Using android:windowLightNavigationBar which requires API 27 or higher along with android:navigationBarColor will result what we want.
<item name="android:navigationBarColor">#color/colorPrimaryDark</item>
<item name="android:windowLightNavigationBar">true</item>
Here for my app case colorPrimaryDark is #FFFFFF in light theme and dracula in dark theme, you can also declare a new variable for this. Which yields below results:
Light Theme
Dark Theme
Here android:navigationBarColor is setting background colour of navbar. And android:windowLightNavigationBar setting dark button colours and indicates that background is light as you can understand from it's name.
Hope this helps !
use following code in onCreate of your Activity:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
Window w = getWindow(); w.setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
}
and in your styles.xml:
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
<item name="android:windowTranslucentNavigation">false</item>
Since no one has mentioned this and I spent too much time trying to figure it out on my own. A solution for API 29+ to make the system bars fully transparent regardless of anything:
<item name="android:enforceNavigationBarContrast">false</item>
<item name="android:enforceStatusBarContrast">false</item>
You can achieve that using FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS. Documentation explains:
Flag indicating that this Window is responsible for drawing the
background for the system bars. If set, the system bars are drawn with
a transparent background and the corresponding areas in this window
are filled with the colors specified in getStatusBarColor() and
getNavigationBarColor().
Set it like that in your Activity's onCreate:
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
Try this
<resources>
<style name="Theme" parent="android:Theme.Material.Wallpaper.NoTitleBar">
<item name="android:navigationBarColor">#android:color/transparent</item>
<item name="android:windowTranslucentNavigation">true</item>
</style>
It worked for android >= 10
You can try the color by default color android
<item name="android:navigationBarColor">#android:color/transparent</item>
<item name="android:windowLightNavigationBar">true</item>
<item name="android:windowTranslucentNavigation">true</item>
By the way you can try this
<item name="android:fitsSystemWindows">true</item>
<item name="android:statusBarColor">#android:color/transparent</item>
<item name="android:navigationBarColor">#android:color/transparent</item>

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

windowSoftInputMode has no effect in Android M

I have a MainActivity with android:windowSoftInputMode="adjustNothing" set in the AndroidManifest. The Theme has parent Theme.AppCompat.Light.NoActionBar. I add a DialogFragment to this activity and show an AlertDialog inside of it, then set alertDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN); inside the fragment.
Now, on Android 5.1.1 it works as expected. The keyboard does not auto show when the dialog is created. When the user taps on an EditText inside of the dialog, the keyboard pops up and resizes the activity so that it won't overlap.
The problem is that on Android M, this doesn't happen. The keyboard is indeed not shown when the dialog is created, but when it pops-up after the user touched an EditText, it overlaps the dialog.
Any idea why this happens on M, but on previous versions everything works fine?
Edit: Apparently after creating a HelloWorld project with only the basics of the issue, I've found out that the below 2 Activity Theme elements cause the keyboard to not resize. If anybody has any permanent solution to this matter, I'm all ears (or rather eyes).
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
<item name="android:windowTranslucentStatus">true</item>
</style>
I've figured out that the following 2 lines from the Activity Theme causes the keyboard to not resize.
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
<item name="android:windowTranslucentStatus">true</item>
</style>
For now, this is a quick fix. If anybody has a permanent solution to maybe retain those 2 lines but also fix the problem, please do post another answer.

Categories

Resources