I am working on an immersive mode app that contains a CustomAlertDialog. I have done everything I could to keep the app in immersive mode after opening the Alert. I have found solutions that would work on Android 14 devices, but I have not found a solution for Android 8 device (API 26). On API 26 device after opening the dialog the navigation bar becomes visible and only disappears after closing the Dialog. All that I need is to have a button that keeps the navigation bar hidden when pressed and then opens up a container with options. I have tried using DropdownMenus or Popups, but nothing worked.
Bottom line: I would like to know if it is possible to create a fully immersive mode Android app with kotlin that would support API26+ and would contain dialogs that when opened don’t break the immersive mode.
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
hideSystemBars()
setContent {
FullScreenTheme {
Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding ->
Greeting(
name = "Android",
modifier = Modifier.padding(innerPadding)
)
}
}
}
}
override fun onWindowFocusChanged(hasFocus: Boolean) {
super.onWindowFocusChanged(hasFocus)
if (hasFocus) {
hideSystemBars()
}
}
override fun onResume() {
super.onResume()
hideSystemBars()
}
fun hideSystemBars() {
WindowCompat.setDecorFitsSystemWindows(window, false)
val controller = WindowInsetsControllerCompat(window, window.decorView)
controller.hide(WindowInsetsCompat.Type.systemBars())
controller.systemBarsBehavior = WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
}
}
@Composable
fun Greeting(name: String, modifier: Modifier = Modifier) {
var showDialog by remember { mutableStateOf(false) }
val activity = LocalContext.current as? MainActivity
Column(
modifier = modifier
.fillMaxSize()
.padding(16.dp),
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.Center
) {
Text(text = "Hello $name!")
Spacer(modifier = Modifier.height(16.dp))
Button(onClick = { showDialog = true }) {
Text(text = "Open Dialog")
}
if (showDialog) {
CustomAlertDialog(
onDismissRequest = { showDialog = false },
title = { Text(text = "Dialog Title") },
text = { Text(text = "This is a simple dialog.") },
confirmButton = {
Button(
onClick = { showDialog = false }
) {
Text(text = "Confirm")
}
},
dismissButton = {
Button(
onClick = { showDialog = false }
) {
Text(text = "Dismiss")
}
},
activity = activity
)
}
}
}
@Composable
fun CustomAlertDialog(
onDismissRequest: () -> Unit,
title: @Composable () -> Unit,
text: @Composable () -> Unit,
confirmButton: @Composable () -> Unit,
dismissButton: @Composable () -> Unit,
activity: MainActivity?
) {
SideEffect {
activity?.hideSystemBars()
}
DisposableEffect(Unit) {
activity?.hideSystemBars()
onDispose { activity?.hideSystemBars() }
}
AlertDialog(
onDismissRequest = onDismissRequest,
title = title,
text = text,
confirmButton = confirmButton,
dismissButton = dismissButton
)
}
OngoGablogianAC is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.