You can use AndroidViewClient/culebra and forget about orientation, different screen sizes, etc.
As an example, let's say we want to enter text and press OK in this dialog (part of Api Demos)

just run
culebra -uGo myscript.py
when the window is displayed, click on the entry, type the text, then click OK and this script will be automatically generated
#! /usr/bin/env python
# -*- coding: utf-8 -*-
'''
Copyright (C) 2013-2016 Diego Torres Milano
Created on 2016-06-10 by Culebra v11.5.8
__ __ __ __
/ \ / \ / \ / \
____________________/ __\/ __\/ __\/ __\_____________________________
___________________/ /__/ /__/ /__/ /________________________________
| / \ / \ / \ / \ \___
|/ \_/ \_/ \_/ \ o \
\_____/--<
@author: Diego Torres Milano
@author: Jennifer E. Swofford (ascii art snake)
'''
import re
import sys
import os
try:
sys.path.insert(0, os.path.join(os.environ['ANDROID_VIEW_CLIENT_HOME'], 'src'))
except:
pass
from com.dtmilano.android.viewclient import ViewClient
TAG = 'CULEBRA'
_s = 5
_v = '--verbose' in sys.argv
kwargs1 = {'ignoreversioncheck': False, 'verbose': False, 'ignoresecuredevice': False}
device, serialno = ViewClient.connectToDeviceOrExit(**kwargs1)
kwargs2 = {'forceviewserveruse': False, 'useuiautomatorhelper': False, 'ignoreuiautomatorkilled': True, 'autodump': False, 'startviewserver': True, 'compresseddump': True}
vc = ViewClient(device, serialno, **kwargs2)
#vc.dump(window='-1') # FIXME: seems not needed
vc.dump(window=-1)
vc.findViewByIdOrRaise("com.example.android.apis:id/username_edit").setText(u"hello culebra!")
vc.sleep(_s)
vc.dump(window=-1)
vc.findViewWithTextOrRaise(u'OK').touch()
when run, it will set the text to hello culebra!
and touch OK. You can then adapt the generated script to your needs.
CulebraTester
CulebraTester is a new implementation of culebra as a service that runs on the device under test.
It's now under closed beta testing but if you are interested in participating you can find the opt-in form at culebra.dtmilano.com (check under Support).
One of its features is to be able to detect the virtual keyboard and treat it the same as other Views.

This screenshot illustrates the test generated after touching A, b, c, and Next (which is what your are looking for).
The generated test is like this
/**
* @@Test comment here@@
*
* @throws Exception
*/
@Test
public void culebraGeneratedTest() throws Exception {
mDevice.findObject(By.clazz(Pattern.compile(".*")).desc("A").pkg("com.android.inputmethod.latin")).clickAndWait(Until.newWindow(), DEFAULT_TIMEOUT);
mDevice.findObject(By.clazz(Pattern.compile(".*")).desc("b").pkg("com.android.inputmethod.latin")).clickAndWait(Until.newWindow(), DEFAULT_TIMEOUT);
mDevice.findObject(By.clazz(Pattern.compile(".*")).desc("c").pkg("com.android.inputmethod.latin")).clickAndWait(Until.newWindow(), DEFAULT_TIMEOUT);
mDevice.findObject(By.clazz(Pattern.compile(".*")).desc("Next").pkg("com.android.inputmethod.latin")).clickAndWait(Until.newWindow(), DEFAULT_TIMEOUT);
}
and you can compile, install and run as any other UiAutomator test.
As an additional example, this screenshot show how as you hover the virtual keyboard Views in the tree they are highlighted in the device representation

->|
if you're entering some data, [OK] in the password field, but the meaning is identical, finish text entry and proceed.IME_ACTION
s.