Cannot hide Android soft keyboard even with inputmanager

I decided to use an onclicklistener for my button, instead of using another function and calling it via onClick in AddActivity.xml.

I was halfway through my question when I decided to try using OnCliCkListener once again. After several random tries, the following is the final code that works for me:

btn.setOnClickListener(new View.OnClickListener() {


        public void onClick(View v) {
            // TODO Auto-generated method stub

                InputMethodManager inputManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
                inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);

        }
    });

Another way is to use OnClickListener in the following manner:

    private View.OnClickListener mListener = new View.OnClickListener() {
    public void onClick(View v) {
        // do something when the button is clicked

        //public void onClick(View v) {

            try {
                InputMethodManager inputManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
                inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
            } 
 catch (Exception e) {
      e.printStackTrace();
            }

    }
};

Important points for beginners like me:

  • The above code should be placed before protected void onCreate(Bundle savedInstanceState)
  • To call the above, place this code in protected void onCreate(Bundle savedInstanceState):

    btn.setOnClickListener(mListener);

  • Use try-catch for exception handling in both methods (onClickListener, setOnClickListener)

Upon searching for why onClick, the XML attribute doesn't work for me, while OnClickListener does, I have found the following links useful:

setOnclickListener vs OnClickListener vs View.OnClickListener

Android onClick in XML vs. OnClickListener

How exactly does the android:onClick XML attribute differ from setOnClickListener?

Difference between OnClick() event and OnClickListener?

Now, halfway through my answer, I realized my mistake in using the onClick from XML. Here is what I did to get rid of the OnClickListeners:

  • First, I moved the part of my code which hid the keyboard into the method in onClick. Here in my case, I moved the entire code inside hideKeyboard() to addNewTask.

Some important points regarding using onClick

  • The method should be public and void.

  • It should take a View parameter, such as View V.

Finally, the code that works:

  public void addNewTask(View view) {
    String s1 = text1.getText().toString();
    String s2 = text2.getText().toString();
    db.addData(s1, s2);
    loadDataInAdd();
  //  hideKeyboard(); below is the code to hide keyboard
    if (view != null) {
        InputMethodManager inputManager = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
        inputManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
    }
}

Summary

I found 3 ways to hide soft keyboard for my case, using:

  • OnClickListener :

    private View.OnClickListener mListener = new View.OnClickListener() {
    public void onClick(View v) {
        // do something when the button is clicked
    
        //public void onClick(View v) {
    
            try {
                InputMethodManager inputManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
                inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
            } 
    
       catch (Exception e) {
      e.printStackTrace();
            }
    
    }
    };
    
  • setOnClickListener:

      btn.setOnClickListener(new View.OnClickListener() {
    
    
        public void onClick(View v) {
            // TODO Auto-generated method stub
    
                InputMethodManager inputManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
                inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
    
        }
    });
    
  • onClick, the XML attribute:

    public void addNewTask(View view) {
    String s1 = text1.getText().toString();
    String s2 = text2.getText().toString();
    db.addData(s1, s2);
    loadDataInAdd();
    //  hideKeyboard();
    if (view != null) {
        InputMethodManager inputManager = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
        inputManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
    }
    }
    

this will help you to hide your keyboard on startup until you touch the edit text.

getWindow().setSoftInputMode(
        WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN
    );

in your case, try this

btn.setOnClickListener(new View.OnClickListener() {


        public void onClick(View v) {
             getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN); 

        }
    });