Skip to content

Instantly share code, notes, and snippets.

@bassettsj
Created January 25, 2024 22:33
Show Gist options
  • Save bassettsj/386723bcedb95c5ecb0f090101ad48be to your computer and use it in GitHub Desktop.
Save bassettsj/386723bcedb95c5ecb0f090101ad48be to your computer and use it in GitHub Desktop.
MoveTalkbackFocus
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.focusable
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.material.Button
import androidx.compose.material.SnackbarDefaults.backgroundColor
import androidx.compose.material.Surface
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.Modifier
import androidx.compose.ui.focus.FocusRequester
import androidx.compose.ui.focus.focusRequester
import androidx.compose.ui.focus.focusTarget
import androidx.compose.ui.focus.onFocusChanged
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.semantics.clearAndSetSemantics
import androidx.compose.ui.semantics.invisibleToUser
import androidx.compose.ui.semantics.isTraversalGroup
import androidx.compose.ui.semantics.semantics
import androidx.compose.ui.semantics.traversalIndex
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
@OptIn(ExperimentalComposeUiApi::class)
@Preview
@Composable
fun FocusTrial() {
var showAlert by remember { mutableStateOf(false) }
var color by remember { mutableStateOf(Color.Red) }
val buttonModifier = Modifier.then(
if (showAlert) Modifier.clearAndSetSemantics {
invisibleToUser()
} else Modifier
)
Surface(
modifier = Modifier
.fillMaxSize()
){
Column(
verticalArrangement = Arrangement.SpaceBetween,
) {
Button(
onClick = { showAlert = !showAlert },
modifier = buttonModifier,
) {
Text("Click me")
}
if (showAlert) {
Row(
modifier = Modifier
.background(color)
.fillMaxWidth()
.padding(8.dp)
) {
Text("Alert", color = Color.White)
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment