I have a problem with a layout file in my Android Project.
It works perfectly in Android <= 13, but now in Android 15 the layout goes beyond the normal bounds of the touchscreen, and invades the notification/navigation bar, where touch actions do not work. So the top part of my app screen is not accessible.
Android Studio gives me this warning, that seems to be related to my problem:
"TwoLineListItem is partially hidden in layout because it is not contained within the bounds of its parent in a preview configuration. Fix this issue by adjusting the size or position of TwoLineListItem."
But there is no TwoLineListItem in my layout, nor anywhere in my project...
This is the beginning of my layout file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_vertical"
android:orientation="vertical">
<View style="@style/Separator" />
...
EDIT
I was unable to make the code in the page suggested by CommonsWare to work. Yet, this seems to work fine (after setContentView
):
fixTopPaddingAndroid15(getWindow(),findViewById(android.R.id.content));
where the function fixTopPaddingAndroid15
is given by:
public static void fixTopPaddingAndroid15(@NonNull Window w, @NonNull View v) {
if (android.os.Build.VERSION.SDK_INT >= 35) {
View decorView = w.getDecorView();
decorView.post(() -> {
WindowInsetsCompat insets = ViewCompat.getRootWindowInsets(decorView);
if (insets != null) {
ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) v.getLayoutParams();
params.topMargin = insets.getInsets(WindowInsetsCompat.Type.systemBars()).top;
v.setLayoutParams(params);
}
});
}
}
By CommonsWare's advice, I ended up fixing the margins for good with the following code:
public static void fixPaddingAndroid15(@NonNull Window w, @NonNull View v) {
if (android.os.Build.VERSION.SDK_INT < 35) return;
View decorView = w.getDecorView();
decorView.post(() -> {
WindowInsetsCompat insets = ViewCompat.getRootWindowInsets(decorView);
if (insets != null) {
ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) v.getLayoutParams();
params.topMargin = insets.getInsets(WindowInsetsCompat.Type.systemBars()).top;
params.bottomMargin = insets.getInsets(WindowInsetsCompat.Type.systemBars()).bottom;
params.leftMargin = insets.getInsets(WindowInsetsCompat.Type.systemBars()).left;
params.rightMargin = insets.getInsets(WindowInsetsCompat.Type.systemBars()).right;
v.setLayoutParams(params);
}
});
}
Then, I call this inside onCreate
and after setContentView
through the line:
fixPaddingAndroid15(getWindow(),findViewById(android.R.id.content));