Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trouble getting systemBars Insets on Android 11+

Tags:

android

I'm fairly new to Android, and I've been experimenting with edge-to-edge content as described in this guide just making it work with a basic RecyclerView that scrolls behind a transparent navigation bar. As the guide instructs, I use window insets to adjust padding and prevent overlap:

ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.layout_root), (v, windowInsets) -> {
            Insets insets = windowInsets.getInsets(WindowInsetsCompat.Type.systemBars());
            Log.d("DEBUG", "Insets: " + insets.toString());
            v.setPadding(0, insets.top, 0, insets.bottom);
            return WindowInsetsCompat.CONSUMED;
        });

Everything works perfectly on Android 10, but the top inset always returns 0 when testing on Android 11 or 12. So far I've found a lot about changes to Window Insets in Android 11, but I can't find anything pertaining to this particular issue.

like image 711
Zach Bearse Avatar asked Feb 02 '26 14:02

Zach Bearse


1 Answers

I faced the same issue as well. It always returned 0.

I fixed my issue with the following:

int insetTypes = WindowInsetsCompat.Type.displayCutout() | WindowInsetsCompat.Type.systemBars();
Insets insets = windowInsets.getInsets(insetTypes);
like image 133
Eric Cen Avatar answered Feb 04 '26 03:02

Eric Cen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!