Reputation: 61
I have Login screen , which contains email and password to be enter with submit button at the bottom.As per the requirement whenever soft keyboard is enabled , i am moving the submit button to top,i.e,showing submit button to the user above soft keyboard and below my email/password layout using the below code.
mainrel.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
Rect r = new Rect();
mainrel.getWindowVisibleDisplayFrame(r);
int heightDiff = mainrel.getRootView().getHeight() - (r.bottom - r.top);
if (heightDiff > 128 && heightDiff != 146) {
//KeyBoard Enabled
moveButtonTo_Top();
} else {
//KeyBoard Disabled
moveButtonTo_Down();
}
}
});
//Floating down the button
private void moveButtonTo_Down() {
RelativeLayout.LayoutParams relativeparams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
relativeparams.addRule(RelativeLayout.CENTER_HORIZONTAL);
relativeparams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
relativeparams.setMargins(0, 0, 0, Constants.NEXT_LAYOUT_MARGIN_TOP);
submitLay.setLayoutParams(relativeparams);
}
//Floating Up button
private void moveButtonTo_Top() {
RelativeLayout.LayoutParams relativeparams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
relativeparams.addRule(RelativeLayout.BELOW, R.id.mainLinearlay);
relativeparams.setMargins(0, 20, 0, 0);
relativeparams.addRule(RelativeLayout.CENTER_HORIZONTAL);
submitLay.setLayoutParams(relativeparams);
}
Every thing works fine while running my application.But the problem is while running espresso UI tests i am not able to see the softkeyboard for entering values in email edittext. Here i am mentioning my Espresso Ui test case code.
public class LoginActivityTest { @Rule public ActivityTestRule mActivityRule = new ActivityTestRule(LoginActivity.class);
@Test
public void login() {
onView(withId(R.id.ripplebtn_step)).perform(click());
Utils.sleep(3000);
//Sign In
onView(withId(R.id.edt_email)).perform(typeText(TestCaseConstants.CUSTOM_SIGNIN_USER_EMAIL)).perform(closeSoftKeyboard());
onView(withId(R.id.edt_password)).perform(typeText(TestCaseConstants.CUSTOM_SIGNIN_PASSWORD)).perform(closeSoftKeyboard());
onView(withId(R.id.txt_submit)).perform(click());
}
*** Note : If i comment global layout listener for my layout i.e, mainrel in my activity , then i am able to execute the test case. I think there is problem with my button movement to up and down using OnGlobalLayoutListener.
Can anyone please suggest / help me ?
public class SampleTest {
@Rule
public IntentsTestRule<StepSignInActivity> mAddIntentsTestRule =
new IntentsTestRule<StepSignInActivity>(SignInActivity.class);
IdlingResource idlingResource;
@Before
public void before() {
idlingResource = new ElapsedTimeIdlingResourc`enter code here`e(5000);
Espresso.registerIdlingResources(idlingResource);
}
@After
public void after() {
Espresso.unregisterIdlingResources(idlingResource);
}
@Test
public void runSequence() {
// this triggers our intent service, as we registered
// Espresso for it, Espresso wait for it to finish
onView(withId(R.id.edt_email)).perform(typeText(TestCaseConstants.CUSTOM_SIGNIN_USER_EMAIL));
onView(withId(R.id.edt_password)).perform(typeText(TestCaseConstants.CUSTOM_SIGNIN_PASSWORD)).perform(closeSoftKeyboard());
onView(withId(R.id.txt_submit)).perform(click());
}
Upvotes: 3
Views: 2320
Reputation: 494
There is a keyboard setting for showing the virtual keyboard even when typing is performed via a hardware keyboard (which the tests pretend to use). It can be set via ADB
adb shell settings put secure show_ime_with_hard_keyboard 1
or accessed in the navigation bar when the cursor is in a text field:
Then set the "Show virtual keyboard" option:
This should show the keyboard while in the text field, but it will automatically close once the focus leaves the text field (which is not the case for real phones).
Upvotes: 1
Reputation: 2246
Try This:
@Rule
public IntentsTestRule<YourActivity> mAddIntentsTestRule =
new IntentsTestRule<>(YourActivity.class)
@Before
public void registerIdlingResource() {
Espresso.registerIdlingResources(
mAddIntentsTestRule.getActivity().getCountingIdlingResource());
}
/**
* Unregister your Idling Resource so it can be garbage collected and does not leak any memory.
*/
@After
public void unregisterIdlingResource() {
Espresso.unregisterIdlingResources(
mAddIntentsTestRule.getActivity().getCountingIdlingResource());
}
Upvotes: 0