I have an XML layout file that contains an EditText and a Button. I would like to display the following validation message to provide feedback to the user:
You must enter 4 numbers
What's the best way to go about accomplishing this?
From my experience, this is the best way:
EditText yourEditText;
// when you detect an error:
yourEditText.setError("Input must be 4 digits and numeric");
The result is:
Also, if the input must be numeric, use android:inputType="numberSigned" in the EditText definition. That way the device won't allow the user to put non-numeric values; even better, it will show a special keyboard to do so:
On the EditText definition in the xml use android:numeric to bring up the numeric IME and use android:maxLength = "4" to limit the input to 4 digits. Use android:onClick on the button to trigger a click handler.
public void onClick(View v) {
if(mEditText.length() != 4) { // check if 4 digits
Toast.makeText(this, "Input must be 4 digits and numeric", Toast.LENGTH_SHORT).show();
}
}
Related
I want to write a input filter
such that if i say the edit text should not have a number higher than 40
you can input 1-40 , but anything else will not work
I do not want to do that with a text watcher, but an InputFilter
how can I do that ?
There's a property of EditText (in xml) called:
android:inputType="number"
You might be having a button click or something during which you would validate the EditText, there you can do something like this:
int num = Integer.parseInt(editText.getText().toString());
if(num<1 || num>40)
{
// display a message that you have to write something to EditText first
}
Folks,
I need to capitalize first letter of every sentence. I followed the solution posted here
First letter capitalization for EditText
It works if I use the keyboard. However, if I use setText() to programatically add text to my EditText, first letter of sentences are not capitalized.
What am I missing? Is there a easy way to fix or do I need to write code to capitalize first letters in my string before setting it to EditText.
The only thing the inputType flag does is suggest to the input method (e.g. keyboard) what the user is attempting to enter. It has nothing to do with the internals of text editing in the EditText view itself, and input methods are not required to support this flag.
If you need to enforce sentence case, you'll need to write a method which does this for you, and run your text through this method before applying it.
You can use substring to make this
private String capSentences( final String text ) {
return text.substring( 0, 1 ).toUpperCase() + text.substring( 1 ).toLowerCase();
}
Setting inputType doesn't affect anything put into the field programmatically. Thankfully, programmatically capitalizing the first letter is pretty easy anyway.
public static String capFirstLetter(String input) {
return input.substring(0,1).toUpperCase() + input.substring(1,input.length());
}
I need to let user choose between two variants when he inputs decimal number:
use comma(,) as separator
use dot(.) as separator
By default if I use inputType="numberDecimal" in the EditText xml config - EditText shows only digits and comma (,) as possible separator.
I've tried to use android:digits="0123456789, in my EditText config, but without result - EditText widget shows just digits and comma.
I want to have both variants (. and ,) available for user on on-screen keyboard when he tries to input decimal number.
Could you please advise?
Specifying both android:inputType="numberDecimal" and android:digits="0123456789,." will display a keyboard with mostly just numbers and punctuation (depending on the user's chosen keyboard). It will also limit the characters accepted as input to those in your digits attribute.
If you'd like to further limit the keyboard to display only certain characters, you'll need to define a custom one. Here's one of the better tutorials on doing that.
Use proper validation. Let the user see full keyboard but he remain aloof of using it. Means user should not be able to use or input anything using keyboard.
etlocation = (EditText) findViewById(R.id.etlocation);
and used this
etlocation.getText().toString();
if (!isValidLocation(etlocation.getText().toString().trim()))
{
etlocation.setError("Invalid location");
}
validate this
public static boolean isValidLocation(String str) {
boolean isValid = false;
String expression = "^[0-9,.]*$";
CharSequence inputStr = str;
Pattern pattern = Pattern.compile(expression);
Matcher matcher = pattern.matcher(inputStr);
if (matcher.matches()) {
isValid = true;
}
return isValid;
}
You can read this link and also read allow only number and period(.) in edit text in android. Hopefully are helpful from those links. Best of Luck!
You can create a custom keyboard.
The below link shows a nice example of custom keyboard
http://www.mediafire.com/download/39q7of884goa818/myKeyborad2.zip
and check this link also
How to develop a soft keyboard for Android?
I am doing some math on the values of two EditText fields, I want to have the following validations on them:
They are not empty.
They are valid to each other(if the first field was integer the second should be the same, if the first was decimal the second should be the same).
I cannot figure out how to validate the decimal values or specifically the decimal point.
I have tried this out, but it didn't work. My app just crashes.
if (editText1.getText().toString().equals(".") || editText2.getText().toString().equals("."))
return;
For decimal value you can use this:
EditText et = (EditText) findViewById(R.id.myTextView) ;
et.setInputType(0x00002002);
It will accept only decimal values.
Another solution is here.
You can use contains() method to check if your string contains the argument you provided
if (textView1.getText().toString().contains(".") || textView2.getText().toString().contains(".")) {
Toast.makeText(getApplicationContext(), "Wrong Values", Toast.LENGTH_LONG).show();
First of all, you want to set your input type to accept decimals and/or numbers.
You can set it up in the xml layout:
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="numberDecimal|number"
/>
Then you can just convert your EditText text to double:
double result = new Double(editText1.getText().toString());
or
double result = Double.valueOf(editText1.getText().toString());
This rocket science that you wroted is not needed anymore after that.
You can test whether something is a valid value or not by trying to parse it with Float.parseFloat and seeing if it throws a NumberFormatException (via try/catch). If it does, then you know the input is invalid.
Btw, it's nice if you provide a visual cue to the user to inform them that the input is invalid. As opposed to waiting until the user presses the "calculate" button and only tell them then.
I want to have an edittext that only accepts numbers as input and when a button is clicked I want to check that the edittext has got a number inside and is not empty. Thanks.
try this in layout.xml
<EditText android:numeric="integer" ..../>
in Code
EditText mNumber = (EditText)findViewById(R.id....);
onButtonClick
if(mNumber.getText().toString().length()>0)
//logic
else
//empty Editext
Call EditText.setInputType(EditorInfo.TYPE_CLASS_NUMBER);
This will make sure that numeric virtual keypad will appear, and special filtering will be applied to only allow only numbers to be entered.
To check empty text, call
EditText.getText().length() != 0
Instead of validating you can add a (inpuType = "number") attribute in your xml file (Under the editText) after which you'll be able to add only numbers in your editText.