Q:

Detect or Suppress Keyboard in UiAutomator Tests

In my UI tests I am setting the text of two UiObjects near the top of the screen, and clicking on third UiObject which is located near the bottom of the screen. On some devices (eg my Nexus 6 running 5.0) this works perfectly. The text is set, the UiObject at the bottom of the screen is still visible, and it is successfully clicked on.
On other devices (eg my Nexus 4 running 4.4) calling setText on the UiObjects brings up the soft keyboard, which obscures the third UiObject near the bottom of the screen and prevents it from being clicked on.
I considered using UiDevice.pressBack() to dismiss the keyboard, but the problem is that the keyboard shows on some devices and not others. Pressing back on the devices that do not show the keyboard causes other behavior which leads to the test failing.
I also considered using UiDevice.pressEnter() as a solution. I figured this would dismiss the keyboard if it was visible, and do nothing if it wasn't (best of both worlds). The problem is, that the keyboard shown by UiAutomator when it is running my tests does not have an "enter" button, it has a "next" button. This is contrary to the way I have the actual UI elements setup in code though. When I test this manually the keyboard shows the "enter" button as expected. However, since UiAutomator is seeing the "next" button, that is the functionality it is executing. The focus simply passes to the next UiObject and the keyboard is still visible.
So what I am trying to do is dismiss the keyboard IF it is visible, without risking pressing the back button if it is not. Is there a way to determine if it is showing or not? Or better yet, never show it in the first place?

A:

Different device have different packages, and keyboards function may different too.
So, the details may not be all the same.
Steps listed below are succeed in Android 4.0+ on my device:

Disable packages relative to keyboard input method.
The commands are listed here:
adb root
adb shell pm disable com.google.android.apps.inputmethod.hindi
adb shell pm disable com.google.android.inputmethod.korean
adb shell pm disable com.google.android.inputmethod.pinyin
adb shell pm disable com.google.android.inputmethod.latin
adb shell pm disable jp.co.omronsoft.iwnnime.ml
pause

Note: Some package name like "com.google.android.inputmethod.pinyin" may not exist in device.
Use this command to check:
adb shell pm list packages inputmethod

Disable the "Google voice typing"
"Google voice typing" can still popup the keyboard.
If you want to disable keyboard:
Go to Settings → Apps → Running (or All) → "Google Keyboard" → Settings → Disable "Google voice typing".

Note: If you want enable the keyboard, run command like this:
adb shell pm enable com.google.android.inputmethod.latin

