0

I'm new to mobile automation and I've been trying to find a way to close the soft keyboard on Android (using Java). The best solution I've come across so far was from this post:

Close/hide the Android Soft Keyboard

The function that I'm trying to use from there is:

public static void hideKeyboard(Activity activity) {
    InputMethodManager imm = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
    //Find the currently focused view, so we can grab the correct window token from it.
    View view = activity.getCurrentFocus();
    //If no view currently has focus, create a new one, just so we can grab a window token from it
    if (view == null) {
        view = new View(activity);
    }
    imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}

I want to be able to call this function when the keyboard pops up, but what I don't understand is how to pass through the Activity when I call this function?

Before calling the function to close the keyboard I have the following:

    // Create object of  DesiredCapabilities class and specify android platform
    DesiredCapabilities capabilities = DesiredCapabilities.android();


    // set the capability to execute test in android app
    capabilities.setCapability(MobileCapabilityType.PLATFORM, Platform.ANDROID);
    capabilities.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android");
    capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "Emulator_1");
    capabilities.setCapability(MobileCapabilityType.VERSION, "8.0");
    capabilities.setCapability("appPackage", "com.spreeza.shop.stag.debug");
    capabilities.setCapability("appActivity", "com.spreeza.shop.ui.features.splash.EntryPointActivity");

    driver = new RemoteWebDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);

// click on the login button
driver.findElement(By.id(identifierName)).click();

// close the keyboard
hideKeyboard();
ChrisG29
  • 1,129
  • 3
  • 17
  • 36

4 Answers4

0

Activity => activity mean a reference of a Activity object.

  1. Call the method as below .

    hideKeyboard(YourActivity.this);

Checkout your Activity name and replace YourActivity with the name .

Piash Sarker
  • 446
  • 5
  • 20
0

You can call the function from Activity as hideKeyboard(MainActivity.this) and from Fragment as hideKeyboard((Activity)getActivity())

Yamini Balakrishnan
  • 1,826
  • 13
  • 23
0

I managed to find a simple solution that works for me:

http://discuss.appium.io/t/can-we-hide-android-soft-keyboard/6956/4

I just added in these 2 capabilities:

capabilities.setCapability("unicodeKeyboard", true);
capabilities.setCapability("resetKeyboard", true);
ChrisG29
  • 1,129
  • 3
  • 17
  • 36
0
    /**
     * Hide the keyboard
     */
    private void hideKeyboard() {
        View view = getCurrentFocus();
        if (view != null) {
            ((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE)).
                    hideSoftInputFromWindow(view.getWindowToken(), 0);
        }
    }
kubs
  • 117
  • 9