Use Done button on Keyboard in Databinding
android, android-databinding
Solution
I won't claim to be an expert in `onEditorAction()` or soft keyboard. That said, assuming you use the solution to the stack overflow question Firoz Memon suggested, you can make it happen. Even if there is another solution that works better, this can give you an idea on how to add your own event handlers.
You'd need a binding adapter that takes some kind of handler. Let's assume you have an empty listener like this:
public class OnOkInSoftKeyboardListener {
void onOkInSoftKeyboard();
}
Then you need a BindingAdapter:
@BindingAdapter("onOkInSoftKeyboard") // I like it to match the listener method name
public static void setOnOkInSoftKeyboardListener(TextView view,
final OnOkInSoftKeyboardListener listener) {
if (listener == null) {
view.setOnEditorActionListener(null);
} else {
view.setOnEditorActionListener(new OnEditorActionListener() {
@Override
public void onEditorAction(TextView v, int actionId, KeyEvent event) {
// ... solution to receiving event
if (somethingOrOther) {
listener.onOkInSoftKeyboard();
}
}
});
}
}
Problem
I am trying to use the done button of the soft keyboard to activate a method via databinding. Just like onClick. Is there a way to do that? example: ``` <EditText android:id="@+id/preSignUpPg2EnterPhone" android:layout_width="match_parent" android:layout_height="wrap_content" onOkInSoftKeyboard="@{(v) -> viewModel.someMethod()}" /> ``` onOkInSoftKeyboard doesn't exists... Is there something to create this behavior? Thanks!