How to Hide Soft Keyboard on Android After Clicking Outside Edittext

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

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)
}
}

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

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)
}
}

How to hide keyboard just by one tap outside of an edittext?

Try to replace onClick with onTouch. For this you need to change your layout attributes like this:

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

<RelativeLayout>

// widgets here

</RelativeLayout>

</RelativeLayout>

Then remove rl_main_onClick(View view) {...} method and insert onTouch listener method inside of 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;
}
});

Hide Android soft keyboard in Fragment when clicked on outside

In the parent Activity of the fragment override the following method:

 @Override
public boolean dispatchTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
View v = getCurrentFocus();
if ( v instanceof EditText) {
Rect outRect = new Rect();
v.getGlobalVisibleRect(outRect);
if (!outRect.contains((int)event.getRawX(), (int)event.getRawY())) {
v.clearFocus();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
}
}
}
return super.dispatchTouchEvent(event);
}

And in the layout of the fragment use this attribute:

android:focusableInTouchMode="true"

Hope this will help you.

Close keyboard on click outside of EditText in fragment

Code to dismiss Softkeyboard is below:

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

You can put it in Utility Class or if you are defining it within an activity, avoid the activity parameter, or call hideSoftKeyboard(this).

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.

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();
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);
}
}
}

Call this method after SetcontentView() with paramet as id of your view like:

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

Then call `setupUI(findViewById(R.id.parent))

Reference : Hide keypad in android while touching outside Edit Text Area

Solution for hiding soft keyboard when touching outside of any part of screen in android

This code helps to hide the keyboard:

public static void hideSoftKeyboard(Activity activity){
try{
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(
getCurrentFocus().getWindowToken(),
0
);
}catch(Exception e){
e.printStackTrace();
}

}

hope this works!

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

Try to get EditTesxt focus status with setOnFocusChangeListener and if it's not focused hide the keyboard:

    editText.setOnFocusChangeListener((v, hasFocus) -> {
if (!hasFocus) {
InputMethodManager imm = (InputMethodManager) context.getSystemService(Activity.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
});

how to hide keypad when click outside editText?

For this you have to take the onTouchListener on the parent layout of the Layout File. on the TouchListener you have to code to hide the Keyboard when click outside the EditText. Please follow XML Layout and Java class to resolve this issue please follow following url.

http://amitthaperandroidquery.blogspot.com/2011/10/remove-keyboard-after-click-outside.html



Related Topics



Leave a reply



Submit