Android: need to validate an edittext for non-blank input - android

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.

Related

Difference between InputType.TYPE_TEXT_VARIATION_WEB_PASSWORD and TYPE_TEXT_VARIATION_PASSWORD

How do you know whether to use
android.text.InputType.TYPE_TEXT_VARIATION_WEB_PASSWORD or android.text.InputType.TYPE_TEXT_VARIATION_PASSWORD?
The documentation of TYPE_TEXT_VARIATION_WEB_PASSWORD:
Variation of TYPE_CLASS_TEXT: entering password inside of a web form. This was added in HONEYCOMB. An IME must target this API version or later to see this input type; if it doesn't, a request for this type will be seen as TYPE_TEXT_VARIATION_PASSWORD when passed through EditorInfo.makeCompatible(int).
The documentation of TYPE_TEXT_VARIATION_PASSWORD:
Variation of TYPE_CLASS_TEXT: entering a password.
In which case is a form considered a "web form"?
Is there any difference in behavior between the two? (visually and/or logically)
In which case is a form considered a "web form"?
If EditText( html input field) is inside browser page( any web form which takes input from user).
We need to use InputType TYPE_TEXT_VARIATION_WEB_PASSWORD in case of web-from.
if EditText is object of EditText in native application then need to use TYPE_TEXT_VARIATION_PASSWORD
Is there any difference in behaviour between the two? (visually and/or
logically)
YES,
See com.android.inputmethod.latin.InputAttributes here:
TYPE_TEXT_VARIATION_WEB_EDIT_TEXT flag is used :
// If it's a browser edit field and auto correct is not ON explicitly, then
// disable auto correction, but keep suggestions on.
// If NO_SUGGESTIONS is set, don't do prediction.
// If it's not multiline and the autoCorrect flag is not set, then don't correct
But in case of TYPE_TEXT_VARIATION_PASSWORD InputType above comments are not TRUE.

Stuck on MIT APP Inventor:

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}

Problems intercepting space key in EditText

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."

Dialog.Confirm that returns boolean in Android

I need a Confirm dialog that returns the boolean value in order to remind the user about the missing series of values in the form that are required based on the settings but not mandatory.
I did lot of research but none of the tell me how to active this.
For example: In a form lets say the user forgot to enter City, State and Zip. I need to throw an reminder asking the user "Did you intentionally not enter the City?" with "Yes" and "No" buttons. If the user answers "Yes" then throw the reminder for State else set the focus to City, so that user can enter the city. And so on....
Any help would be greatly appreciated.
Thanks,
Vincy
First, what you are proposing is not a particularly friendly UX. Use color coding or something to indicate fields that you think should be filled in but are not, rather than forcing the user to have to keep tapping on dialogs to do what the user wants to do.
All that being said, use AlertDialog (perhaps via AlertDialog.Builder) and set up DialogInterface.OnClickListener objects for the buttons. Depending upon the button choice, you either set the focus (via requestFocus(), called on the widget needing focus) or continue with your processing.

How to custom Edittext listener

How can I use the edittext listener to prevent the user from entering special characters (&%+?#...etc) and also prevent the user from entering numbers, finally allow user to enter only letters (A B C ...)
Check out EditText.setFilters(). This allows you to specify one or more instances of InputFilter for an EditText. These filter certain characters from the input, as the name suggest. There are a lot of prebuild ones (see "know subclasses" at the top of the class documentation), but you may also implement your own by extending the InputFilter class.
You can use add TextWatcher (link: http://developer.android.com/reference/android/text/TextWatcher.html) to your EditText (link: http://developer.android.com/reference/android/widget/TextView.html#addTextChangedListener%28android.text.TextWatcher%29) and implement the three provided methods in order to control the input from the user and perform any operation you want.

Categories

Resources