Reputation: 1312
I am trying to handle imeOption actionDone in my viewmodel using databinding.
There are a couple other posts that dont give a solution that is what im looking for. I want to set this in XML with a BindingAdapter to handle the event of actionDone. I keep receiving different databinding errors following solutions to the other posts.
I really wish I could find a doc that outlines all the databinding XML syntax and why things work cause a lot of docs (including official Android ones) use a mixture of lambdas not/passing params without explanation. change it even a little bit and binding errors in gradle build.
xml
<EditText
android:id="@+id/passwordEdit"
android:layout_width="0dp"
android:layout_height="42dp"
android:ems="10"
android:text="@={mainViewModel.password}"
android:inputType="textPassword"
android:imeOptions="actionDone"
app:onEditorActionDone="@{(view) -> mainViewModel.onEditorActionDone(view)}"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/passwordTitle" />
ViewModel
@BindingAdapter({"onEditorActionDone"})
public void onEditorActionDone(EditText view) {
AppLog.d(TAG, "-> onEditorActionDone()");
view.setOnEditorActionListener((v, actionId, event) -> {
if (actionId == EditorInfo.IME_ACTION_DONE) {
//do login
}
//do nothing
});
}
I have tried
app:onEditorActionDone="@{mainViewModel::onEditorActionDone}"
app:onEditorActionDone="@{(view) -> mainViewModel.onEditorActionDone()}"
app:onEditorActionDone="@{mainViewModel.onEditorActionDone}"
Upvotes: 1
Views: 891
Reputation: 15766
You don't need a custom @BindingAdapter
. Try this:
<EditText
android:id="@+id/passwordEdit"
android:layout_width="0dp"
android:layout_height="42dp"
android:ems="10"
android:text="@={mainViewModel.password}"
android:inputType="textPassword"
android:imeOptions="actionDone"
android:onEditorAction="@{(view,actionId,event) -> viewModel.onEditorAction(view,actionId,event)}"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/passwordTitle" />
And your view model class should implement the TextView.OnEditorActionListener
interface:
public class ViewModel implements TextView.OnEditorActionListener {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
Log.d("ViewModel", "onEditorAction");
return false;
}
}
Upvotes: 5