I want to search with my android custom keyboard with the enter key, but it does not work.
I've already mapped the keys, I just need to trigger the "search action" on a search text field, just like searching on google.
I tried this code for triggering the search action, but it doesn't work:
ic.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_ENTER));
Here is my method for overwriting the enter key event:
public class Keyboard extends InputMethodService
implements KeyboardView.OnKeyboardActionListener {
@Override
public void onStartInputView(EditorInfo info, boolean restarting) {
super.onStartInputView(info, restarting);
setInputView(onCreateInputView());
switch (EditorInfo.IME_MASK_ACTION | EditorInfo.IME_FLAG_NO_ENTER_ACTION) {
case EditorInfo.IME_ACTION_SEARCH:
Toast.makeText(this, "test", Toast.LENGTH_SHORT).show();
//Action Search
break;
}
}
My xml layout is:
<?xml version="1.0" encoding="UTF-8"?>
<android.inputmethodservice.KeyboardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/keyboard"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:keyPreviewHeight="60dp"
android:keyPreviewOffset="12dp"
android:keyPreviewLayout="@layout/preview"
android:visibility="visible"/>
I don't have any EditText in my layout to set android:imeOptions="actionSearch"
.