2

I have multiple edit text and button on screen, on every button click there are some validation if validation is successful then i have to hide keyboard. I have tried so many code but nothing work. Currently i am using,

InputMethodManager imm = (InputMethodManager) cntx.getSystemService(Activity.INPUT_METHOD_SERVICE);
        imm.toggleSoftInput(InputMethodManager.HIDE_NOT_ALWAYS, 0);

This is toggle, it opens if keyboard is hidden, but i wants to hide keyboard whether it is open or not.

Mrudul
  • 21
  • 2
  • Refer this link http://stackoverflow.com/questions/1109022/close-hide-the-android-soft-keyboard – ajantha Feb 25 '16 at 09:36

3 Answers3

1

Make one function like this, and Call from anywhere by passing context like this :

public static void hideKeyBoard_WithView(Context context) {

       // Check if no view has focus:
        View view = ((Activity) context).getCurrentFocus();
        if (view != null) {
            InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
        }

    }

Hope it will help you ! :) just try !

Rahul
  • 480
  • 2
  • 8
  • Please show me your code which you are using for calling this method ! Because it's perfectly working in My Apps. – Rahul Feb 25 '16 at 10:31
  • I posted my code above... But it is toggleSoftInput so it hides keyboard when it is open and opens if it is hidden. In case of your code anything happens. If it is open it remains open and if hidden then remain hidden!! – Mrudul Feb 25 '16 at 10:41
  • can you share your Activity or class from you are calling this method ? – Rahul Feb 25 '16 at 10:44
1
edittext.setFocusable(true);
edittext.setFocusableInTouchMode(true);

//try using this.. make the parameter as false when u dont required the keyboard & write the onclick method on edit text & make parameters true to get the focus again

  • I dont have any click method on edit text, i have click on button on which i have hide keyboard which is opened. – Mrudul Feb 25 '16 at 10:11
  • keep all edittext focusable & setFousableIntouch false.. & set onclick method for every editext... when the user click on edittext it will gain the focus & keyboard will pop up & on onclick of button set edittext focussable & in touchfocusable false again. So the keyboard will only popup when a particular edittext is touched – Nilesh Mamidi Feb 25 '16 at 10:54
0

Get current focussed view and using that view window token hide your keyboard.

View view = this.getCurrentFocus(); // get current focussed edittext
    if (view != null) {  
        InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
    }
Amy
  • 3,910
  • 1
  • 17
  • 33
  • I have multiple edit text so i dont which edit text user will click last, and calling this multiple times i not good at all. – Mrudul Feb 25 '16 at 10:03
  • What do you want exactly? – Amy Feb 25 '16 at 10:23
  • write code as above you don' have to pass view explicitly. You just try it out as is it'll definitely work. – Amy Feb 25 '16 at 10:34