Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to hide keyboard in Android

I would like to know the best way to hide keyboard after entering the text to EditText.

1) setonfocuschangelistener : Does this listener is fired only, when the done button is pressed or when the focus changes from one EditText to other? When I used this method, I couldn't hide the keyboard.

2) setOnTouchListener : When I used this, I could hide the keyboard, but i doubt there might be an issue with this. In this case, I add the touch listener to the root LinearLayout. Following code I had used:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.login);

    txtUserName = (EditText)findViewById(R.id.txtUserName);
    btnLogin = (Button)findViewById(R.id.btnLogin);
    layoutView = (LinearLayout)findViewById(R.id.li);

    layoutView.setOnTouchListener(new OnTouchListener() {
        public boolean onTouch(View v, MotionEvent event) {
            InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            inputManager.hideSoftInputFromWindow(txtUserName
                    .getWindowToken(), 0);
            return true;
        }
    });
}

Inside the main LinearLayout, I am using other two LinearLayouts. The issue that i faced with the above code is that at some points when I pressed, the keyboard doesn't hides. My doubt is that I am adding touch listener only with root layout, not giving touch listener with other inner layouts or other controls(TextView). When I touch over other controls or some points around the TextView(ie, inner layouts), keyboard doesn't hides.

That means do i need to add touchListener to all layouts or controls inside the root layout? How this situation can be handled in a better way?

like image 444
sree_iphonedev Avatar asked May 11 '12 11:05

sree_iphonedev


People also ask

How do you hide on keyboard?

You can force Android to hide the virtual keyboard using the InputMethodManager, calling hideSoftInputFromWindow , passing in the token of the window containing your focused view. This will force the keyboard to be hidden in all situations. In some cases, you will want to pass in InputMethodManager.

How do I hide the keyboard on Android touch outside?

Ok everyone knows that to hide a keyboard you need to implement: InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE); imm. hideSoftInputFromWindow(getCurrentFocus(). getWindowToken(), 0);


2 Answers

You can use this code

InputMethodManager imm = 
    (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(mEditView.getWindowToken(), 0);
like image 119
waseemwk Avatar answered Oct 21 '22 05:10

waseemwk


My answer on this question:

Add this method:

public static void hideSoftKeyboard(Activity activity) {
  InputMethodManager inputMethodManager = (InputMethodManager)  activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
  inputMethodManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
}

If you want to hide keyboard when you touch screen, you can do by this way:

@Override
public boolean onTouchEvent(MotionEvent event) {
 hideSoftKeyboard(LoginActivity.this);
 return false;
}

Hope this will help you.

like image 20
Hiren Patel Avatar answered Oct 21 '22 05:10

Hiren Patel