How to hide keyboard with one touch outside edittext?

I want to hide the keyboard by clicking outside the edit text. This is my XML code:

<RelativeLayout android:clickable="true" android:focusable="true" android:focusableInTouchMode="true" android:onClick="rl_main_onClick"> <RelativeLayout //Here there are some widgets including some edittext. </RelativeLayout> 

This is my Java code (MainActivity):

 public void rl_main_onClick(View view) { InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0); } 

But I have to double tap to hide the keyboard. The first press simply changes the “next” (for the last editing text it is “done”) to the enter icon, then the second press hides the keyboard. Here's what happens when you first click:

What the first tap does.

Now I have two questions:

  1. How can I fix it and hide the keyboard with one click?

  2. Is it possible to do this for all of my edittext (one code for all)?

+6
source share
3 answers

Try replacing onClick with onTouch . To do this, you need to change the layout attributes as follows:

 <RelativeLayout android:id="@+id/relativeLayout" android:clickable="true" android:focusable="true" android:focusableInTouchMode="true"> <RelativeLayout> // widgets here </RelativeLayout> </RelativeLayout> 

Then remove the rl_main_onClick(View view) {...} method and insert the onTouch method inside onCreate() :

 findViewById(R.id.relativeLayout).setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0); return true; } }); 
+9
source

I use the below Process. This works fine for me. Add the function below in your activity class.

  override fun dispatchTouchEvent(event: MotionEvent): Boolean { if (event.action == MotionEvent.ACTION_DOWN) { val v = currentFocus if (v is EditText) { val outRect = Rect() v.getGlobalVisibleRect(outRect) if (!outRect.contains(event.rawX.toInt(), event.rawY.toInt())) { Log.d("focus", "touchevent") v.clearFocus() val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager imm.hideSoftInputFromWindow(v.windowToken, 0) } } } return super.dispatchTouchEvent(event)} 

You can check the focus status using the code below. Both activity and fragment

 appCompatEditText.onFocusChangeListener = View.OnFocusChangeListener { view, hasFocus -> if (!hasFocus) { toast("Focus Off") }else { toast("Focus On") } } 
0
source

Kotlin version with extension

 fun View.hideKeyboard() { val inputMethodManager = context!!.getSystemService(android.content.Context.INPUT_METHOD_SERVICE) as? InputMethodManager inputMethodManager?.hideSoftInputFromWindow(this.windowToken, 0) } 

And call it that from the fragment

 view.setOnClickListener { it.hideKeyboard() } 

Or how is it from activity, the contentView is the identifier of my root view

 val contentView: View = findViewById(R.id.contentView) contentView.setOnClickListener { it.hideKeyboard() } 
0
source

Source: https://habr.com/ru/post/1259375/


All Articles