user5594710
user5594710

Reputation:

Hide keyboard autocompletetextview

I have problems with when I try click out from view (autocomplete), my keyboard not dissmis, and I don't know what I'm do wrong.

In my activity hace : autocomplete.setOnItemClickListener into here onItemClick, when user click I call :

InputMethodManager in = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
in.hideSoftInputFromWindow(parent.getWindowToken(), 0);

... but never hide keyboard.

I try also and not work:

/*autoCompleteTextView1.requestFocus();
 autoCompleteTextView1.requestFocusFromTouch();*/

This is my class custom from autocomplete

public class InstantAutoComplete extends AutoCompleteTextView  {

    public InstantAutoComplete(Context context) {
        super(context);
    }

    public InstantAutoComplete(Context arg0, AttributeSet arg1) {
        super(arg0, arg1);
    }

    public InstantAutoComplete(Context arg0, AttributeSet arg1, int arg2) {
        super(arg0, arg1, arg2);
    }

    @Override
    public boolean enoughToFilter() {
        return true;
    }

    private boolean mIsKeyboardVisible;

    @Override
    protected void onFocusChanged(boolean focused, int direction,Rect previouslyFocusedRect) {

        super.onFocusChanged(focused, direction, previouslyFocusedRect);
        if (getWindowVisibility() == View.GONE) {
            Log.d("InstantAutoComplete", "Window not visible, will not show drop down");
            return;
        }
        if (focused) {
            performFiltering(getText(), 0);
        }
        mIsKeyboardVisible = focused;
    }

    @Override
    public boolean onKeyPreIme(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && isPopupShowing()) {
            InputMethodManager inputManager = (InputMethodManager) getContext().getSystemService(
                    Context.INPUT_METHOD_SERVICE);
            // inputManager.isAcceptingText() will not work because view is still focused.
            if (mIsKeyboardVisible) { // Is keyboard visible?
                // Hide keyboard.
                inputManager.hideSoftInputFromWindow(getWindowToken(), 0);
                mIsKeyboardVisible = false;

                // Consume event.
                return true;
            } else {
                // Do nothing.
            }
        }

        return super.onKeyPreIme(keyCode, event);
    }

}

How I can close correctly keyboard ? Thanks.

Upvotes: 0

Views: 3545

Answers (2)

tony gil
tony gil

Reputation: 9564

In AutoCompleteTextView, You can hide the keyboard from onItemClick after you click on a list item

public void onItemClick(AdapterView<?> adapterViewIn, View viewIn, int indexSelected, long arg3) {
     InputMethodManager imm = (InputMethodManager) getSystemService(viewIn.getContext().INPUT_METHOD_SERVICE);
     imm.hideSoftInputFromWindow(viewIn.getApplicationWindowToken(), 0);
     // whatever else should be done
}

Upvotes: 2

sud
sud

Reputation: 505

use this-

InputMethodManager inputManager = 
        (InputMethodManager) context.
            getSystemService(Context.INPUT_METHOD_SERVICE); 
inputManager.hideSoftInputFromWindow(
        this.getCurrentFocus().getWindowToken(),
        InputMethodManager.HIDE_NOT_ALWAYS); 

Upvotes: 1

Related Questions