I want to hide the keyboard by tapping outside of edittext. 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 tap twice to hide keyboard. The first tap just changes "next" (for last edittext it's "done") to "enter" icon, then the second tap hides the keyboard. This is what that happen by the first tap:
Now I have two questions:
How can I fix it and hide keyboard by just one tap?
Is it possible to do it for all of my edittext (one code for all)?
I use below Process. It's Working For Me Perfectly. Add below function in your activity Class.
You can check Focus status using Below Code. Both activity and Fragment
Try to replace
onClick
withonTouch
. For this you need to change your layout attributes like this:Then remove
rl_main_onClick(View view) {...}
method and insertonTouch
listener method inside ofonCreate()
:Kotlin version with extension
And call it like this from fragment
Or like this from activity, contentView is the id of my root view