androidkeyboardimeoptions

Android EditText imeOptions making the actionSearch act like actionDone?


I have a weird bug.

I have an EditText, and I'm performing a search with a TextWatcher, when I type 3 letters and above I am performing a search.

Until recently I had a normal EditText, and I want to have a Search Icon in my keyboard so I added my EditText in code:

searchField.setImeOptions(EditorInfo.IME_ACTION_SEARCH);

now it has the search icon, now to my problem. when pressing the Search Icon, all I want to do is close my keyboard. no search required.

here's my code :

searchField.setOnEditorActionListener(new TextView.OnEditorActionListener() {
                @Override
                public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                    if (event != null&& (event.getKeyCode() == KeyEvent.KEYCODE_SEARCH)) {
                        hideSoftKeyboard();
                    }
                    return false;
                }
            });

hideSoftKeyboard is a method that close my keyboard, and that's the code inside:

public void hideSoftKeyboard() {
    if (getActivity().getCurrentFocus() != null) {
        InputMethodManager inputMethodManager = (InputMethodManager) getActivity().getSystemService(Activity.INPUT_METHOD_SERVICE);
        inputMethodManager.hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(), 0);
    }
}

NOW, my keyboard DOES close, but for some reason, a "normal" keyboard appears behind it. the keyboard with the search icon is closing, but a keyboard with an enter icon appears, and it's not typing anything.. not connected to any edit text or something, i only have 1 edit text in that screen and I can't figure out what's wrong.

if I change back to IME_ACTION_DONE everything works fine again.

EDIT 1 :

my activity in the manifest :

<activity
        android:name=".UI.activity.HomeActivity"
        android:label="@string/app_name"
        android:launchMode="singleTask"
        android:screenOrientation="portrait"
        android:theme="@android:style/Theme.Holo.Light.NoActionBar"
        android:windowSoftInputMode="adjustResize" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

and my EditText in the xml :

<EditText
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:id="@+id/search_document_main"
                    android:typeface="serif"
                    android:visibility="invisible"
                    android:singleLine="true"
                    android:background="@color/top_bar_bg_color"
                    android:hint="Search" />

any idea what i'm doing wrong?

also, is there a way I can show that Search icon without using the imeOption Search??


Solution

  • try to get the window token from the EditText window. maybe your current focus item is not the EditText

    public void hideSoftKeyboard() {
        InputMethodManager imm = (InputMethodManager) getSystemService(
                Context.INPUT_METHOD_SERVICE
        );
        imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
    }
    

    Also you can simplify your listener to:

    new TextView.OnEditorActionListener() {
                    @Override
                    public boolean onEditorAction (TextView v, int actionId, KeyEvent event) {
                        if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                            hideKeyboard();
                            return true;
                        }
                        return false;
                    }
                }
    

    If you still have problem please add your Activity layout and also add the Activity manifest declaration