1 /* <lambda>null2 * Copyright 2023 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 * http://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 androidx.activity.integration.testapp 18 19 import android.app.Dialog 20 import android.graphics.Color 21 import android.os.Build 22 import android.os.Bundle 23 import android.view.View 24 import androidx.activity.SystemBarStyle 25 import androidx.activity.enableEdgeToEdge 26 import androidx.appcompat.app.AlertDialog 27 import androidx.appcompat.app.AppCompatActivity 28 import androidx.appcompat.app.AppCompatDelegate 29 import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen 30 import androidx.fragment.app.DialogFragment 31 32 class EdgeToEdgeActivity : AppCompatActivity(R.layout.edge_to_edge_activity) { 33 34 override fun onCreate(savedInstanceState: Bundle?) { 35 if (Build.VERSION.SDK_INT >= 21) { 36 installSplashScreen() 37 } 38 enableEdgeToEdge() 39 super.onCreate(savedInstanceState) 40 findViewById<View>(R.id.default_config).setOnClickListener { 41 // The default style. 42 // API 29+: Transparent on gesture nav, Auto scrim on 3-button nav (same as default). 43 // API 26-28: Transparent status. Light or dark scrim on nav. 44 // API 23-25: Transparent status. Dark scrim on nav. 45 // API 21,22: Dark scrim (system default). 46 enableEdgeToEdge() 47 } 48 findViewById<View>(R.id.auto_config).setOnClickListener { 49 // API 29+: Transparent on gesture nav, Auto scrim on 3-button nav (same as default). 50 // API 23-28: Yellow bars. 51 // API 21,22: Dark scrim (system default). 52 val style = 53 SystemBarStyle.auto( 54 lightScrim = Color.argb(0x64, 0xff, 0xeb, 0x3b), 55 darkScrim = Color.argb(0x64, 0x4a, 0x14, 0x8c) 56 ) 57 enableEdgeToEdge(statusBarStyle = style, navigationBarStyle = style) 58 } 59 findViewById<View>(R.id.custom_config).setOnClickListener { 60 // API 29+: Transparent on gesture nav, Auto scrim on 3-button nav (same as default). 61 // API 23-28: Yellow bars. 62 // API 21,22: Dark scrim (system default). 63 val style = 64 SystemBarStyle.auto( 65 lightScrim = Color.argb(0x64, 0xff, 0xeb, 0x3b), 66 darkScrim = Color.argb(0x64, 0x4a, 0x14, 0x8c), 67 detectDarkMode = { false } 68 ) 69 enableEdgeToEdge(statusBarStyle = style, navigationBarStyle = style) 70 } 71 findViewById<View>(R.id.transparent_config).setOnClickListener { 72 // API 23+: Transparent regardless of the nav mode. 73 // API 21,22: Dark scrim (system default). 74 val style = SystemBarStyle.dark(Color.TRANSPARENT) 75 enableEdgeToEdge(statusBarStyle = style, navigationBarStyle = style) 76 } 77 findViewById<View>(R.id.purple_config).setOnClickListener { 78 // API 23+: Purple. 79 // API 21,22: Dark scrim (system default). 80 val style = SystemBarStyle.dark(scrim = Color.argb(0x64, 0x4a, 0x14, 0x8c)) 81 enableEdgeToEdge(statusBarStyle = style, navigationBarStyle = style) 82 } 83 findViewById<View>(R.id.yellow_config).setOnClickListener { 84 // API 23+: Yellow. 85 // API 21,22: Dark scrim (system default). 86 val style = 87 SystemBarStyle.light( 88 scrim = Color.argb(0x64, 0xff, 0xeb, 0x3b), 89 darkScrim = Color.rgb(0xf5, 0x7f, 0x17) 90 ) 91 enableEdgeToEdge(statusBarStyle = style, navigationBarStyle = style) 92 } 93 findViewById<View>(R.id.light_mode).setOnClickListener { setDarkMode(false) } 94 findViewById<View>(R.id.dark_mode).setOnClickListener { setDarkMode(true) } 95 findViewById<View>(R.id.show_dialog).setOnClickListener { 96 EdgeToEdgeDialogFragment().show(supportFragmentManager, null) 97 } 98 } 99 100 private fun setDarkMode(darkMode: Boolean) { 101 AppCompatDelegate.setDefaultNightMode( 102 if (darkMode) AppCompatDelegate.MODE_NIGHT_YES else AppCompatDelegate.MODE_NIGHT_NO 103 ) 104 } 105 } 106 107 class EdgeToEdgeDialogFragment : DialogFragment() { 108 onCreateDialognull109 override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { 110 return AlertDialog.Builder(requireContext()) 111 .setTitle("Demo Dialog") 112 .setMessage("Hello, world!") 113 .setPositiveButton(android.R.string.ok, { dialog, _ -> dialog.dismiss() }) 114 .create() 115 } 116 } 117