From 37a802705a861fe9a735e80536a7c75c6612053e Mon Sep 17 00:00:00 2001 From: Kevin Puertas Date: Wed, 21 Oct 2026 13:15:45 +0000 Subject: [PATCH] =?UTF-8?q?UI:=20UiUtils=20con=20insets=20edge-to-edge=20e?= =?UTF-8?q?stilo=20FrasesDeAnime,=20bottom=20nav=20compartida=20y=20banner?= =?UTF-8?q?=20adaptativo=20por=20c=C3=B3digo?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../st/calendariodefrases/UiUtils.java | 190 ++++++++++++++---- 1 file changed, 151 insertions(+), 39 deletions(-) diff --git a/app/src/main/java/com/jkanetwork/st/calendariodefrases/UiUtils.java b/app/src/main/java/com/jkanetwork/st/calendariodefrases/UiUtils.java index f4d6bf5..c7100b5 100644 --- a/app/src/main/java/com/jkanetwork/st/calendariodefrases/UiUtils.java +++ b/app/src/main/java/com/jkanetwork/st/calendariodefrases/UiUtils.java @@ -1,74 +1,186 @@ package com.jkanetwork.st.calendariodefrases; import android.app.Activity; -import android.util.TypedValue; +import android.content.Intent; +import android.content.res.Configuration; +import android.view.Gravity; import android.view.View; import android.view.ViewGroup; +import android.widget.FrameLayout; +import android.widget.ImageView; +import android.widget.TextView; -import androidx.appcompat.app.ActionBar; import androidx.appcompat.app.AppCompatActivity; +import androidx.core.content.ContextCompat; 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; +import com.google.android.gms.ads.AdRequest; +import com.google.android.gms.ads.AdSize; +import com.google.android.gms.ads.AdView; + /** - * Edge-to-edge helpers. + * Helpers compartidos del rediseño 2026-10 (mismo papel que App.kt en + * FrasesDeAnime, en Java): * - * 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. + * - Edge-to-edge insets (Android 15+ las fuerza con targetSdk 35+). + * - Barra de navegación inferior compartida. + * - Banner AdMob adaptativo creado por código. */ public final class UiUtils { private UiUtils() { } + /* ------------------------------------------------------------------ */ + /* Edge-to-edge */ + /* ------------------------------------------------------------------ */ + /** - * 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). + * Pinta la activity a pantalla completa y aplica los insets (status bar + + * barra de navegación) como padding del root, con el fondo extendiéndose + * por debajo de las barras. Llamar en onCreate tras setContentView. * - * Call once after setContentView, before any view lookup that depends - * on layout position. + * El rediseño oculta el ActionBar en todas las pantallas: [navColor] fija + * el color de la barra de navegación (bgCard) y [darkNavbar] ajusta el + * color de los iconos del sistema según el tema. */ - public static void applySystemBarsInsets(Activity activity) { + public static void applySystemBarsInsets(AppCompatActivity activity, View root, + int navColor, boolean darkNavbar) { + boolean lightStatusIcons = !isDarkTheme(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); + controller.setAppearanceLightStatusBars(lightStatusIcons); + controller.setAppearanceLightNavigationBars(!darkNavbar); + + /* Barra de navegación: Android < 15 usa window.navigationBarColor; en + 15+ la barra hereda el fondo de la ventana, pero aplica un scrim de + contraste en el modo 3 botones que lo quita (API 29+). */ + activity.getWindow().setNavigationBarColor(navColor); + if (android.os.Build.VERSION.SDK_INT >= 29) { + activity.getWindow().setNavigationBarContrastEnforced(false); + } + + /* Franja al fondo del decor con el color de la barra, para el modo + gestos en 15+ (la barra hereda el fondo de la ventana). */ + final View navBackdrop = new View(activity); + navBackdrop.setBackgroundColor(navColor); + navBackdrop.setClickable(false); + ((ViewGroup) activity.getWindow().getDecorView()).addView(navBackdrop, + new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 0, Gravity.BOTTOM)); - 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); + v.setPadding(bars.left, bars.top, bars.right, bars.bottom); + navBackdrop.setLayoutParams(new FrameLayout.LayoutParams( + ViewGroup.LayoutParams.MATCH_PARENT, bars.bottom, Gravity.BOTTOM)); + navBackdrop.bringToFront(); return WindowInsetsCompat.CONSUMED; }); } + + /** ¿Tema oscuro del sistema? (para el color de los iconos del sistema) */ + public static boolean isDarkTheme(Activity activity) { + int mask = activity.getResources().getConfiguration().uiMode + & Configuration.UI_MODE_NIGHT_MASK; + return mask == Configuration.UI_MODE_NIGHT_YES; + } + + /* ------------------------------------------------------------------ */ + /* Barra de navegación inferior */ + /* ------------------------------------------------------------------ */ + + /** Pestañas de la barra inferior */ + public enum NavTab {INICIO, FAVORITOS, OPCIONES} + + /** + * Prepara la barra inferior (view_bottom_nav.xml) incluida en el layout: + * marca [active] con color accent (icono+texto) y lanza las demás pestañas. + * REORDER_TO_FRONT reutiliza la instancia existente de cada pantalla (sin + * recrear, el banner NO se recarga); NO_ANIMATION + overridePendingTransition(0,0) + * quita la animación lateral. Las pestañas no se cierran al navegar; las + * secundarias (Acerca de) sí. + */ + public static void setupBottomNav(final AppCompatActivity activity, NavTab active) { + int accent = ContextCompat.getColor(activity, R.color.accent); + int muted = ContextCompat.getColor(activity, R.color.colorTextMuted); + + styleTab(activity, R.id.nav_inicio, accent, muted, active == NavTab.INICIO); + styleTab(activity, R.id.nav_favoritos, accent, muted, active == NavTab.FAVORITOS); + styleTab(activity, R.id.nav_opciones, accent, muted, active == NavTab.OPCIONES); + + activity.findViewById(R.id.nav_inicio).setOnClickListener( + v -> goTab(activity, active, NavTab.INICIO)); + activity.findViewById(R.id.nav_favoritos).setOnClickListener( + v -> goTab(activity, active, NavTab.FAVORITOS)); + activity.findViewById(R.id.nav_opciones).setOnClickListener( + v -> goTab(activity, active, NavTab.OPCIONES)); + } + + /** Tinta icono+texto de una pestaña (accent la activa, muted las demás) */ + private static void styleTab(AppCompatActivity activity, int tabId, + int accent, int muted, boolean selected) { + ViewGroup tab = activity.findViewById(tabId); + ImageView icon = (ImageView) tab.getChildAt(0); + TextView label = (TextView) tab.getChildAt(1); + int color = selected ? accent : muted; + icon.setColorFilter(color); + label.setTextColor(color); + label.getPaint().setFakeBoldText(selected); + } + + /** Navega a otra pestaña; las pantallas secundarias se cierran al salir */ + private static void goTab(AppCompatActivity activity, NavTab current, NavTab target) { + if (target == current) { + return; + } + Class cls; + if (target == NavTab.INICIO) { + cls = MainActivity.class; + } else if (target == NavTab.FAVORITOS) { + cls = FavsActivity.class; + } else { + cls = OptsActivity.class; + } + Intent intent = new Intent(activity, cls) + .addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT + | Intent.FLAG_ACTIVITY_NO_ANIMATION); + activity.startActivity(intent); + activity.overridePendingTransition(0, 0); + /* Solo las pestañas viven en la pila; Acerca de se cierra al salir */ + if (!(activity instanceof MainActivity) && !(activity instanceof FavsActivity) + && !(activity instanceof OptsActivity)) { + activity.finish(); + activity.overridePendingTransition(0, 0); + } + } + + /* ------------------------------------------------------------------ */ + /* Banner AdMob compartido */ + /* ------------------------------------------------------------------ */ + + /** + * Carga un banner adaptativo anclado en [container] (FrameLayout sobre la + * barra inferior, presente en todas las pantallas del rediseño). + * El AdView se crea SIEMPRE por código con su adSize adaptativo: si + * viniera del XML sin ads:adSize, el SDK no regenera el WebView a la + * altura correcta. MobileAds.initialize lo hace MainActivity (tras el + * consent UMP); las demás pantallas sólo crean y cargan su AdView. + */ + public static void loadBannerAd(AppCompatActivity activity, FrameLayout container) { + AdView adView = new AdView(activity); + // AdSize trabaja en dp, no en píxeles físicos + int adWidthDp = (int) (activity.getResources().getDisplayMetrics().widthPixels + / activity.getResources().getDisplayMetrics().density); + adView.setAdSize(AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(activity, adWidthDp)); + adView.setAdUnitId(activity.getString(R.string.banner_ad_unit_id)); + container.addView(adView, new FrameLayout.LayoutParams( + ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)); + adView.loadAd(new AdRequest.Builder().build()); + } }