Saraschandraa
Saraschandraa

Reputation: 496

how to close a softinputkeyboard

I have created a customauto EditText field. My problem with the text field is that when i click on the EditText the keyboard rises but when i click elsewhere the keyboard still remains open. Please help me in this issue

The name of the custom auto EditText is auto_list. I have attached the onFocusChangeListener

auto_list.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
            public void onFocusChange(View v, boolean hasFocus) {
                // TODO Auto-generated method stub
                if (hasFocus) {
                    getActivity()
                            .getWindow()
                            .setSoftInputMode(
                                    WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
                }
                else{
                    InputMethodManager im = (InputMethodManager)v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                    im.hideSoftInputFromWindow(v.getWindowToken(), 0);
                }
            }
        });

Upvotes: 0

Views: 160

Answers (4)

umair.ali
umair.ali

Reputation: 2714

Do this in event on which you want to hide soft keyboard...

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

where xxx is the edit text in which you want to hide keyboard. do same if you have more than one edit texts on which you want to apply same functionality.

Upvotes: 0

Hendy
Hendy

Reputation: 265

try this :

try
    {
        InputMethodManager inputManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
        inputManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
     }
    catch (Exception e)
    {
        // Ignore exceptions if any
            Log.e("KeyBoardUtil", e.toString(), e);
    }

Upvotes: 0

Rohit5k2
Rohit5k2

Reputation: 18112

You are only checking hasFocus, this seems to be causing you problem. you should check something like this

    if(v.getId() == R.id.your_edit_text && hasFocus)

Upvotes: 0

PgmFreek
PgmFreek

Reputation: 6402

Try below code

public static void hideKeyboard(Activity activity) {
     InputMethodManager inputManager = (InputMethodManager) activity
       .getSystemService(Context.INPUT_METHOD_SERVICE);
    if (inputManager != null && activity.getCurrentFocus() != null) {
      inputManager.hideSoftInputFromWindow(activity.getCurrentFocus()
     .getWindowToken(), 0);
    }
}

Upvotes: 1

Related Questions