0

I want to set the status bar to translucent on devices running MIUI. I can't find a solution to this problem.

I've tried doing it like this:

activity.window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLCUENT_STATUS, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)

and similarly when I want to remove the translucent status bar

activity.window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)

It works on most devices (I haven't tried all of them) but it doesn't work on MIUI devices. I've tried it on Xiaomi Redmi Note 4 running Android 7.0 (Nougat). Transparent status bar works normally, but translucent doesn't.

srjhnd
  • 189
  • 1
  • 10

2 Answers2

0

On Android M and above you can use the following code to get transparent status with dark icons

window.statusBarColor = ContextCompat.getColor(this, android.R.color.transparent)
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
    window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
    window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_STABLE or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR

Or if you need transparent status with white icons just remove LIGHT_STATUS_BAR flag from last line

cobe_tech
  • 176
  • 6
  • The problem im facing is TRANSLUCENT not transparent status bar only on devices running MIUI, I know how to solve this problem on other devices. – jakub ledwon Aug 19 '19 at 08:02
  • From what we experienced MIUI devices doesn't support translucent status just like other Android devices do. So that is why we added light status bar with dark icons on Android M and above devices and below Android 6 translucent with white icons. Most of the Xiaomi devices are android 6+ so this solved issues with status bar. Here is post: http://en.miui.com/thread-520993-1-1.html – cobe_tech Aug 19 '19 at 08:40
  • Unfortunately I really need it to be translucent because I have dynamically changing banner as top delegate of my recycler view, so no matter what color icons are, with some banners it will still be not possible to see them. Miui doesn't support translucent flags from what I experienced, but you can see this workaround below i've found it seems to work fine for me. – jakub ledwon Aug 19 '19 at 09:50
0

To make it work on all devices you have to do :

fun setTranslucent(activity : Activity){
    activity.window.statusBarColor = 0x66000000
}

fun clearTranslucent(activity : Activity){
    activity.window.statusBarColor = Color.TRANSPARENT
}

this code works on devices running MIUI as well. I don't know why flags won't work, may be it's because MIUI devices draw status bar in some strange way. If you know why I'd be more than happy to hear it :)

  • If you have view with content that scrolls, do you see it below status bar with this solution? – cobe_tech Aug 19 '19 at 08:46
  • Yes, I do, although to accomplish it, you have to work with window insets a lot. I recommend this talk from Chris Banes, it helped me understand how status bar in android works : https://chris.banes.dev/talks/2017/becoming-a-master-window-fitter-lon/?source=post_page--------------------------- and after you watch it read this : https://chris.banes.dev/2019/04/12/insets-listeners-to-layouts/ – jakub ledwon Aug 19 '19 at 09:52
  • This will set status bar color and it isn't actually translucent with this approach. But as you said you worked with window insets to accomplish it and if this works for you then great. – cobe_tech Aug 19 '19 at 09:59
  • I agree that it isn't actually translucent, it's just a work around, I'd really prefer it to be done with FLAG_TRANSLUCENT_STATUS. But afaik and as you said it just doesn't work with MIUI :( – jakub ledwon Aug 19 '19 at 10:02