9

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 ?

Community
  • 1
  • 1
RelativeGames
  • 1,593
  • 2
  • 18
  • 27

2 Answers2

3

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);
    }
}
bkDJ
  • 750
  • 7
  • 10
-1

You can see if the translucent effect is available on the current device using following code :

int id = getResources().getIdentifier("config_enableTranslucentDecor", "bool", "android");
if (id != 0 && getResources().getBoolean(id)) { // Translucent available
    Window w = getWindow();
    w.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION, WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
    w.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
}

With this code, you can check if the device can use the FLAG_TRANSLUCENT_NAVIGATION and FLAG_TRANSLUCENT_STATUS flags. Then, you have to say that you want Actionbar, notifications bar and soft buttons bar to overlap your layout (to be called before setContentView(R.layout.yourlayout); in onCreate()):

getWindow().requestFeature(Window.FEATURE_ACTION_BAR_OVERLAY);

Don't forget to add some padding to your content to avoid it being unreachable since it would be 'hidden' by those elements. In order to achieve this, you can add android:fitsSystemWindows="true" attribute on your main container in your layout.

Quentin S.
  • 1,462
  • 20
  • 18
  • But it is available on a Nexus 4. I tested your code, it's available, have you ever seen the translucent effect in Landscape though ? Cause I never. – RelativeGames Dec 31 '13 at 18:30
  • Yes, translucent effect is also available on landscape mode. For example, Nova Launcher uses it (translacent has to be enabled manually). I don't think that you have to enable it manually, do you? – Quentin S. Jan 02 '14 at 09:41
  • Just installed it, forced landscape on the home screen, no translucency. How is translucency enabled manually ? What device are you using btw ? – RelativeGames Jan 02 '14 at 21:42
  • I'm using the Nexus 7. Actually, Nova Launcher added it with its beta version, so I don't know if it is yet available in the main version. Anyway, I'm using the translucent effect in one of my apps (Dikkenek Soundboard, available for free on Google Play). The translucent effect is available with the notifications bar, but indeed not on the vertical side buttons bar (on the right of the screen). I'm using the code above in order to make it work. – Quentin S. Jan 03 '14 at 19:24
  • Well, we're only talking the vertical side bar as you call it, that's the "navigation" bar. I also doubt it's available in the launcher but not in an app. I'll try installing the Nova Launcher soon. – RelativeGames Jan 04 '14 at 13:13
  • 1
    On tablets, the navigation bar (with back, home and apps stack buttons) is horizontal and placed on the lower side of the screen. In this case, it is possible to apply the translucent effect on it. On smartphones, the navigation bar is vertical and placed on the right side of the screen. In this special case, the translucent effect is not available. (since developpers would have to handle 3 different padding) – Quentin S. Jan 04 '14 at 16:21
  • Where does it say it's not available on phones + landscape ? This is the inferred behavior, but I haven't seen it being documented. I'd be totally ok with making my phone's navigation bar appear on the bottom side in landscape though. – RelativeGames Jan 05 '14 at 18:54