8

I am using the following layout (main_activity.xml)

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

<android.support.v4.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/text_margin"
        android:fitsSystemWindows="true"
        android:text="@string/large_text" />
</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>

With the following styles-v21

<resources>
<style name="AppTheme.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
    <item name="android:windowDrawsSystemBarBackgrounds">true</item>
    <item name="android:statusBarColor">@android:color/transparent</item>
    <item name="android:windowTranslucentStatus">true</item>
    <item name="android:windowTranslucentNavigation">true</item>
</style>
</resources>

But I still cannot archive a transparent status bar

It look like this

Non-Transparent

But I want something like this (New Google Newsstand App)

Transparent status bar

Note: I am using

compile 'com.android.support:appcompat-v7:25.1.1'
compile 'com.android.support:support-v4:25.1.1'
compile 'com.android.support:design:25.1.1'
Allan Veloso
  • 5,823
  • 1
  • 38
  • 36
Shifatul
  • 2,277
  • 4
  • 25
  • 37
  • check this [link](http://stackoverflow.com/questions/27856603/lollipop-draw-behind-statusbar-with-its-color-set-to-transparent) – Megha Maniar Feb 18 '17 at 09:38

6 Answers6

31

try this:

add this in your app theme:

<item name="android:windowTranslucentStatus">true</item>
<item name="android:windowTranslucentNavigation">true</item>

Set android:fitsSystemWindows=”true” to root container

Now in your activity onCreate():

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

Updated, Result:

enter image description here

you can make the snackbar draw on top of screen:

Snackbar snack = Snackbar.make(parentLayout, str, Snackbar.LENGTH_LONG);
View view = snack.getView();
FrameLayout.LayoutParams params =(FrameLayout.LayoutParams)view.getLayoutParams();
params.gravity = Gravity.TOP;
view.setLayoutParams(params);
snack.show();

For more info see here

Community
  • 1
  • 1
rafsanahmad007
  • 23,683
  • 6
  • 47
  • 62
6
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Window window = getWindow();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            window.setStatusBarColor(Color.TRANSPARENT); } }

}

Use this in your activity. But changing the status bar color is only allowed after android L

Arpan Sharma
  • 2,142
  • 11
  • 22
4

The code below is making the status bar transparent but navigation bar opaque for API21+.

NOTE: you shouldn't be setting android:fitsSystemWindows="true".

override fun onCreate(savedInstanceState: Bundle?) {
      setupContentWindow()
      super.onCreate(savedInstanceState)
      setContentView(R.layout.activity_main)
}

fun setupContentWindow() {
    window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
    window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
    window.statusBarColor = Color.TRANSPARENT
    window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
}
dgngulcan
  • 3,101
  • 1
  • 24
  • 26
1

Add these two lines in the onCreate of the activity that you want its layout status bar to be transparent

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
getWindow().setStatusBarColor(Color.TRANSPARENT);
}

No need to change your app theme or xml layout. In doing this your bar doesn't have to be transparent throughout your whole app if you don't want to.

  • This is the correct answer. All other answers mistaken "transparent status bar" for "no status bar". – nibbana Feb 18 '19 at 10:57
  • It should be like `if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { getWindow().setStatusBarColor(Color.TRANSPARENT); }` because `setStatusBarColor` requires minimum lollipop version – androidXP May 04 '19 at 08:52
1

It seems some people have grey status bar instead of transparent. To avoid this problem do the next in your activity:

override fun onCreate(savedInstanceState: Bundle?) {
    ...
    window.apply {
        clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
        addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
        statusBarColor = Color.TRANSPARENT
        decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
    }
}
txqe
  • 85
  • 1
  • 5
-3

Please remove this line from your CoordinatorLayout:

android:fitsSystemWindows="true"

Edited, Result:

enter image description here

Shifatul
  • 2,277
  • 4
  • 25
  • 37
Rajkumar Kumawat
  • 290
  • 2
  • 11