0

I want to achieve a transparent status bar in pre lollipop and above lollipop as well. My minimumSdkLevel is 19. I have tried the following method to achieve transparent statusbar. But the toolbar is going underneath statusbar.

Here is the screenshot (This is a Full screen Fragment with Toolbar on it):

**This is a Full screen Fragment**

Layout of the above screenshot:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clickable="true"
    app:cardCornerRadius="3dp"
    android:fitsSystemWindows="true"
    app:cardElevation="3dp">

    <RelativeLayout
        android:id="@+id/gamesLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <android.support.design.widget.AppBarLayout
            android:id="@+id/toolbarLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/AppTheme.AppBarOverlay">

            <android.support.v7.widget.Toolbar
                android:id="@+id/gameToolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                app:popupTheme="@style/AppTheme.PopupOverlay" />

        </android.support.design.widget.AppBarLayout>

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@+id/toolbarLayout">

            <ImageView
                android:layout_width="200dp"
                android:layout_height="200dp"
                android:adjustViewBounds="true"
                android:alpha="0.3"
                android:scaleType="fitXY"
                android:src="@drawable/background_img" />

            <ImageView
                android:layout_width="200dp"
                android:layout_height="200dp"
                android:layout_alignParentBottom="true"
                android:layout_alignParentRight="true"
                android:adjustViewBounds="true"
                android:alpha="0.3"
                android:rotation="180"
                android:scaleType="fitXY"
                android:src="@drawable/background_img" />

            <android.support.v7.widget.RecyclerView
                android:id="@+id/gameList"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_margin="10dp" />
        </RelativeLayout>
    </RelativeLayout>
</android.support.v7.widget.CardView>

Here is my styles.xml:

<!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="android:colorBackgroundCacheHint">@null</item>
        <item name="android:windowTranslucentStatus">true</item>
        <item name="android:windowTranslucentNavigation">true</item>
        <item name="android:windowAnimationStyle">@android:style/Animation</item>
    </style>

Here is my v21/styles.xml :

<!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="android:colorPrimary">@color/colorPrimary</item>
        <item name="android:colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="android:colorAccent">@color/colorAccent</item>
        <item name="android:windowContentTransitions">true</item>
        <item name="android:windowTranslucentStatus">true</item>
        <item name="android:windowDrawsSystemBarBackgrounds">true</item>
        <item name="android:windowTranslucentNavigation">true</item>
    </style>

I have even added android:fitsSystemWindows="true" to the root layout of the Activities and Fragments.

I have added this below code as well in the Activities and Fragments:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            Window window = getWindow();
            window.setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
        }

Why I am not getting transparent Statusbar and why Toolbar is going underneath Statusbar?

sagar suri
  • 4,351
  • 12
  • 59
  • 122

4 Answers4

0

in default style.xml

<!-- Base application theme. -->
<style name="BGTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
</style>

<style name="BGTheme.ActionBar.Transparent" parent="BGTheme">
    <item name="android:windowContentOverlay">@null</item>
    <item name="windowActionBarOverlay">true</item>
    <item name="windowActionBar">false</item>
    <item name="colorPrimary">@android:color/transparent</item>
    <item name="android:textColorSecondary">@color/white</item>
</style>

<style name="BGTheme.ActionBar.Transparent.AppBarOverlay" 
    parent="ThemeOverlay.AppCompat.Dark.ActionBar" />

<style name="BGTheme.ActionBar.Transparent.PopupOverlay" 
    parent="ThemeOverlay.AppCompat.Light" />

and in your v-21 style.xml

<style name="BGTheme.ActionBar.Transparent" parent="BGTheme">
    <item name="colorPrimary">@android:color/transparent</item>
</style>

Note: Add this in your root layout android:fitsSystemWindows="true"

Nirmal Shethwala
  • 268
  • 1
  • 15
0

remove if statment

not work below 21
getWindow().setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);

Keren Caelen
  • 1,466
  • 3
  • 17
  • 38
AsAd KhAn
  • 11
  • 3
-2

add flowing lines in your 'style.xml'

    <item name="android:windowContentOverlay">@null</item>
    <item name="windowActionBarOverlay">true</item>
    <item name="windowActionBar">false</item>
    <item name="colorPrimary">@android:color/transparent</item>
Nirmal Shethwala
  • 268
  • 1
  • 15
-2

EDIT:

try Using the following Line in your Base View.

android:fitsSystemWindows="true"

as it is mention also android:fitsSystemWindows is an

internal attribute to adjust view layout based on system windows such as the status bar. If true, adjusts the padding of this view to leave space for the system windows. Will only take effect if this view is in a non-embedded activity.

Must be a boolean value, either "true" or "false".

This may also be a reference to a resource (in the form "@[package:]type:name") or theme attribute (in the form "?[package:][type:]name") containing a value of this type.

This corresponds to the global attribute resource symbol fitsSystemWindows.

You can also use it globally by using the below line in your theme

<item name="android:fitsSystemWindows">true</item>

I hope it helps. It works in my Project.

Sahil
  • 1,399
  • 9
  • 11
  • By this solution you must write this in every activity. Will generate a bunch of repeated code – GuilhermeFGL Aug 30 '17 at 14:21
  • I tried..didn't work for me. can you post the whole solution whatever worked for you. @Sahil – sagar suri Aug 31 '17 at 14:17
  • hey remove this line if you have this line in your themes @android:color/transparent and also add the following and try please once more. true false @Hiren removing the translucent status will make it work,but i think thats your primary need in your app? Right ? – Sahil Aug 31 '17 at 14:28