Simulating a click on a Next/Done button in Robolectric - android

In a given screen i have few controls where the first one is EditText and than RadioGroup and than a Button, at EditText i have used android:imeOptions="actionNext", now for this writing a straightforward Robolectric test case is not a big deal as
we can write
Button someButton = (Button) findViewById(R.id.some_button);
someButton.performClick();
but the question is how to automate and write a test case on clicking Next/Done button of the given softinput KeyBoard. How we can write and perform next/done Button of softinput keyboard click ?
Please guide!!

You've got probably something like this which you want to test:
getView(R.id.etPassword).setOnEditorActionListener(
new OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId,
KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_NEXT) {
checkCredentialsAndStartLoginTask();
}
return false;
}
});
This listener is called when you click on "Next" on the soft keyboard. If you want to test this simply call the onEditorAction with the adequate actionId as parameter on the EditText to simulate this soft keyboard button click. See this sample.
#Test
public void emptyUsernameShouldTriggerToast() throws Exception {
activity = controller.create().start().resume().visible().get();
// enter username and password
ui.etAccount.setText("");
ui.etPassword.setText("Battery");
// test initial state
ui.etPassword.onEditorAction(EditorInfo.IME_ACTION_NEXT);
// test Toast
ShadowHandler.idleMainLooper();
assertThat( ShadowToast.getTextOfLatestToast() ).isEqualTo(("Please enter username and password.") );
}

Related

How to customize the 'Enter' of the virtual keyboard when giving input in an Android app and also handle its click action?

I am searching for a way to customize (or handle) the 'Enter' of the virtual keyboard when some app user is giving some input in an Android app, for example, when he is pressing "Enter" in an Android app (in some EditText) or when he has completed giving his input, then at that point (or before) can I customize the 'Enter' of the virtual keyboard (like in some apps' virtual keyboard, it is "Done") to text like "Go" etc. Also, if something like some (other) action could also be possibly performed when that virtual keyboard button (like Enter) is being clicked or pressed. Any kind of help would be greatly appreciated.
You can set different imeOptions like actionDone and actionGo reference link
imeOptions can setted like this in editText
android:imeOptions="actionDone"
to customize these imeActions firstly you can change imeOptions label like this
android:imeActionLabel="#string/anyString"
You have to implements OnEditorActionListener interface and set it to your EditText
editText.setOnEditorActionListener(new OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
Toast.makeText(getApplicationContext(), "Your changes have been saved", Toast.LENGTH_SHORT).show();
}
}
});
set ImeOptions
et_input.setImeOptions(EditorInfo.IME_ACTION_DONE);
Editor Action Listener
et_input.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView view, int actionId, KeyEvent event) {
int result = actionId & EditorInfo.IME_MASK_ACTION;
switch (result) {
case EditorInfo.IME_ACTION_DONE:
break;
case EditorInfo.IME_ACTION_NEXT:
//
break;
}
return true;
}
});

Why does moving textview focus work in button function but not in onKeyListener?

I have this form where you are adding multiple names to a list. I have two edit texts, Firstname and LastName. When the add button is clicked, the focus moves from the last name back to the first name. Works pretty well:
public void onAddBtnClick(View v) {
addNameToList();
TextView txtFirstName = (TextView) findViewById(R.id.txtFirstName);
Selection.setSelection((Editable) txtFirstName.getText(), txtFirstName.getSelectionStart());
txtFirstName.requestFocus();
}
No problems here. However, I want it to do the same thing when the user hits the "Next" button within their keyboard, i.e. the enter button. Here, it does all the functions in addNameToList() and goes through the next few lines within the code. But it doesn't remove the focus from the second textview:
EditText etLastName = (EditText) findViewById(R.id.txtLastName);
etLastName.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
(keyCode == KeyEvent.KEYCODE_ENTER)) {
Button btn = (Button) findViewById(R.id.btnAdd);
btn.performClick();
}
return true;
}
});
As you see, I tried just doing the performClick to see if that would work, but it still keeps the focus. I also tried putting the focus code there. Is there anyway I can make it do the same action on the enter?

Android default button and DONE on keyboard

I have an activity with the following structure
- LinearLayout
- EditText (Username)
- EditText (Password)
- Button (Login)
I want that when the user is focused on the second EditText, instead of displaying NEXT in the keyboard, it displays "DONE" or "GO" and by pressing it, the click event of my button is raised.
Is this possible?
Yse, is possible, just use following code:
mUserID.setImeOptions(mUserID.getImeOptions()|EditorInfo.IME_ACTION_NEXT);
mUserPass.setImeOptions(mUserPass.getImeOptions()|EditorInfo.IME_ACTION_DONE);
mUserPass.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView pView, int pActionId, KeyEvent pEvent) {
if (pActionId == EditorInfo.IME_ACTION_DONE) {
// Do something
return true;
}
return false;
}
});

Softkeyboard Done button causing focus to skip

I have a set of EditTexts in my app that the user needs to fill in. Pressing "Next" on the soft keyboard advances the focus to the next EditText and at the last one I capture the soft key Enter action to validate and submit the entered data. In case there is a validation error (e.g. an unfilled field) the relevant field (i.e. the unfilled field) requests focus whilst also showing a toast to display the error.
It works fine (focuses on the correct item) if I use a Send button which calls the same validation function, but if I hit the Enter button on the soft keyboard, the focus jumps to the EditText that caused the validation error and immediately moves to the one immediately after it.
This is the snippet that sets the listener on the last EditText.
lastEditText.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) {
if(validateFields()){ //Validation function
sendPressed(); //Submit function
}
return true;
}
return false;
}
});
Any help would be appreciated..

android virtual keyboard listener

Though i have asked the question before but dint get any proper answer. I have an EditText when edit text is focused android virtual keyboard pops up, i have added Done button to the keyboard using ime option from property window. Now i want to perform some action by pressing the Done button. How to do this? Please any body help.
you an do it by firest finding reference of edittext and than set belows code for ur edittext:
editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_SEARCH) {
performSearch();//do here your stuff f
return true;
}
return false;
}
});
here i have taken editText name which is ref. editextbox which u added and you have to add actionId==type of ime options which u have setted

Categories

Resources