1

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??

JozeRi
  • 3,219
  • 6
  • 27
  • 45

2 Answers2

5

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

royB
  • 12,779
  • 15
  • 58
  • 80
  • Hi royB, thanks for replying, your solution didn't help =/ I edited my question and added the information you asked for – JozeRi May 05 '15 at 11:12
  • ok, can you please move the declaration of `imeSearch` to the xml file just for the fun of it...: `android:imeOptions="actionSearch"`. and remove it from your programming code – royB May 05 '15 at 11:23
0

This works for my use of actionSearch:

txtAutocomplete.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent) {
                boolean handled = false;
                if(actionId == EditorInfo.IME_ACTION_SEARCH || actionId == EditorInfo.IME_ACTION_UNSPECIFIED)
                {
                    txtAutocomplete.clearFocus();

                    InputMethodManager imm = (InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.hideSoftInputFromWindow(txtAutocomplete.getWindowToken(),0);

                    handled = true;
                }
                return handled;
            }
        });
HondaGuy
  • 1,251
  • 12
  • 29