Edge-to-edge: aplicar insets de systemBars en todas las activities (fix UI tapada por topbar en Android 15+)

This commit is contained in:
kprkpr committed 2026-10-21 00:10:02 +02:00
1 parent a254432dd9
commit f88defa7f1
7 files changed
+84 -2

No files matched your search

@@ -13,6 +13,7 @@ public class AboutOfActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_about);
UiUtils.applySystemBarsInsets(this);
}
}
@@ -31,6 +31,7 @@ public class FavsActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_favs);
UiUtils.applySystemBarsInsets(this);
db = new DatabaseHelper(this);
final ListView listFavs = (ListView) findViewById(R.id.listSentences);
@@ -96,6 +96,7 @@ public class MainActivity extends AppCompatActivity implements RobotoCalendarLis
}
setContentView(R.layout.activity_main);
UiUtils.applySystemBarsInsets(this);
db = new DatabaseHelper(this);
txtfraseTitle = findViewById(R.id.txt_frasetitle);
txtfraseText = findViewById(R.id.txt_frasetext);
@@ -32,6 +32,7 @@ public class OptsActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_opts);
UiUtils.applySystemBarsInsets(this);
final Config conf = new Config(this);
/* Do variables of objects */
final Spinner spinnerHour = (Spinner) findViewById(R.id.spinnerHour);
@@ -0,0 +1,74 @@
package com.jkanetwork.st.calendariodefrases;
import android.app.Activity;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.core.view.WindowInsetsControllerCompat;
/**
* Edge-to-edge helpers.
*
* targetSdk 35+ forces edge-to-edge on Android 15+: the decor view spans the
* whole screen and the classic theme ActionBar no longer pushes the content,
* so without inset handling the UI draws behind the status bar and the
* ActionBar. Every activity must call {@link #applySystemBarsInsets(Activity)}
* right after setContentView.
*/
public final class UiUtils {
private UiUtils() {
}
/**
* Lays the activity out edge-to-edge on every Android version (uniform
* geometry on old and new phones): the root view keeps painting its
* background under the system bars, while its content is padded below
* them (status bar + ActionBar on top, gesture/nav bar on the bottom).
*
* Call once after setContentView, before any view lookup that depends
* on layout position.
*/
public static void applySystemBarsInsets(Activity activity) {
WindowCompat.setDecorFitsSystemWindows(activity.getWindow(), false);
// Apply the padding to the inflated layout root (child of content),
// so its background still covers the full screen under the bars.
ViewGroup content = activity.findViewById(android.R.id.content);
final View root = content.getChildCount() > 0 ? content.getChildAt(0) : content;
// The classic AppCompat ActionBar overlays the content in
// edge-to-edge: add its themed height to the top padding manually.
int actionBarSize = 0;
if (activity instanceof AppCompatActivity) {
ActionBar bar = ((AppCompatActivity) activity).getSupportActionBar();
if (bar != null) {
TypedValue tv = new TypedValue();
activity.getTheme().resolveAttribute(androidx.appcompat.R.attr.actionBarSize, tv, true);
actionBarSize = TypedValue.complexToDimensionPixelSize(tv.data,
activity.getResources().getDisplayMetrics());
}
}
// Light app background under the bottom bar -> dark system icons.
// The status bar sits over the dark ActionBar -> keep default light icons.
WindowInsetsControllerCompat controller =
WindowCompat.getInsetsController(activity.getWindow(), root);
controller.setAppearanceLightNavigationBars(true);
final int extraTop = actionBarSize;
ViewCompat.setOnApplyWindowInsetsListener(root, (v, windowInsets) -> {
Insets bars = windowInsets.getInsets(WindowInsetsCompat.Type.systemBars()
| WindowInsetsCompat.Type.displayCutout());
v.setPadding(bars.left, bars.top + extraTop, bars.right, bars.bottom);
return WindowInsetsCompat.CONSUMED;
});
}
}
+2 -2
View File
@@ -1,2 +1,2 @@
#Tue Oct 20 22:01:20 CEST 2026
VERSION_CODE=380
#Wed Oct 21 00:08:36 CEST 2026
VERSION_CODE=382