Skip to content

Commit 2c32a2e

Browse files
ashnohealexvanyo
andauthored
add system bar protection views snippet (#470)
* add system bar protection views snippet * add license header * fix region tags * fix region tags again * moved SystemBarProtection.kt into its own package & simplified layout of system_bar_protection.xml * fix region tag in xml * add in an alpha gradient --------- Co-authored-by: Alex Vanyo <[email protected]>
1 parent f9bb817 commit 2c32a2e

File tree

3 files changed

+110
-1
lines changed

3 files changed

+110
-1
lines changed

gradle/libs.versions.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ androidx-compose-ui-test = "1.7.0-alpha08"
88
androidx-constraintlayout = "2.2.0"
99
androidx-constraintlayout-compose = "1.1.0"
1010
androidx-coordinator-layout = "1.2.0"
11-
androidx-corektx = "1.15.0"
11+
androidx-corektx = "1.16.0-beta01"
1212
androidx-credentials = "1.5.0"
1313
androidx-credentials-play-services-auth = "1.5.0"
1414
androidx-emoji2-views = "1.5.0"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* Copyright 2025 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package insets
18+
19+
import android.graphics.Color
20+
import android.os.Bundle
21+
import android.view.View
22+
import androidx.activity.enableEdgeToEdge
23+
import androidx.appcompat.app.AppCompatActivity
24+
import androidx.core.view.ViewCompat
25+
import androidx.core.view.WindowInsetsCompat
26+
import androidx.core.view.insets.GradientProtection
27+
import androidx.core.view.insets.ProtectionLayout
28+
import com.example.example.snippet.views.R
29+
30+
class SystemBarProtectionSnippet : AppCompatActivity() {
31+
32+
override fun onCreate(savedInstanceState: Bundle?) {
33+
super.onCreate(savedInstanceState)
34+
setContentView(R.layout.system_bar_protection)
35+
36+
enableEdgeToEdge()
37+
38+
ViewCompat.setOnApplyWindowInsetsListener(
39+
findViewById(R.id.item_list)
40+
) { v: View, insets: WindowInsetsCompat ->
41+
val innerPadding = insets.getInsets(
42+
WindowInsetsCompat.Type.systemBars() or
43+
WindowInsetsCompat.Type.displayCutout()
44+
)
45+
v.setPadding(
46+
innerPadding.left,
47+
innerPadding.top,
48+
innerPadding.right,
49+
innerPadding.bottom
50+
)
51+
insets
52+
}
53+
54+
// [START android_system_bar_protection_kotlin]
55+
val red = 52
56+
val green = 168
57+
val blue = 83
58+
findViewById<ProtectionLayout>(R.id.list_protection)
59+
.setProtections(
60+
listOf(
61+
GradientProtection(
62+
WindowInsetsCompat.Side.TOP,
63+
// Ideally, this is the pane's background color
64+
// alpha = 204 for an 80% gradient
65+
Color.argb(204, red, green, blue)
66+
)
67+
)
68+
)
69+
// [END android_system_bar_protection_kotlin]
70+
}
71+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!--
3+
4+
Copyright 2025 The Android Open Source Project
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
https://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
18+
-->
19+
20+
<!-- [START android_system_bar_protection_xml] -->
21+
<androidx.core.view.insets.ProtectionLayout
22+
xmlns:android="http://schemas.android.com/apk/res/android"
23+
android:id="@+id/list_protection"
24+
android:layout_width="match_parent"
25+
android:layout_height="match_parent">
26+
27+
<ScrollView
28+
android:id="@+id/item_list"
29+
android:clipToPadding="false"
30+
android:layout_width="match_parent"
31+
android:layout_height="match_parent">
32+
33+
<!--items-->
34+
35+
</ScrollView>
36+
37+
</androidx.core.view.insets.ProtectionLayout>
38+
<!-- [END android_system_bar_protection_xml] -->

0 commit comments

Comments
 (0)