Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to search with enter key on android custom InputMethodService?

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".

like image 901
John Avatar asked Mar 03 '16 18:03

John


People also ask

What is input method service in Android?

An input method editor (IME) is a user control that enables users to enter text. Android provides an extensible input-method framework that allows applications to provide users alternative input methods, such as on-screen keyboards or even speech input.


2 Answers

You have to map your keys and overwrite the enter key press, then put your search code inside.

@Override
public void onKey(int primaryCode, int[] keyCodes) {     
    switch(primaryCode){

        case android.inputmethodservice.Keyboard.KEYCODE_DONE:
            ic.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_ENTER));
            break;
    break;
    }
}

In case you don't have custom keyCode you can see some here and here.

like image 169
Ricardo A. Avatar answered Nov 06 '22 23:11

Ricardo A.


try his code

 public boolean onKey(View v, int keyCode, KeyEvent event) {
            // If the event is a key-down event on the "enter" button
            if ((event.getAction() == KeyEvent.ACTION_DOWN)
                    && (keyCode == KeyEvent.KEYCODE_ENTER)) {
                // do ur stuff
                return true;
            }
            return false;
        }
    });
like image 42
Sagar Avatar answered Nov 06 '22 22:11

Sagar