Skip to content

Instantly share code, notes, and snippets.

@KatieBarnett
Created March 19, 2023 03:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KatieBarnett/2390800610b484c478334eb4179d6457 to your computer and use it in GitHub Desktop.
Save KatieBarnett/2390800610b484c478334eb4179d6457 to your computer and use it in GitHub Desktop.
Wear OS input full code
@Composable
fun UserInputScreen(
modifier: Modifier = Modifier
) {
val defaultText = stringResource(id = R.string.edit_user_input)
var userInput by remember { mutableStateOf(defaultText) }
val inputTextKey = "input_text"
val remoteInputs: List<RemoteInput> = listOf(
RemoteInput.Builder(inputTextKey)
.setLabel(stringResource(id = R.string.edit_user_input))
.wearableExtender {
setEmojisAllowed(true)
setInputActionType(EditorInfo.IME_ACTION_DONE)
}.build(),
)
val launcher = rememberLauncherForActivityResult(
ActivityResultContracts.StartActivityForResult()
) {
it.data?.let { data ->
val results: Bundle = RemoteInput.getResultsFromIntent(data)
val newInputText: CharSequence? = results.getCharSequence(inputTextKey)
userInput = newInputText?.toString() ?: ""
}
}
val intent: Intent = RemoteInputIntentHelper.createActionRemoteInputIntent()
RemoteInputIntentHelper.putRemoteInputsExtra(intent, remoteInputs)
Box(
contentAlignment = Alignment.Center,
modifier = modifier.fillMaxSize()
) {
Row(modifier = Modifier.fillMaxWidth(0.75f)) {
Text(text = userInput, Modifier.weight(1f))
CompactButton(
onClick = { launcher.launch(intent) },
) {
Icon(
imageVector = Icons.Filled.Edit,
contentDescription = stringResource(id = R.string.edit_user_input)
)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment