Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add system bar protection views snippet #470

Merged
merged 9 commits into from
Mar 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ androidx-compose-ui-test = "1.7.0-alpha08"
androidx-constraintlayout = "2.2.0"
androidx-constraintlayout-compose = "1.1.0"
androidx-coordinator-layout = "1.2.0"
androidx-corektx = "1.15.0"
androidx-corektx = "1.16.0-beta01"
androidx-credentials = "1.5.0"
androidx-credentials-play-services-auth = "1.5.0"
androidx-emoji2-views = "1.5.0"
Expand Down
71 changes: 71 additions & 0 deletions views/src/main/java/insets/SystemBarProtectionSnippet.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Copyright 2025 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package insets

import android.graphics.Color
import android.os.Bundle
import android.view.View
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.core.view.insets.GradientProtection
import androidx.core.view.insets.ProtectionLayout
import com.example.example.snippet.views.R

class SystemBarProtectionSnippet : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.system_bar_protection)

enableEdgeToEdge()

ViewCompat.setOnApplyWindowInsetsListener(
findViewById(R.id.item_list)
) { v: View, insets: WindowInsetsCompat ->
val innerPadding = insets.getInsets(
WindowInsetsCompat.Type.systemBars() or
WindowInsetsCompat.Type.displayCutout()
)
v.setPadding(
innerPadding.left,
innerPadding.top,
innerPadding.right,
innerPadding.bottom
)
insets
}

// [START android_system_bar_protection_kotlin]
val red = 52
val green = 168
val blue = 83
findViewById<ProtectionLayout>(R.id.list_protection)
.setProtections(
listOf(
GradientProtection(
WindowInsetsCompat.Side.TOP,
// Ideally, this is the pane's background color
// alpha = 204 for an 80% gradient
Color.argb(204, red, green, blue)
)
)
)
// [END android_system_bar_protection_kotlin]
}
}
38 changes: 38 additions & 0 deletions views/src/main/res/layout/system_bar_protection.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?xml version="1.0" encoding="utf-8"?>
<!--

Copyright 2025 The Android Open Source Project

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

https://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

-->

<!-- [START android_system_bar_protection_xml] -->
<androidx.core.view.insets.ProtectionLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/list_protection"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ScrollView
android:id="@+id/item_list"
android:clipToPadding="false"
android:layout_width="match_parent"
android:layout_height="match_parent">

<!--items-->

</ScrollView>

</androidx.core.view.insets.ProtectionLayout>
<!-- [END android_system_bar_protection_xml] -->