How to Hide the Soft Keyboard on Android After Clicking Outside EditText

How to Hide the Soft Keyboard on Android After Clicking Outside EditText

6 June 2024 Stephan Petzl Leave a comment Tech-Help

Managing the soft keyboard on Android can be a bit tricky, especially when you want it to hide automatically when the user touches outside an EditText. This guide will walk you through a robust method to achieve this functionality using a simple yet effective approach.

Step-by-Step Solution

The following method offers a comprehensive way to hide the soft keyboard when the user touches outside the EditText. This involves setting up a touch listener on all views except the EditText. Here’s how you can do it:

1. Create a Utility Method to Hide the Keyboard

public static void hideSoftKeyboard(Activity activity) {
    InputMethodManager inputMethodManager = 
        (InputMethodManager) activity.getSystemService(
    if (inputMethodManager.isAcceptingText()) {

2. Setup Touch Listeners on Non-Text Views

public void setupUI(View view) {
    if (!(view instanceof EditText)) {
        view.setOnTouchListener(new View.OnTouchListener() {
            public boolean onTouch(View v, MotionEvent event) {
                return false;

    if (view instanceof ViewGroup) {
        for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
            View innerView = ((ViewGroup) view).getChildAt(i);

Assign an ID to your parent container in your layout file:

    ... >

Then, call the setupUI method in your activity after setting the content view:


Additional Tips

If you use multiple activities, you can create a base activity that includes this method and extend it across your application. This way, you maintain consistency and reduce redundancy:

public class BaseActivity extends Activity {
    protected void onResume() {

Ensure all parent layouts in your activities have a common ID, such as main_parent, to make this approach effective.

Kotlin Version

For Kotlin users, here is the equivalent method:

fun Activity.hideSoftKeyboard() {
    currentFocus?.let {
        val inputMethodManager = ContextCompat.getSystemService(this,!!
        inputMethodManager.hideSoftInputFromWindow(it.windowToken, 0)


By following the steps outlined above, you can ensure that the soft keyboard hides whenever the user touches outside an EditText. This improves the user experience by providing a cleaner and more intuitive interface.

Enhancing Your Development Workflow

For mobile developers looking to streamline their testing process, consider using Repeato. Repeato is a no-code test automation tool for iOS and Android that leverages computer vision and AI to create, run, and maintain automated tests quickly and efficiently. This allows developers to focus on building great products while delegating test automation to non-technical colleagues or QA teams.

For more details on how Repeato can assist you in your development process, check out our documentation and blog.

Like this article? there’s more where that came from!