-
-
Save KatieBarnett/2390800610b484c478334eb4179d6457 to your computer and use it in GitHub Desktop.
Wear OS input full code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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