I have a screen with TextField and, ideally, I want to achieve the following behavior:
TextField should become focused as soon as the screen is shown and the software keyboard must appear
User can clear the focus and hide the software keyboard (or I can do it programatically)
When device is rotated, focus and the keyboard must retain their state (so, for example, if the keyboard was shown, it should stay)
Currently I can't even get keyboard to always show up after screen rotation.
I have the following code, but it does this: for example, if the screen was initially opened in portrait mode, when rotated into landscape the keyboard hides, but when rotated back to portrait it returns back. Focus always stays as expected.
val focusRequester = remember { FocusRequester() }
TextField(
value = ...,
onValueChange = { ... },
modifier = Modifier.focusRequester(focusRequester)
)
focusRequester.requestFocus()
Modifying it as follows works, but I don't really like this delay:
...
val keyboard = LocalSoftwareKeyboardController.current
LaunchedEffect(Unit) {
focusRequester.requestFocus()
delay(1000L) // Just a magic constant which works on my device
keyboard?.show()
}
How can I achieve the described behavior? I see that Google Search screen acts almost like I want, though, it doesn't remember keyboard's state and always opens it after rotation.
Related
Recomposition breaks if a view is added to the layout container from above.
Situation: there is a profile screen, by tapping on the cell with the number, we load information in the back, whether the user can change the phone and, accordingly, turn the progress bar. If it cannot, we show an error (a custom view that is simply added to the very top of the container), after that the composition screen freezes and does not render changes, CircularProgressIndicator instead of hiding - just hangs. But it is worth clicking in any other place, the screen starts to be drawn correctly. In the photo below, the progress bar is stuck:
Code:
if (rightIconId != null && !isLoading) {
RightIcon()
} else if (isLoading) {
CircularProgressIndicator()
}
Calling validate() on the container does not fix the bug.
Update.
It turned out that the blur was breaking the rendering of the UI.
https://issuetracker.google.com/issues/266807258
Am I missing something, but why Scaffold's TopAppBar moves out of the screen when keyboard appears?
I have a simple layout:
setContent {
MyTheme {
Scaffold(topBar = { MyTopAppBar() }) {
ScaffoldContent(it)
}
}
}
Where ScaffoldContent is just a Column with a TextField.
When I press a TextField, keyboard opens and moves everything up . How can I make the bar stick at the top?
Tried:
android:windowSoftInputMode="adjustResize" or "adjustPan"
Instead of Scaffold use a Column and add elements there, but moves anyway
Expected:
TopAppBar to be at the top all the time, like in our Views system
Created a LazyColumn with textFields and it seems to have issue with "android:windowSoftInputMode="adjustResize"parameter.
When clicking a text field that needs to move up so it won't be hidden behind keyboard, causes issues when using AdjustResize parameter. The keyboard will open and then close again. It seems that textfield has lost a focus when ui is doing adjustResize.
Simple code example:
LazyColumn(state = rememberLazyListState()) {
for (i in 1..20) {
item {
TextField(value = "item{$i}", onValueChange = {})
}
}
}
And in AndroidManifest.xml I have
<activity
android:name=".MainActivity"
android:exported="true"
android:windowSoftInputMode="adjustResize"
...
Demo to show that keyboard is closed just after it opens. (This issue only happens when to click on item that needs adjustment to be visible when keyboard is open)
LazyColumn Textfield works correctly without AdjustResize focused item will be moved up and everything is fine. The reason why I would like to use AdjustResize is that in my actual app I have undo/redo buttons at the bottom of the screen and I would like to move them up when keyboard is opened, but it seems I need to use AdjustResize to make it work, but that brakes textFields.
So I was wondering how can I make LazyColumn TextFields to work with AdjustResize? (I'm using latest compose version: 1.3.1)
I have an Android Jetpack Compose project where I show a screen with a InputField:
When coming to that screen, the keyboard opens automatically, ready to read the users entered characters.
The screen also includes a "imprint" link opening default webbrowser
On Android 11 and higher, when I tap the back button (Android hardware button, triangle) the already opened webbrowser disappears and the previous input screen show again and the keyboard again opens automatically.
On Android 10 (emulator and real device), when I tap the back button (Android hardware button, triangle) the already opened webbrowser disappears and the previous input screen show again but the keyboard is not shown on Android 10.
Whats the reason for missing keyboard on Android 10?
Try using
val focusRequester = remember { FocusRequester() }
modifier = Modifier
.focusRequester(focusRequester = focusRequester)
you can also use
LaunchedEffect(Unit) {
focusRequester.requestFocus()
}
I have a Dialog containing a header, footer, and content item which is a ListView:
Dialog {
width: 200
height: 200
x: 10
y: 100
header: Rectangle {
}
contentItem: ListView {
delegate: TextField {
// this is covered by the Android keyboard
}
}
footer: Rectangle {
}
}
On iOS the whole screen is shifted up so that the ListView's TextField (https://doc.qt.io/qt-5/qml-qtquick-controls2-textfield.html) owning the text cursor is always visible. On Android the TextField's cursor is covered by the Android keyboard. I already tried to set android:windowSoftInputMode="adjustResize" in the AndroidManifest.xml but without success.
Is there an easy way to make sure that the TextField currently owning the cursor is always visible? Or is the only solution to manually move the Dialog's y coordinate?
Qml with hidden keyboard:
Qml keyboard hidding cursor:
Regular Android text field:
Regular Android text field is shifted up when the keyboard is open:
Regards,
The entire text view? No, that's not how keyboards work in Android. The only lever you have is the softInputMode, which is either pan or resize. In pan mode, the screen is scrolled up such that the cursor is always visible (but the rest of the text view may not be). In resize mode, the app is resized in the space above the keyboard, and then if needed shifted so the cursor is visible. This can sometimes make more of the screen visible, if you had empty space on screen and your layout is set to scale well (login screens frequently benefit). But there is no way to ensure the entire text view, or any other field, is still on screen. It's just not how Android works.