|
| 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 | +} |
0 commit comments