Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

hide keyboard when user taps anywhere else on the screen in android

I need to hide the softkeypad in android when user click on anywhere other than a Edittext. There are many help for iphone but not for android. I tried this code but its not working :(

final RelativeLayout base = (RelativeLayout) findViewById(R.id.RelativeLayout1);      findViewById(R.id.base).setOnClickListener(new OnClickListener() {          @Override         public void onClick(View v) {             InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);             imm.hideSoftInputFromWindow(base.getWindowToken(), 0);          }     }); 

Thanks in advance !

like image 520
Chrishan Avatar asked Jan 02 '12 04:01

Chrishan


People also ask

How do I hide my keyboard from clicking?

To hide keyboard, use the following code. InputMethodManager inputMethodManager = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE); inputMethodManager. hideSoftInputFromWindow(v. getApplicationWindowToken(),0);

How do I hide the soft keyboard on Android after clicking outside EditText?

and put the following code in the onTouch method. InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE); imm. hideSoftInputFromWindow(getCurrentFocus(). getWindowToken(), 0);

How do I make my keyboard always visible on Android?

You must have an EditText in your layout and that need to extent EditText base class. then Override onKeyPreIme() method, and return True. Now your keyboard will be always visible and can't be dismissed by Back key.


1 Answers

You can use the onTouchEvent() to hide the Softkeyboard.

@Override     public boolean onTouchEvent(MotionEvent event) {         InputMethodManager imm = (InputMethodManager)getSystemService(Context.                                                         INPUT_METHOD_SERVICE);         imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);         return true;     } 

Though this solution works, but the best I would suggest is to use below answer as it gives best solution of closing the keyboard touching anywhere else then EditText.

like image 127
Lalit Poptani Avatar answered Oct 06 '22 06:10

Lalit Poptani