Search code examples
androidstatusbarnavigationbar

android - Transparent Navigation Bar but not Status Bar


I am not sure if it is possible but I am trying to make the Navigation Bar transparent without making the Status Bar transparent. The reason for the later is I have a Toolbar that would otherwise draw behind the status bar.

Right now I am using a fakeStatusBar Layout that displays before the status bar so everything looks good to the user:

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){
        Window window = getWindow();
        window.setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);

        //now the status bar is transparent too, which we have to fix

        //first we get status bar height
        int statusBarHeight = 0;
        int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            statusBarHeight = getResources().getDimensionPixelSize(resourceId);
        }

        //then we get the full width
        Display display = getWindowManager().getDefaultDisplay();
        Point size = new Point();
        display.getSize(size);
        int screenWidth = size.x;

        //then we set that height to the fake item in the layout
        LinearLayout fakeStatusBar = findViewById(R.id.fakeStatusBar);
        ViewGroup.LayoutParams params = fakeStatusBar.getLayoutParams();
        params.height = statusBarHeight;
        params.width = screenWidth;
        fakeStatusBar.setLayoutParams(params);

The only problem is when going to multitasking, the thumbnail does not look good because the toolbar is not where it should be.


Solution

  • Not exactly what I wanted but I found a workaround using Translucent Navigation Bar. The final code looks something like this:

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){
    
            //set navigation bar translucent
            Window window = getWindow();
            window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION, WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
            //now the status bar is translucent too, which we have to fix
    
            //first we get status bar height
            int statusBarHeight = 0;
            int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
            if (resourceId > 0) {
                statusBarHeight = getResources().getDimensionPixelSize(resourceId);
            }
    
            //then set it as top padding for the main layout
            ConstraintLayout mainLayout = (ConstraintLayout) findViewById(R.id.mainLayout);
            mainLayout.setPadding(0,statusBarHeight,0,0);
        }
    

    Where top padding is added to the main layout of the activity.

    If anyone knows how to do something similar with transparent navigation bar, perfect, but otherwise I am happy with my solution.