I'm attempting to make a way for a user to input text into a TextView from an EditText. However, if the user enters something, and wants to fix it, I want them to be able to press space on an empty EditText to get the last thing they wrote back. The first problem is, if they type in "hello", hit enter to add it to the TextView (which clears it from the EditText), then hit space, the EditText then has " hello". Not what I want, and I can't figure out why.
My code to place the entered text into a holding string:
b1 = ti.getText().toString();
Then, if the user hits the space key, I believe they should get b1 in the EditText. Instead, I get: " " + b1. Why is this added space in there?
if((event.getAction()==KeyEvent.ACTION_DOWN)&&(key == KeyEvent.KEYCODE_SPACE)){
if(ti.getText().toString().equals("")){
ti.setText(b1);
}
My second, bigger problem is that the above code only works on a hardware keyboard. What is the key event for a software keyboard pressing space?
This is all in an onKeyListener.
An easy way to solve the extra space problem is to just remove the space from the String before you put it into the EditText
b1 = b1.substring(1); //<--- cut out the first character, which is the " " in your case.
ti.setText(b1);
p.s. I strongly recommend more descriptive variable names. Your programs are likely to be confusing to work on if you use names like ti and b1. Perhaps these choices make more sense in the context of your program. But from what you've shown here it is not easy to tell what these names refer to.
For your first problem I suspect you need to return true from your onKey method in the onKeyListener when your 'if' condition is met, to indicate that the event is consumed, otherwise you get the default onKeyListener adding the space to the EditText. i.e.:
if((event.getAction()==KeyEvent.ACTION_DOWN)&&(key == KeyEvent.KEYCODE_SPACE)){
if(ti.getText().toString().equals("")){
ti.setText(b1);
return true;
}
}
This extract from the KeyEvent API docs should help with problem 2:
"As soft input methods can use multiple and inventive ways of inputting text, there is no guarantee that any key press on a soft keyboard will generate a key event: this is left to the IME's discretion, and in fact sending such events is discouraged. You should never rely on receiving KeyEvents for any key on a soft input method. In particular, the default software keyboard will never send any key event to any application targetting Jelly Bean or later, and will only send events for some presses of the delete and return keys to applications targetting Ice Cream Sandwich or earlier. Be aware that other software input methods may never send key events regardless of the version. Consider using editor actions like IME_ACTION_DONE if you need specific interaction with the software keyboard, as it gives more visibility to the user as to how your application will react to key presses."
Related
I am creating a custom keyboard for a Xamarin Android Application. I have it working, and my listener receives the input. My issue is the parameter Android.Views.Keycode. This enumeration does not have all of the possible keys. I have found some of them generate the correct keystroke if the KeyEvent is created with MetaKeyStates.ShiftOn. But even in that I have not found the right combination for the {} keys. There also doesn't appear to be a clear answer to which key or key/MetaState combination maps to backspace, Next, Done, etc.
I have not found documentation that shows which Keyode in conjunction with the required MetaState will generate which key strokes. Does such documentation exists? Does anyone have an exhaustive example showing which Keycode and which metastates create which characters?
Also, in the case of my keyboard, the ! and ? characters will appear on the screen with the normal text. Do I need to use a custom Keycode for them so I can tell the difference between them and the character that would have the same Keycode without a metastate?
Why are you using keycodes? Those are for hardware buttons. Software keyboards usually use InputConnection.commitText and skip keycodes entirely.
Next, Done, etc are the action button. That's another call on InputConnection- performEditorAction.
Delete is generally done by InputConenction.deleteSurroundingText.
Shifts are generally an internal state and not connected to any keycode.
You're doing everything the wrong way, basically. Here's the android implementation, I assume xamarin has its wrappers. https://developer.android.com/reference/android/view/inputmethod/InputConnection
I am writing an accessibility service. I've been trying to focus EditText fields in a way that pops up the software keyboard and I have been unsuccessful. I am able to get the cursor into the EditText field, but the soft keyboard doesn't pop up. Given EditTextNode is an AccessibilityNodeInfo that I have gotten from various accessibility events and that said nodeInfo isEditable, isFocusable, isVisibleToUser and isClickable when I attempt all of these actions and they all return true upon completion.
editTextNode.performAction(AccessibilityNodeInfo.ACTION_CLICK);
In my mind the above should simply work, and the fact that it does not is a bug in the Accessibility API, if not for my entire android version, at the very least on my device (Droid Ultra, Android 4.4.4). I have also tried:
editTextNode.performAction(AccessibilityNodeInfo.ACTION_FOCUS);
Puts focus into the field so I can see the input cursor, but no keyboard.
editTextNode.performAction(AccessibilityNodeInfo.ACTION_ACCESSIBILITY_FOCUS);
Doesn't really do anything unless talkback is enabled.
editTextNode.performAction(AccessibilityNodeInfo.ACTION_SET_SELECTION, someBundle);
Along with appropriate arguments this will work, but only if there is already text in the editText field. I need to pull the keyboard up on empty text fields as well. Yes, I tried inputing 0,0 for start and end text selection arguments. The SET_SELECTION method only works with text in the field!
This has been frustrating me for a couple days, hopefully you guys can help. Though I believe I've stumbled onto a bug and am going to have to wait for Android to update. Because the ACTION_CLICK method should really be all that is needed. But, I could be missing something silly, Accessibility API Doc is somewhat scant. Am willing to attempt anything.
To restate #alanv's comment in answer form:
You are correct, it is a bug in accessibility services prior to API 21.
Once I have completed the form, I cannot see the the form's output in the ResultsLabel I created. Attached is a screenshot below, does it look OK?
All help appreciated!
Regarding your statement "I cannot see the form's output in the label", is it possible that it's hidden behind the keyboard? Press the 'Done' button to hide the keyboard when you're done typing.
Regarding your question "does it look OK":
Your first StoreValue event is saving the value as the tag, which probably isn't great practice but can work
However, your second StoreValue overwrites the first one, and the third StoreValue overwrites the second one because you're using the same tag for all three.
If you want to save the two thumbpositions for that particular name you should do this:
TinyDB.StoreValue
tag = {name.text}
value = {make a list
{cashrequired.thumbposition}
{period.thumbposition}
I have a custom view with onKeyDown() overridden. It works fine while I use standard Android english (or italian) soft keyboard, but when I set the device to Russian language key events never reach onKeyDown() or onKeyUp() (they're never called, I tried with a breakpoint).
Moreover, if all views discarded the event, I should be able to catch it from the activity's methods, but it seems I can't even there. Note that the view is focused, in fact I can catch directional key presses. Also the delete/backspace russian soft key is catched.
How am I supposed to catch russian character key events?
From the KeyEvent docs:
As soft input methods can use multiple and inventive ways of inputting text, there is no guarantee that any key press on a soft keyboard will generate a key event: this is left to the IME's discretion, and in fact sending such events is discouraged. You should never rely on receiving KeyEvents for any key on a soft input method.
To the extent you are getting KeyEvents, it's against the recommendation of Google and is not supported in all versions of android going forward. That approach is certain to break on more devices as time goes on. So how do you capture characters?
One option is to use a TextWatcher.
This seems to be an issue that other Android users have had as well, and the solution that I found is to override dispatchKeyEvent instead.
#Override
public boolean dispatchKeyEvent(KeyEvent event) {
Toast.makeText(getApplicationContext(), "Key: " + event.getKeyCode(),
Toast.LENGTH_LONG).show();
return super.dispatchKeyEvent(event);
}
And using the event.getAction() call can tell you if it's onKeyDown, onKeyUp, etc. For more information on KeyEvents check out this Android developers' page and for more information about Android keyboards not responding to certain languages check out this bug report.
If I want to enforce a maximum length of input in an EditText field, I can use the maxLength attribute. But if I want to enforce a minimum length (in my case, simply non-blank), I find no corresponding minLength attribute.
I've also looked at the various 'inputType' attributes (phone, password, etc) but I don't see anything like 'required' or 'non-blank'.
I can't believe this would require a custom input filter; it's more likely the answer is so obvious it just doesn't get written down in the form I'm asking the question.
You can simply check that yourField.text() is not equivalent to null or "" at the point of submission. If it is, prompt the user to input something.
Carefull with something though. If you have a submit button, for instance, and your user presses submit without changing the focus out of your TextEdit, your method won't be called.
Take an Activity with 2 EditTexts and 1 button named "Go":
1) User writes something in EditText1
2) User clicks on EditText2
3) Instead of writting something there just clicks go.
Your onFocusChanged() method won't be called because focus is not "lost".
If you have a validation in your Go button_click method, it will trigger, of course, but you won't achieve your goal "I was looking/hoping for was a way to give instant feedback"
You can try using TextWatcher.