Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Ok everyone knows that to hide a keyboard you need to implement:

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

But the big deal here is how to hide the keyboard when the user touches or selects any other place that is not an EditText or the softKeyboard?

I tried to use the onTouchEvent() on my parent Activity but that only works if user touches outside any other view and there is no scrollview.

I tried to implement a touch, click, focus listener without any success.

I even tried to implement my own scrollview to intercept touch events but I can only get the coordinates of the event and not the view clicked.

Is there a standard way to do this?? in iPhone it was really easy.

like image 740
htafoya Avatar asked Nov 12 '10 14:11

htafoya


People also ask

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

This example demonstrates how to hide a soft keyboard on android after clicking outside EditText using Kotlin. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.

How do I hide soft keyboard when Edittext is focused?

Hiding the Soft Keyboard Programmatically You can force Android to hide the virtual keyboard using the InputMethodManager, calling hideSoftInputFromWindow, passing in the token of the window containing your edit field. This will force the keyboard to be hidden in all situations.

How do I make my keyboard invisible on Android?

You can force Android to hide the virtual keyboard using the InputMethodManager, calling hideSoftInputFromWindow , passing in the token of the window containing your focused view. This will force the keyboard to be hidden in all situations. In some cases, you will want to pass in InputMethodManager.


2 Answers

The following snippet simply hides the keyboard:

public static void hideSoftKeyboard(Activity activity) {     InputMethodManager inputMethodManager =          (InputMethodManager) activity.getSystemService(             Activity.INPUT_METHOD_SERVICE);     if(inputMethodManager.isAcceptingText()){         inputMethodManager.hideSoftInputFromWindow(                 activity.getCurrentFocus().getWindowToken(),                 0         );     } } 

You can put this up in a utility class, or if you are defining it within an activity, avoid the activity parameter, or call hideSoftKeyboard(this).

The trickiest part is when to call it. You can write a method that iterates through every View in your activity, and check if it is an instanceof EditText if it is not register a setOnTouchListener to that component and everything will fall in place. In case you are wondering how to do that, it is in fact quite simple. Here is what you do, you write a recursive method like the following, in fact you can use this to do anything, like setup custom typefaces etc... Here is the method

public void setupUI(View view) {      // Set up touch listener for non-text box views to hide keyboard.     if (!(view instanceof EditText)) {         view.setOnTouchListener(new OnTouchListener() {             public boolean onTouch(View v, MotionEvent event) {                 hideSoftKeyboard(MyActivity.this);                 return false;             }         });     }      //If a layout container, iterate over children and seed recursion.     if (view instanceof ViewGroup) {         for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {             View innerView = ((ViewGroup) view).getChildAt(i);             setupUI(innerView);         }     } } 

That is all, just call this method after you setContentView in your activity. In case you are wondering what parameter you would pass, it is the id of the parent container. Assign an id to your parent container like

<RelativeLayoutPanel android:id="@+id/parent"> ... </RelativeLayout>

and call setupUI(findViewById(R.id.parent)), that is all.

If you want to use this effectively, you may create an extended Activity and put this method in, and make all other activities in your application extend this activity and call its setupUI() in the onCreate() method.

Hope it helps.

If you use more than 1 activity define common id to parent layout like <RelativeLayout android:id="@+id/main_parent"> ... </RelativeLayout>

Then extend a class from Activity and define setupUI(findViewById(R.id.main_parent)) Within its OnResume() and extend this class instead of ``Activity in your program


Here is a Kotlin version of the above function:

@file:JvmName("KeyboardUtils")  fun Activity.hideSoftKeyboard() {     currentFocus?.let {         val inputMethodManager = ContextCompat.getSystemService(this, InputMethodManager::class.java)!!         inputMethodManager.hideSoftInputFromWindow(it.windowToken, 0)     } } 
like image 82
Navneeth G Avatar answered Oct 22 '22 02:10

Navneeth G


You can achieve this by doing the following steps:

  1. Make the parent view(content view of your activity) clickable and focusable by adding the following attributes

        android:clickable="true"      android:focusableInTouchMode="true"  
  2. Implement a hideKeyboard() method

        public void hideKeyboard(View view) {         InputMethodManager inputMethodManager =(InputMethodManager)getSystemService(Activity.INPUT_METHOD_SERVICE);         inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);     } 
  3. Lastly, set the onFocusChangeListener of your edittext.

        edittext.setOnFocusChangeListener(new View.OnFocusChangeListener() {         @Override         public void onFocusChange(View v, boolean hasFocus) {             if (!hasFocus) {                 hideKeyboard(v);             }         }     }); 

As pointed out in one of the comments below, this might not work if the parent view is a ScrollView. For such case, the clickable and focusableInTouchMode may be added on the view directly under the ScrollView.

like image 41
vida Avatar answered Oct 22 '22 01:10

vida