I’m trying to take advantage of the new KitKat translucent system bars, to get a full screen image in the background of my app.
I’m having problems figuring out the right settings to get the behaviour I want. Right now I have a
ViewPager with three
Fragments each one composed of a
RelativeLayout containing an
ImageView (for the background image) and a
TextView for the content.
What I’m after is to have the content fit fully in the portion of the screen available for interactions, and the image to take the whole visible portion of the screen.
If I just use
android:Theme.Holo.Light.NoActionBar.TranslucentDecor as my theme it looks fine in portrait, but the navigation bar overlaps the content in landscape (see the screenshots below).
Following the recommendation in the docs, I added
android:fitsSystemWindows = true to my theme, but that generates some weird artifacts (see below)
How can I make it behave like in the second example, but look good, without the visual artifacts?
I know this is old, but I’ve just run into the same problem and found an easy solution, so I wanted to share it in case this was run into by anyone Google’ing around.
It seems that there is an Android bug when a ViewPager (especially an ImageSwitcher) is placed within a layout that has the attribute
android:fitsSystemWindows="true". For some reason the system windows seem to be getting artifacts drawn all over them. :/
Anyway, I found a fix. For my activity, I had an XML layout as follows:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto" <!-- NOT HERE! android:fitsSystemWindows="true" --> android:layout_width="match_parent" android:layout_height="match_parent"> <ImageSwitcher android:id="@+id/background_image_switcher" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="0dp" android:layout_margin="0dp" android:background="@color/background_dark_navy" android:inAnimation="@android:anim/fade_in" android:outAnimation="@android:anim/fade_out" > <ImageView style="@style/BlurredBackgroundImage" /> <ImageView style="@style/BlurredBackgroundImage" /> </ImageSwitcher> <FrameLayout android:fitsSystemWindows="true" <!-- Here!!! --> android:layout_width="match_parent" android:layout_height="match_parent"> <!-- Fitted content here --> </FrameLayout>
The trick was to not contain the ImageSwitcher in a layout with the
android:fitsSystemWindows="true" attribute, but to instead move the
android:fitsSystemWindows="true" attribute to the inner FrameLayout containing the actual content I needed to fit (the title text in your case). Unfortunately, this allows for the ImageSwitcher/ViewPager’s view to get slightly cut off by the system windows, but if the image is used like a background image anyway it doesn’t matter too much and is a much better trade off than artifacts or maintaining all of the different dimensions/styles that may or may not have disabled navigation (such as the currently selected answer).
I hope this helps someone!
My solution was to disable translucent navigation in landscape mode. You still get a translucent status bar, but it fixes the issue where the nav bar is opaque and overlaps in landscape.
res/values-v19/styles.xml (these values are in my theme)
<item name="android:windowTranslucentStatus">true</item> <item name="android:windowTranslucentNavigation">@bool/translucentNavBar</item>
res/values-v19/bools.xml (enable translucent nav)
res/values-land-v19/bools.xml (disable translucent nav in landscape)
res/values-sw600dp-land-v19/bools.xml (enable translucent nav for tablets in landscape)
I’ve run into similar problems, but I wasn’t using a ViewPager, so this may not work. I solved by doing some nesting as Gabe described: only the inner ViewGroup is fitSystemWindows=”true”, the outer ViewGroup which displays the background is therefore allowed to extend under the translucent bars. Then I’m also calling requestFitSystemWindows() on orientation changes. Again, your ViewPager might be complicating things and my solution won’t work, but I hope this helps.
Put another ViewGroup inside of the one with the image and fitsSystemWindows to true
You should wrap this inside an Linear Layout and set
fitsSystemWindows to true.