Unable to hide Android soft keyboard even with input manager

Problem:

I want to hide the keyboard when the "Add" button is clicked. There are two EditText on the screen. The keyboard does not appear when the operation starts, which is good, but it does not disappear when the button is pressed.

enter image description here

Here are all the possible questions from Stack Overflow that I saw, the answers to which do not help me:

Close / Hide Android Soft Keyboard

Programmatically hide / show Android soft keyboard

How to hide Soft Keyboard when starting activity

How to hide soft keyboard on android by clicking outside EditText?

and many others.

Here is my code:

Addactivity

 public class AddActivity extends ActionBarActivity { EditText text1,text2; DbHelper db; ListView l; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_add); db = new DbHelper(this); l = (ListView) findViewById(R.id.listInAddActivity); text1 = (EditText) findViewById(R.id.i1); text2 = (EditText) findViewById(R.id.i2); // text1.setInputType(InputType.TYPE_NULL); // text2.setInputType(InputType.TYPE_NULL); hideKeyboard(); loadDataInAdd(); } public void addNewTask(View view) { String s1 = text1.getText().toString(); String s2 = text2.getText().toString(); db.addData(s1,s2); loadDataInAdd(); hideKeyboard(); } public void loadDataInAdd() { try { Cursor cursor = db.fetchData(); ListAdapter myAdapter = new SimpleCursorAdapter(this, R.layout.tasks, cursor, new String[]{db._ID, db.COLUMN_1, db.COLUMN_2}, new int[]{R.id.idnum, R.id.c1, R.id.c2}, 0); l.setAdapter(myAdapter); } catch(NullPointerException e) { e.printStackTrace(); } // MainActivity.loadData(); } private void hideKeyboard() { // Check if no view has focus: View view = this.getCurrentFocus(); if (view != null) { InputMethodManager inputManager = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE); inputManager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS); } } } 

Most answers relate to the InputManager method, but it does not work for me, i.e. does not hide the keyboard when you click on the button. Here is another variant of InputManager I:

 View view = this.getCurrentFocus(); if (view != null) { InputMethodManager inputManager = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE); inputManager.hideSoftInputFromWindow(view.getWindowToken(), 0); 

I also tried this:

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

and this also does not work (the keyboard is not hidden when the button is pressed).

I also tried:

  <activity android:name=".AddActivity" android:label="@string/app_name" android:parentActivityName=".MainActivity" android:windowSoftInputMode="stateAlwaysHidden"> </activity> 

and

  <activity android:name=".AddActivity" android:label="@string/app_name" android:parentActivityName=".MainActivity" android:windowSoftInputMode="stateAlwaysHidden"> </activity> 

With this application, my application stopped working:

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

This completely hid the keyboard (the keyboard did not display even when EditText pressed):

 text1.setInputType(InputType.TYPE_NULL); text2.setInputType(InputType.TYPE_NULL); 
+6
source share
2 answers

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

I was halfway to my question when I decided to try onclicklistener again. After several random attempts, the following 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 as follows:

  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 above, put this code in protected void onCreate(Bundle savedInstanceState) :

    btn.setOnClickListener (mListener);

  • Use try-catch to handle exceptions in both methods ( onclicklistener , setOnClickListener )

When searching for the reason onClick the XML attribute does not work for me, but onclicklistener , I found the following useful links:

setOnclickListener vs OnClickListener vs View.OnClickListener

Android onClick in XML and 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 onClick from XML. Here is what I did to get rid of onclicklistener s:

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

Some Important Points onClick Using onClick

  • The method must be public and void .

  • It must accept 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 the 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, 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); } } 
+6
source

check this

 View view = this.getCurrentFocus(); if (view != null) { InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(view.getWindowToken(), 0); } 

Close / Hide Android Soft Keyboard

+1
source

All Articles