BottomNavigationView height issue when changing status bar transparency in Android app material 3

75 views Asked by At

I'm working on an Android app where I want to change the status bar transparency in a DetailFragment. I've implemented a function transparentStatusBar to achieve this. When I call transparentStatusBar(true) in the DetailFragment, the status bar becomes transparent, but when I press the return button to go back to the previous fragment, the height of the bottomNavigationView gets messed up.

Here's the code for the transparentStatusBar function:

fun Activity.transparentStatusBar(transparent: Boolean) {
    if (transparent) {
        WindowCompat.setDecorFitsSystemWindows(window, false)
        window.statusBarColor = Color.TRANSPARENT
    } else {
        WindowCompat.setDecorFitsSystemWindows(window, true)
        window.statusBarColor = Color.parseColor("#F8FDFF")
    }
}

And in the DetailFragment, I'm calling it like this:

requireActivity().transparentStatusBar(true)

Here's a before and after screenshot of the bottomNavigationView:

bottomNavigationView before

bottomNavigationView next

It seems that setting WindowCompat.setDecorFitsSystemWindows(window, false) is causing the issue. How can I prevent this issue and maintain the correct bottomNavigationView height when changing the status bar transparency in the DetailFragment?

Any insights or suggestions would be greatly appreciated. Thank you!

1

There are 1 answers

0
Jing Liu On

I had the same issue as you after changing the status bar and navigation bar transparent, and I solved this problem by reducing the bottom padding of bottomNaviagtionView.

binding.bottomNavigationView.post(() -> {
            int start = binding.bottomNavigationView.getPaddingStart();
            int top = binding.bottomNavigationView.getPaddingTop();
            int end = binding.bottomNavigationView.getPaddingEnd();
            int bottom = binding.bottomNavigationView.getPaddingBottom();
            binding.bottomNavigationView.setPadding(start, top, end, bottom - getNavigationBarHeight(this));
        });

here is the method to get navigation bar height.

public int getNavigationBarHeight() {
    int navigationBarHeight = 0;
    int resourceId = getResources().getIdentifier("navigation_bar_height", "dimen", "android");
    if (resourceId > 0) {
        navigationBarHeight = getResources().getDimensionPixelSize(resourceId);
    }
    return navigationBarHeight;
}