For ViewModel, LiveData and Data binding
I needed this functionality for EditText
with multiline support in my notes app. I wanted the cursor at the end of the text when the user navigates to the fragment that has note text.
The solution suggested by the djleop comes close. But the problem with this is that, if the user puts the cursor somewhere in the middle of the text for editing and starts typing, the cursor would jump to the end of text again. This happened because the LiveData
would emit the new value and cursor would jump to the end of the text again resulting in user not able to edit the text somewhere in the middle.
To solve this, I use MediatorLiveData
and assign it the length of String
only once using a flag. This will cause the LiveData to read the value only once, that is, when the user navigates to the fragment. After that the user can place the cursor anywhere they want to edit the text there.
ViewModel
private var accessedPosition: Boolean = false
val cursorPosition = MediatorLiveData<Event<Int>>().apply {
addSource(yourObject) { value ->
if(!accessedPosition) {
setValue(Event(yourObject.note.length))
accessedPosition = true
}
}
}
Here, yourObject
is another LiveData retrieved from the database that holds the String text that you are displaying in the EditText
.
Then bind this MediatorLiveData
to your EditText using binding adapter.
XML
Uses two-way data binding for displaying text as well as accepting the text input.
<!-- android:text must be placed before cursorPosition otherwise we'll get IndexOutOfBounds exception-->
<EditText
android:text="@={viewModel.noteText}"
cursorPosition="@{viewModel.cursorPosition}" />
Binding Adapter
@BindingAdapter("cursorPosition")
fun bindCursorPosition(editText: EditText, event: Event<Int>?) {
event?.getContentIfNotHandled()?.let { editText.setSelection(it) }
}
Event
class
The Event
class here is like a SingleLiveEvent written by Jose Alcérreca from Google. I use it here to take care of screen rotation. Using the single Event
will make sure that the cursor won't jump to the end of text when the user is editing the text somewhere in the middle and the screen rotates. It will maintain the same position when the screen rotates.
Here's the Event
class:
open class Event<out T>(private val content: T) {
var hasBeenHandled = false
private set // Allow external read but not write
/**
* Returns the content and prevents its use again.
*/
fun getContentIfNotHandled(): T? {
return if (hasBeenHandled) {
null
} else {
hasBeenHandled = true
content
}
}
/**
* Returns the content, even if it's already been handled.
*/
fun peekContent(): T = content
}
This is the solution that works for me and provides good user experience. Hope it helps in your projects too.
EditText
s as items of aListView
. As for experimentation, I made some changes on theListView
source code itself, which is a rather complex beast, and tested on the emulator. It was related to focus control management made by the component. So of course it's not a solution I could give to help our friend. :)OnFocusChanged
callback. The solution there is to put setSelection inside a runnable and run it on main thread. Check here stackoverflow.com/a/32156989/4514796