Hide default keyboard when clicking on android

I want to hide the soft keyboard when I exit the editing window on the screen. How can i do this?

+16
android
Oct 23 '10 at 19:41
source share
10 answers

To force the keyboard to hide, you must use the following code ... I put it in a method called "hideSoftKeyboard ()". As already mentioned, Falmarri, the on-screen keyboard should be hiding when you exit it. However, if you call this method in the "onClick ()" of another element, it forcibly closes the keyboard.

private void hideSoftKeyboard(){ if(getCurrentFocus()!=null && getCurrentFocus() instanceof EditText){ InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(yourEditTextHere.getWindowToken(), 0); } } 
+31
Oct 24 '10 at 16:22
source share

I had to edit this to make it work. Check added to see if the focused view is EditText.

 @Override public boolean dispatchTouchEvent(MotionEvent event) { View v = getCurrentFocus(); boolean ret = super.dispatchTouchEvent(event); if (v instanceof EditText) { View w = getCurrentFocus(); int scrcoords[] = new int[2]; w.getLocationOnScreen(scrcoords); float x = event.getRawX() + w.getLeft() - scrcoords[0]; float y = event.getRawY() + w.getTop() - scrcoords[1]; Log.d("Activity", "Touch event "+event.getRawX()+","+event.getRawY()+" "+x+","+y+" rect "+w.getLeft()+","+w.getTop()+","+w.getRight()+","+w.getBottom()+" coords "+scrcoords[0]+","+scrcoords[1]); if (event.getAction() == MotionEvent.ACTION_UP && (x < w.getLeft() || x >= w.getRight() || y < w.getTop() || y > w.getBottom()) ) { InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(getWindow().getCurrentFocus().getWindowToken(), 0); } } return ret; } 

It may be possible to do it in a smoother way, but it works very well.

+57
Aug 30 '11 at 10:24
source share

I added the following to my work. This works because touching the external Focusable View does not change focus (so w == v), but the touch will be outside the View rectangle.

 public boolean dispatchTouchEvent(MotionEvent event) { View v = getCurrentFocus(); boolean ret = super.dispatchTouchEvent(event); View w = getCurrentFocus(); int scrcoords[] = new int[2]; w.getLocationOnScreen(scrcoords); float x = event.getRawX() + w.getLeft() - scrcoords[0]; float y = event.getRawY() + w.getTop() - scrcoords[1]; Log.d("Activity", "Touch event "+event.getRawX()+","+event.getRawY()+" "+x+","+y+" rect "+w.getLeft()+","+w.getTop()+","+w.getRight()+","+w.getBottom()+" coords "+scrcoords[0]+","+scrcoords[1]); if (event.getAction() == MotionEvent.ACTION_UP && (x < w.getLeft() || x >= w.getRight() || y < w.getTop() || y > w.getBottom()) ) { inputManager.hideSoftInputFromWindow(getWindow().getCurrentFocus().getWindowToken(), 0); } return ret; } 

[edit: fix minor bug]

+6
Jun 20 2018-11-11T00:
source share

This can be done using the following code:

1) Take the link of your parent layout in java code using findViewById ().

2), then apply setOnTouchListener () to it.

3) Add the following code to onTouchMethod ().

  lin = (LinearLayout) findViewById(R.id.lin); lin.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(getWindow().getCurrentFocus().getWindowToken(), 0); return false; } }); 
+6
Aug 09 2018-12-12T00:
source share
 public boolean OutsideTouchEvent(MotionEvent m_event) { View v = getCurrentFocus(); boolean value = super.dispatchTouchEvent(m_event); View w = getCurrentFocus(); int scrcoords[] = new int[2]; w.getLocationOnScreen(scrcoords); float x = m_event.getRawX() + w.getLeft() - scrcoords[0]; float y = m_event.getRawY() + w.getTop() - scrcoords[1]; if (m_event.getAction() == MotionEvent.ACTION_UP && (x < w.getLeft() || x >= w.getRight() || y < w.getTop() || y > w.getBottom()) ) { InputMethodManager inputMethodManager = (InputMethodManager) YourActivity.this.getSystemService(Activity.INPUT_METHOD_SERVICE); inputMethodManager.hideSoftInputFromWindow(YourActivity.this.getCurrentFocus().getWindowToken(), 0); } return value; } 
0
Feb 14 '13 at 6:12
source share

set the input type to zero for text input editText.setInputType(0);

he works for me

0
Jul 16 '13 at 5:42 on
source share

First of all, thanks to Daniel, his code is really good, and I used it for a while.

I recently realized that I had to improve it. The problem was scrolling the page. I had a lot of EditText in my project, and it hid the keyboard while scrolling the page.

I came up with a solution using onGestureListener instead of overriding dispatchTouchEvent.

 public class TabActivity extends ActionBarActivity implements GestureDetector.OnGestureListener { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ... ... gestureScanner = new GestureDetector(TabActivity.this,this); } @Override public boolean dispatchTouchEvent(MotionEvent ev) { gestureScanner.onTouchEvent(ev); return super.dispatchTouchEvent(ev); } @Override public boolean onSingleTapUp(MotionEvent event) { View v = getCurrentFocus(); if (v instanceof EditText) { View w = getCurrentFocus(); int scrcoords[] = new int[2]; w.getLocationOnScreen(scrcoords); boolean hide = true; View view = ((ViewGroup)findViewById(android.R.id.content)).getChildAt(0); ArrayList<View> editTexts = view.getFocusables(0); // Get All EditTexts in view for(int i=0; i< editTexts.size(); i++){ View editText = editTexts.get(i); editText.getLocationOnScreen(scrcoords); float x = event.getRawX(); float y = event.getRawY(); int viewX = scrcoords[0]; int viewY = scrcoords[1]; // If touch is in any of EditText, keep keyboard active, otherwise hide it. if (event.getAction() == MotionEvent.ACTION_UP && ( x > viewX && x < (viewX + editText.getWidth())) && ( y > viewY && y < (viewY + editText.getHeight())) ) { hide = false; } } if (hide) { InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(getWindow().getCurrentFocus().getWindowToken(), 0); } } return true; } @Override public boolean onScroll(MotionEvent event, MotionEvent e2, float distanceX, float distanceY) { return true; } } 

So, if the user scrolls the page, he goes to the onScroll method and he does nothing. If users simply touch the screen, it calls the onSingleTapUp method.

I also had to change if you provided Daniel code. Daniel checked to see if the touch event is outside of EditText . Since I have many EditViews , I changed the code to find out if the touch event is inside any of the EditText s.

It works great with me, let me know about any improvements or bugs.

0
Feb 17 '14 at 21:25
source share

Just set the input type to null

editText.setInputType(InputType.TYPE_NULL);

0
Sep 12 '15 at 9:57
source share

As a complement to the accepted answer.

If the accepted answer does not work for you, you can add the hideSoftKeyboard() method to the onClick() onClickListener your EditText . For example:

 editText.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { hideSoftKeyboard(); } }); 

(put the above code in onResume() or onResume() else)

ps. hideSoftKeyboard() definition

 private void hideSoftKeyboard(){ if(getCurrentFocus()!=null && getCurrentFocus() instanceof EditText){ InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(editText.getWindowToken(), 0); } } 
0
Nov 17 '15 at 4:51 on
source share

I got a good solution. I know it too late, but when searching in most cases, get this link as the first link. therefore, it may be useful to others. If you click on any text / button, it will hide the toolbar that is already visible.

 date.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // Hide soft keyboard InputMethodManager imm = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE); imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0); // here i am showing the Date Dialog. one can proceed with their functionality //show date picker dialog showDialog(Date_DIALOG_ID); } }); 
0
May 27 '16 at 6:58 a.m.
source share



All Articles