My `WindowInsets` are going crazy, is anyone exper...
# compose-android
m
My
WindowInsets
are going crazy, is anyone experiencing the same thing? 😵 Why are my Scaffold content padding and status bar insets so big?? Here's a debug point values snapshot
Copy code
ScaffoldContentPadding = {PaddingValuesImpl@41561} PaddingValues(start=0.0.dp, top=105.454544.dp, end=0.0.dp, bottom=104.0.dp)
WindowInsets.safeContent = {UnionInsets@41563} (((systemBars(0, 290, 0, 66) ∪ ime(0, 0, 0, 0)) ∪ displayCutout(0, 136, 0, 0)) ∪ (((tappableElement(0, 136, 0, 0) ∪ mandatorySystemGestures(0, 169, 0, 88)) ∪ systemGestures(82, 169, 82, 88)) ∪ waterfall(left=0, top=0, right=0, bottom=0)))
WindowInsets.safeDrawing = {UnionInsets@41564} ((systemBars(0, 290, 0, 66) ∪ ime(0, 0, 0, 0)) ∪ displayCutout(0, 136, 0, 0))
WindowInsets.safeGestures = {UnionInsets@41565} (((tappableElement(0, 136, 0, 0) ∪ mandatorySystemGestures(0, 169, 0, 88)) ∪ systemGestures(82, 169, 82, 88)) ∪ waterfall(left=0, top=0, right=0, bottom=0))
I'm using a Material 3 Scaffold for a screen within a Compose Navigation
NavHost
. I've set
enableEdgeToEdge()
in my activity
onCreate
. Initially I was getting 0 for all WindowsInsets values on the screen Compose code, until I forced the configuration to update by rotating my Pixel 5 device from portrait to landscape (and back again). This seems like a bug? Anyway, I somehow fixed that by forcing the insets to be fetched immediately:
Copy code
class MainActivity : ComponentActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        installSplashScreen()

        enableEdgeToEdge()

        // We don't use any theme XML files, so we need to hide this here
        actionBar?.hide()

        setContent {
            // Some buggy nonsense in insets going on. Without this (to shock em awake?) all insets are 0 until a configuration change...
            WindowInsets.safeContent

            MyTheme {
                MainScreen()
            }
        }
    }
}
But now I have the insets too big issue as above 🙃 My Compose version "1.7.5", but I've also tried it on "1.7.0".
s
What does
actionBar?.hide()
do there? What is your root theme of your app? Why not do something like
Theme.AppCompat.Light.NoActionBar
so that you get no action bar at all in the first place?
m
I've attempted to remove all the theming XML files and use Compose only as far as I can, so that was a work around for the action bar which is there by default. It's not ideal, but do you think it's actually causing the problem?
s
What is the problem in the first place exactly?
I've attempted to remove all the theming XML files and use Compose only as far as I can
Sure, but you should still set some things in your xml. You can't get around that, compose is not initialized on the very first frames, like when the splash screen is showing for example
You can use NiA as an inspiration on what you must still define even in a compose only app https://github.com/android/nowinandroid/blob/main/app/src/main/res/values/themes.xml
m
Here's a screenshot, the padding is too big above my content
Copy code
Scaffold(
        bottomBar = {
            MyNavigationBar(
                selectedTab = MainTab.ChatList,
                selectTab = navigateToTab,
            )
        },
    ) { contentPadding ->
        LazyColumn(
            modifier = Modifier
                .statusBarsPadding() // This adds the huge gap
                .fillMaxSize(),
            contentPadding = contentPadding.copy(top = 0.dp),
        ) {
            // my list content is in here
        }
    }
Yeah I've still got the theming for the Splash screen
Okay copying NIA exactly fixed the actionBar sized additional padding I was seeing. I'm still not sure I understand why the action Bar is considered part of the WindowInsets
🎉 1
s
Me neither, but I can't say I've seen an ActionBar in an Android app for like 10 years now 😅
😂 1
1
m
Yeah it's kinda lame that it's there by default still
Somehow this also fixed my initial issue that insets were 0 without a configuration change first blob shrug thank you friend
🤗 1
e
> I can’t say I’ve seen an ActionBar in an Android app for like 10 years now Because (almost) everyone uses
Theme.xxx.NoActionBar
or some descendant 🙂. Android has evolved a lot but there was/is a feature where System UI draws the “action bar” for your app. Obvsly that would go into insets if you had something like
fitsSystemWindows
turned on (because the action bar is technically provided by the system) For anyone else checking this out, to configure an android app (or particular activity) to handle insets for the best experience (as of Nov 2024) you need to: • Set the application / specific activity theme to some
xxx.NoActionBar
descendant • Set
android:windowSoftInputMode="adjustResize"
on the activity in the manifest • On the activity, using
androidx activity-ktx
, call
enableEdgeToEdge(...)
• Within compose hierarchy, use the insets API.
💯 1
thank you color 1