Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FLAG_TRANSLUCENT_NAVIGATION not available on landscape mode?

This issue is similar to Check if translucent navigation is available but not quite it. I have a Nexus 4 flashed with CyanogenMod 11 or Android 4.4 equivalent and any app running in landscape mode with FLAG_TRANSLUCENT_NAVIGATION does not feature the translucency on the system UI like in portrait mode.

The same issue can be reproduced on Nexus 5 as I have not seen any google app built for Android 4.4 in Landscape mode with translucent buttons.

This is the code that I'm using

int API_LEVEL =  android.os.Build.VERSION.SDK_INT;

if (API_LEVEL >= 19)
{
    getWindow().addFlags( WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION );     
}

And while the window surface gets larger (and unusable) there is no translucency.

So the question is, do I have to do anything extra to make it work in landscape mode ? or is this an Android bug ?

like image 971
RelativeGames Avatar asked Nov 10 '13 23:11

RelativeGames


1 Answers

I don't know if they intend to change the behavior, but it seems deliberate. Ever since FLAG_TRANSLUCENT_NAVIGATION was introduced in Android Kitkat, "phone"-sized devices have always had an opaque black navigation bar on the right side of the screen in landscape. Even at the time of this post, there is a new flag in Android Lollipop (FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS) which has the same behavior, no matter what was passed to setStatusBarColor()).

Here is some rough code you could use to know when the navigation bar style is out of your control.

class MyActivity extends Activity {
    // ...
    boolean isNavigationForcedBlack() {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
            return true;
        }
        final int windowFlags = getWindow().getAttributes().flags;
        int navControlFlags = WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            navControlFlags |= WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS;
        }
        if ((windowFlags & navControlFlags) == 0) {
            return true;
        }

        boolean deviceHasOpaqueSideLandscapeNav = getDeviceSmallestWidthDp() < 600;
        boolean isLandscape = getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE;

        return deviceHasOpaqueSideLandscapeNav && isLandscape;
    }
    DisplayMetrics dm = new DisplayMetrics();
    float getDeviceSmallestWidthDp() {
        getWindowManager().getDefaultDisplay().getRealMetrics(dm);
        float widthDp = dm.widthPixels / dm.density;
        float heightDp = dm.heightPixels / dm.density;
        return Math.min(widthDp, heightDp);
    }
}
like image 180
bkDJ Avatar answered Sep 19 '22 17:09

bkDJ