Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
194 views
in Technique[技术] by (71.8m points)

java - Transparent status bar - before Android 4.4 (KitKat)

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...