Android 16 support, fix edge to edge
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
package it.danieleverducci.nextcloudmaps.utils;
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
import android.app.Activity;
|
||||
import android.content.Context;
|
||||
import android.graphics.Color;
|
||||
import android.os.Build;
|
||||
import android.util.TypedValue;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
|
||||
import androidx.core.content.ContextCompat;
|
||||
import androidx.core.view.ViewCompat;
|
||||
|
||||
import it.danieleverducci.nextcloudmaps.R;
|
||||
|
||||
public class EdgeToEdgeUtils {
|
||||
|
||||
/**
|
||||
* Called in an activity's onCreate method, adds paddings to clear the system bars, in any orientation,
|
||||
* to make all the content visible. Useful to adapt an old android project to the mandatory introduction
|
||||
* of edge to edge in Android 15.
|
||||
* @param a the activity to pad
|
||||
*/
|
||||
public static void clearSystemInsets(Activity a) {
|
||||
// This color will be used as background in the system bars in case of edge to edge.
|
||||
// Defaults to R.color.colorAccent, if defined, black otherwise.
|
||||
int accentColor = ContextCompat.getColor(a, R.color.defaultBackground);
|
||||
|
||||
ViewGroup rootLayout = a.findViewById(android.R.id.content);
|
||||
rootLayout.setBackgroundColor(accentColor);
|
||||
|
||||
// Register to receive insets callbacks
|
||||
ViewCompat.setOnApplyWindowInsetsListener(rootLayout, (v, insets) -> {
|
||||
// Top padding is the device insets plus the actionbar height
|
||||
boolean hasActionBar = a.getActionBar() != null && a.getActionBar().isShowing();
|
||||
int actionbarHeightInPixels = 0;
|
||||
if (hasActionBar && Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
|
||||
Context context = rootLayout.getContext();
|
||||
TypedValue tv = new TypedValue();
|
||||
if (context.getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true))
|
||||
actionbarHeightInPixels = TypedValue.complexToDimensionPixelSize(tv.data, context.getResources().getDisplayMetrics());
|
||||
}
|
||||
int topPadding = insets.getStableInsetTop() + actionbarHeightInPixels;
|
||||
v.setPadding(insets.getStableInsetLeft(), topPadding, insets.getStableInsetRight(), insets.getStableInsetBottom());
|
||||
return insets;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user