27

I know in Android 4.4 KitKat (API 19) it's possible to make the status bar transparent.

But for example, Go Launcher Ex and others have an option to make it transparent, and it's working on pre KitKat also, for me (Android 4.3 (Jelly Bean)) too and also on my Android 4.0 (Ice Cream Sandwich) device.

I didn't need root or any special privileges to use it.

Download Go Launcher Ex and try it yourself. For me it would also be OK if I would need root.

But how did they do that? How can I make Android's status bar translucent on pre-KitKat devices?

---To clarify things---

I'm not talking about the Action Bar; I mean the Status Bar (Notification Bar)!


see these example pictures:

(Notice, this is taken from my Stock Galaxy Note 3 with Android 4.3 and Go Launcher Ex.)

(The same thing works with my Galaxy S2 and Android 4.0 too.)

Without a transparent status bar:

Without transparent bar (default)

With transparent status bar enabled: (I want to achieve this on pre 4.4 (API 19) devices)

With transparent status bar (what I want to achieve on pre 4.4 devices in any way

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
bricklore
  • 4,125
  • 1
  • 34
  • 62
  • Can you provide an image of what you are trying to accomplish? – Austyn Mahoney Feb 24 '14 at 19:42
  • @AustynMahoney sure, ill add one right now – bricklore Feb 24 '14 at 19:43
  • @AustynMahoney added pics, hope they help to identify my problem :) – bricklore Feb 24 '14 at 19:57
  • You can do it the other way around: Using system window service, insert a transparent LinearLayout over the status bar and draw the background image at 50% transparency. Of course, this solution does not achieve 100% the same result you wish for as it's the background that gets drawn last. Items on the statusbar will not be clear and statusbar black background will clearly affect the image being drawn. – velis Feb 25 '14 at 12:34
  • @velis I really want to achieve a transparent staus bar not a fake – bricklore Feb 25 '14 at 17:32
  • @MalteSchmitz, Are you building an application with a full screen even the status bar OR building operating system!! – Sami Eltamawy Feb 25 '14 at 17:42
  • @AbdEl-RahmanEl-Tamawy i'm building an android application - once again i will lead you to `Go Launcher Ex` – bricklore Feb 25 '14 at 17:43
  • @MalteSchmitz, You want to Hide the notification bar with its items OR make its background transparent? – Sami Eltamawy Feb 25 '14 at 17:45
  • 1
    @AbdEl-RahmanEl-Tamawy I think my question wasn't that confusing or ambigious, please re-read it - (i want to make it transparent - see the second picture in my post) – bricklore Feb 25 '14 at 17:46
  • Just a guess but those custom launchers may hide the status bar, and draw their own custom implementation. – Austyn Mahoney Feb 25 '14 at 19:46
  • 1
    @AustynMahoney they don't do. it would be nearly impossible to do that and despite of that you can verify that they are not in fullscreen mode with some system tools (in developer settings) – bricklore Feb 25 '14 at 19:48

2 Answers2

21

I discovered how to get a transparent status bar on Samsung and Sony devices
running at least Android 4.0.

To start off let me explain why it isn't possible on most devices:
From the official side of Android, there is no solution until KitKat (4.4).
But some device manufacturers like Samsung and Sony have added this option in their proprietary software.

Samsung uses their own skinned version of Android, called TouchWiz, and Sony is using their own system too.
They have a own implementation of the View class with additional SYSTEM_UI_ flags that allow to enable a transparent statusbar on pre 4.4 devices!

But the only way to prove that those flags are existing and to access them is using Reflection.
Here is my solution:


Resolve the view flag:

int ResolveTransparentStatusBarFlag()
{
    String[] libs = getPackageManager().getSystemSharedLibraryNames();
    String reflect = null;

    if (libs == null)
        return 0;

    for (String lib : libs)
    {
        if (lib.equals("touchwiz"))
            reflect = "SYSTEM_UI_FLAG_TRANSPARENT_BACKGROUND";
        else if (lib.startsWith("com.sonyericsson.navigationbar"))
            reflect = "SYSTEM_UI_FLAG_TRANSPARENT";
    }

    if (reflect == null)
        return 0;

    try
    {
        Field field = View.class.getField(reflect);
        if (field.getType() == Integer.TYPE)
            return field.getInt(null);
    }
    catch (Exception e)
    {
    }

    return 0;
}

Apply it on the decorView of your Window (inside any Activity):

void ApplyTransparentStatusBar()
{
    Window window = getWindow();
    if (window != null)
    {
        View decor = window.getDecorView();
        if (decor != null)
            decor.setSystemUiVisibility(ResolveTransparentStatusBarFlag());
    }
}

I think there are maybe other device manufacturers that have implemented such a flag
but I was only able to figure this two out (because I own a Samsung and a Sony device)

bricklore
  • 4,125
  • 1
  • 34
  • 62
  • Nice find. How did you figure it out? – Juan Andrés Diana Feb 27 '14 at 01:29
  • 4
    i decompiled the launchers of samsung and sony which had that feature and there i figured out that those flags were existing – bricklore Feb 27 '14 at 05:19
  • 1
    I'm guessing you decompiled Nova Launcher in addition to (or instead of) the Samsung and Sony launchers... You don't need to search the system libraries, simply using reflection to test both flags would be more straightforward and probably faster as calls through PackageManager tend to be slow (getSystemSharedLibraryNames). – Kevin TeslaCoil Feb 28 '14 at 06:45
  • However Google Play Edition devices will have the View SYSTEM UI flags, but they won't do anything. These devices will not have the shared libraries. Nova checks the libraries to disable the option on unsupported devices. – Kevin TeslaCoil Feb 28 '14 at 06:53
  • thanks i will have a look at nova and see if i can imprive the answer – bricklore Feb 28 '14 at 07:33
  • Don't you changed the name of the method from ResolveTransparentStatusBarFlag() to GetTransparentStatusBarFlag() inside the code? – yshahak Jun 01 '15 at 10:09
  • Oh you're right, I missed that, now it should be correct :) – bricklore Jun 01 '15 at 10:15
2

Use this. :)

after super() and before set content.

if (Build.VERSION.SDK_INT >= 19)
{
    getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
}

if (Build.VERSION.SDK_INT >= 19 && Build.VERSION.SDK_INT < 21)
{
    setWindowFlag(this, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, true);
}

if (Build.VERSION.SDK_INT >= 21)
{
    setWindowFlag(this, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, false);
    getWindow().setStatusBarColor(Color.TRANSPARENT);
}


public static void setWindowFlag(Activity activity, final int bits, boolean state)
{
    Window win = activity.getWindow();
    WindowManager.LayoutParams winParams = win.getAttributes();

    if (state)
     {
        winParams.flags |= bits;
    }
     else
     {
        winParams.flags &= ~bits;
    }
}
Ali Bagheri
  • 3,068
  • 27
  • 28