How to perform actionDone event of EditText with Espresso - android

Question in the title.
I tried something like:
onView(withId(R.id.search_edit_text))
.perform(typeText("some"), pressKey(KeyEvent.KEYCODE_ENTER));
But it doesn't work. Keyboard is still shown and TextView.OnEditorActionListener not called. Any ideas?

Try
onView(withId(R.id.search_edit_text))
.perform(typeText("some"), pressImeActionButton());

I used KEYCODE_BACK and it works well. KEYCODE_ENTER not working on my device SAMSUNG A70
onView(withId(R.id.edit_text).perform(ViewActions.pressKey(KeyEvent.KEYCODE_BACK))

Related

Keyevent doesn't work

I have a problem with keyevents when I try to press the home button.
Where is the bug?
driver.execute_script("mobile: keyevent", {"keycode": 3})
I use python and appium.
The solution to the problem were extra spaces.
The code should look like this:
driver.execute_script("mobile: keyevent",{ "keycode": 3 })

Espresso - typeText not working

I'm trying to type some text inside an EditText:
public void testSearch() {
onView(withId(R.id.titleInput)).perform(typeText("Engineer"));
onView(withId(R.id.titleInput)).check(matches(withText("Engineer")));
}
I see the EditText is getting focus, but nothing happens. No text is typed.
I tried this on a phone and an emulator - same result.
Looks like I figured out the issue. It had to do with hardware vs software keyboard.
For Emulators:
Go to Settings -> Language & Input -> switch the Default Input to Sample Soft Keyboard.
For Phones:
Install a software keyboard from the Play store and switch to it. It appears that the native keyboards of some phones do not work.
It works now.
Had the same issue using Espresso 2. As a workaround I'm using replaceText instead of typeText.
public void testSearch() {
onView(withId(R.id.titleInput)).perform(click(), replaceText("Engineer"));
onView(withId(R.id.titleInput)).check(matches(withText("Engineer")));
}
If the EditText does not has the focus yet, you should click on it first. If this solves your problem, then there is no bug.
onView(withId(R.id.titleInput)).perform(click()).perform(typeText("Engineer"));
You can bypass the problem by calling setText on the EditText.
final EditText titleInput = (EditText) activity.findViewById(R.id.titleInput);
getInstrumentation().runOnMainSync(new Runnable() {
public void run() {
titleInput.setText("Engineer");
}
});
Same issue resolved with the following:
editText.perform(scrollTo(), click(), clearText(), typeText(myInput), closeSoftKeyboard())
Interestingly, I only ever had a problem when my machine was working hard.
You can include it with in the code like this,
onView(withId(R.id.titleInput))
.perform(click(), replaceText("Engineer"), closeSoftKeyboard());
I fixed this issue by setting layout_height="wrap_content" on the View I wanted to click(). Maybe it can help someone here.
If you're using Genymotion, you may need to switch the default keyboard in Genymotion Configuration (it's an app on the emulator).
Go to Apps -> Genymotion Configuration -> Keyboard -> Virtual keyboard (click "Yes" when you're prompted to reboot)
NOTE: These changes do not persist after you close the emulator. You will need to set this every time you start the emulator.
for me I was annotated my test method with #UiThreadTest. I removed that and it solved.
Adding closeSoftKeyboard() after typeText() worked for me.
CODE:
onView(withId(R.id.editTextUserInput))
.perform(typeText(STRING_TO_BE_TYPED), closeSoftKeyboard());
This is how it is documented in the Android docs.

jquery onclick not responding in android tablet

My image-slider code is perfectly working pc ,ipad but on tablet onclick event is not working. when i used .on and print something with alert then onclick works. if i am replacing alert with console.log then it's not working .
i don't understand why is this happening ?
$('#myImageFlow').on("touchstart click",".sliderImage", function(event)
{
//alert('i am on method '+event.type);
console.log('on method ==='+event.type);
});
Simply: console.log is browser-related and you have no way to know if it will work or not (console is browser-dependant). Use other method to log (alert is ok in debug time imho)
try to use tap event of jquery instead of click.

how to use Enter on softkeyboard for my app?

I have big problem with my keyevent i am trying to call my another acvitity but the keyevent is not working please check what mistake i made for my code. actually i created one carousel design if i press softkeyboard enter button in my laptop the keyevent is not working...
This case is not working for me...
case KeyEvent.KEYCODE_ENTER:
System.out.println("enter pressed");
if(position==0);{
Intent intent = new Intent(sampleActivity.this, test.class);
startActivity(intent);
}
I suppose you need to enable your keyboard input. Even though the developer documentation says keyboard support is enabled by default it doesn't seem to be that way in SDK rev 20. I explicitly enabled keyboard support in my emulator's config.ini file and that worked!
Add: hw.keyboard=yes
To: ~/.android/avd/.avd/config.ini
Reference: http://developer.android.com/tools/devices/managing-avds-cmdline.html#hardwareopts
Your code seems correct (apart from the synthax error), the only difference I see from working examples is your event.getAction() condition. I'd suggest to check the ACTION_DOWN clause.

onKeyListener not working with soft keyboard (Android)

I am using onKeyListener to get the onKey events. It works fine with the normal keyboard. But it does not work with soft keyboard. I am only able to get onKey events for numerics and not alphabets. Is there any workaround to solve this? Any kind of help will be greatly appreciated.
I don't believe an OnKeyListener gets called at all with the software keyboard. It has something to do with the software keyboard being an IME device and IME devices possibly being things other than keyboards. It seems to make onKeyListener pretty much useless though, since it only works on phones with hardware keyboards. I worked around this issue recently by using TextWatcher on the EditText field in my Activity instead of using OnKeyListener.
onKeyListener worked perfectly on Android 1.5 via the soft keyboard
From Android 1.6 onwards the character and number keys are not going via the onKey event, yet the DEL key does
Frustrating
This is probably stupid, but that's how Android works at the moment.
The documentation states that the key events will only be propagated for the hardware key strokes, not software.
The device manufacturers are actually being discouraged to propagate soft keyboard events through key listeners, although it is completely up to the manufacturer to honour that or to actually treat the soft and hard keyboards with equal terms.
Starting from Android 4.2.2, Android system itself will not support key stoke events for the soft keyboards at all, so even the manufacturers will not be able to choose their way.
So the only foolproof option here is to implement your own IME (soft keyboard), and handle the keystrokes yourself.
TextWatcher can be used mostly to replace the key listeners, however editText.setText(...); will also trigger the TextWatcher events, so if one is interested in typed keys only then probably TextWatcher is not a solution either.
Please be cautious when using TextWatcher with AutocomleteTextView or EditText. Do not modify text in the AutocompleteTextView / EditText's content from within TextWatcher events, cause otherwise you'll most probably end up in an infinite event/listening loop.
Hope this helps to clarify the available options, but sadly it does not provide a working solution.
Disappointing that Google has missed on this important aspect of their UI.
This seems to be device specific. I can confirm that this works on the Xoom and the Acer A100. However, the Samsung Galaxy Tab Plus only fires the event for the non-character buttons. (All devices running Honeycomb)
I got around this by putting the listener into it's own method and calling it again after the first time. In the onCreate I call setKeyListenerForEnter();
Then, here's the method:
public void setKeyListenerForEnter(){
final EditText search_entry = (EditText) findViewById(R.id.search_entry);
search_entry.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
// If the event is a key-down event on the "enter" button
if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
(keyCode == KeyEvent.KEYCODE_ENTER)) {
getSearchResults(v);
setKeyListenerForEnter();
return true;
}
return false;
}
});
}
I'm not sure if this is a better solution than handling the IME keyboard itself, but it is a solution.
setFocusableInTouchMode(true); //Enable soft keyboard on touch for target view
setFocusable(true); //Enable hard keyboard to target view
example:
public class CanvasView extends View{
public CanvasView(Context c){
super(c);
//enable keyboard
setOnKeyListener(new KeyBoard());
setFocusable(true);
setFocusableInTouchMode(true);
}
}

Categories

Resources