본문 바로가기
모바일개발(Mobile Dev)/안드로이드개발(Android)

about status bar transparency

by 테크한스 2016. 9. 15.
반응형

android – Status bar transparency doesn't work

6

I am trying to make status bar transparent in my application using this code:

getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
getWindow().setStatusBarColor(Color.TRANSPARENT);

But it doesn’t work. Status bar just change a color and became grey, not transparent. Where is my mistake? How to make status bar transparent?

TOP ANSWERS

Answer # 1
You can maybe check one of my previous answers: stackoverflow.com/a/33986302/3426717
 

Added by Andrei

Answer # 2
No, it doesn't work.
 

class=”comment-user owner”>BArtWell

Answer # 3
Tests are being conducted on devices +19? What's the final result using this solution?
 

Added by Ismael Di Vita

Answer # 4
Yes, Nexus 5 with Android 5.1.1. Status bar became grey, but not transparent.
 

class=”comment-user owner”>BArtWell

Answer # 5
WithgetWindow().setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);it works. Thank you!
 

class=”comment-user owner”>BArtWell

Answer # 6
Same result: status bar became grey, but not transparent.
 

class=”comment-user owner”>BArtWell

OTHER ANSWERS

Answer # 7

Try this in your styles.xml(v21)

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

Update

You can achieve the same effect programatically on KitKat and afterwards by setting this Window flag

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
Window w = getWindow(); // in Activity's onCreate() for instance
w.setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
}

If you set a background resource (like a color or a picture) to your layout, you will see the color or picture “below” the status bar.

<item name="android:windowDrawsSystemBarBackgrounds">true</item>
<item name="android:statusBarColor">@color/primary_dark</item>

Source

Answer # 8

try calling this from onCreate:

getWindow().getDecorView().setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);

getWindow().setStatusBarColor(Color.TRANSPARENT);



I want to have appcompat v21 toolbar in my activity. But the toolbar I'm implementing is overlapping below status bar. How can I fix it?

overlapping toolbar

Here is the activity layout xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <include
        android:id="@+id/toolbar"
        layout="@layout/toolbar" />

    <FrameLayout
        android:id="@+id/container"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />
</LinearLayout>

Toolbar view:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="?attr/actionBarSize"
    android:background="?attr/colorPrimary" />

Theme style:

<style name="AppTheme" parent="MaterialNavigationDrawerTheme.Light.DarkActionBar">
    <item name="colorPrimary">@color/primary</item>
    <item name="colorPrimaryDark">@color/primary_dark</item>
    <item name="colorAccent">@color/accent</item>
</style>
shareimprove this question
   
How did you customize your Theme of your applicaiton? post them. – SilentKnight Apr 20 '15 at 2:46

Use android:fitsSystemWindows="true" in the root view of your layout (LinearLayout in your case). And 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.

shareimprove this answer
   
Thanks. This works. But I was wondering if there is a way to do this globally? – AimanB Apr 20 '15 at 2:49
11 
@AimanB you can set it globally by setting in the theme as <item name="android:fitsSystemWindows">true</item> – Samir Jun 17 '15 at 12:08 
7 
@Samir this has a side-effect as reported in this comment – Sufian Sep 2 '15 at 13:12
   
By default, your application should respect the system windows. You might open your activity with some flags perhaps ? – zgulser Apr 14 at 11:54
2 
This issue will come even after we added android:fitsSystemWindows="true" to root layout, If your style contains <item name="android:statusBarColor">@android:color/transparent</it‌​em> this. Just mentioned for those who are reading this question. – Sakkeer Hussain May 27 at 15:48

Just set this to v21/styles.xml file

 <item name="android:windowDrawsSystemBarBackgrounds">true</item>
 <item name="android:statusBarColor">@color/colorPrimaryDark</item>

and be sure

 <item name="android:windowTranslucentStatus">false</item>
shareimprove this answer
   
I tried this but not working @Zahra.HY – TheHunter Jun 29 at 10:49
   
if you have something like app:layout_behavior="@string/appbar_scrolling_view_behavior" ,remove it! @TheHunter – Zahra.HY Jun 29 at 11:02 
   
pastie.org/10894050 @Zahra.HY have a look on my v21 style file – TheHunter Jun 29 at 11:09
   
set <item name="android:windowTranslucentStatus">false</item> – Zahra.HY Jun 29 at 11:14

For me, the problem was that I copied something from an example and used

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

Removing this fixed the problem.

shareimprove this answer
   
This one worked for me, thanks for your answer – blueware Aug 28 at 11:54

I removed all lines mentioned below from /res/values-v21/styles.xml and now it is working fine.

 <item name="android:windowDrawsSystemBarBackgrounds">true</item>
 <item name="android:statusBarColor">@android:color/transparent</item>


 <item name="windowActionBar">false</item>
 <item name="android:windowDisablePreview">true</item>

 <item name="windowNoTitle">true</item>

 <item name="android:fitsSystemWindows">true</item>
shareimprove this answer

None of the answers worked for me, but this is what finally worked after i set android:fitSystemWindows on the root view(I set these in styles v21):

<item name="android:windowDrawsSystemBarBackgrounds">true</item>
<item name="android:windowTranslucentStatus">false</item>

Make sure you don't have the following line as AS put it by default:

<item name="android:statusBarColor">@android:color/transparent</item>
shareimprove this answer


반응형