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