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.
Related
How do I set the input type for registration number. For example my university has specific format for registration number i.e. CIIT/SP17-mcs-044/Atk. I want CIIT/ATK is written already and Sp17-mcs-044 as a hint.
You can't define a random types of input type. You can only have one of these types as inputType.
If you want any custom behaviour, you might use TextWatcher and validate it
As #Tim has stated, the input type would be text over here and when user types in that EditText then check if the input is correct by using RegEx, and then show any message when the input is correct, for that you will need to add an OnTextChangedListener, about which you can learn more on official documentation.
Now, CIIT/ATK can be a TextView placed right before the EditText so it will be written already and will be uneditable. And yes, add that in final string after getting the input, like this:
finalInput = "CIIT/ATK" + input;
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 want a user to be able to input numbers such as the following.
Valid:
~0
~0.00
~12.34
~301.7
~4
Invalid
~01
~3.001
In short, it allows decimal numbers up to two decimal places.
This is what I've been trying to use
Pattern mPattern = Pattern.compile("|(0|[1-9]+[0-9]*)(\\.[0-9]{1,2})?");
When I try to type a "." in the field, it won't let me.
I think the problem is that your validation pattern needs to match the input as you are entering it. In your case, as soon as you type in the ".", your entry is invalid. For example, if you are trying to enter 1.23, when you are entering the decimal point your entry becomes 1., which does not match your regexp.
Try replacing {1,2} with {0,1,2} in your expression to allow a trailing ".".
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();
}
}
I have set input type to be numberdecimal but also want to populate the editText with a "-" programmatically. I can add the text but then I am unable to edit the text as it doesn't confirm to the number decimal format. Any idea on how I can say inputtype is numberdecimal but a "-" can be allowed?
I was able to achieve this behavior by setting digits xml attribute as follows:
<EditText
...
android:inputType="number"
android:digits="0123456789-"/>
Setting it up programatically (Set EditText Digits Programmatically):
weightInput.setKeyListener(DigitsKeyListener.getInstance("0123456789-"));
I managed to do that with:
android:inputType="number|numberSigned"
android:digits="0123456789-"
I found a very easy solution:
editText.setKeyListener(new DigitsKeyListener(true, true));
The first true is for whether is signed input, the second true is for decimal.
Hope that helps
You will have to write your own KeyListener. You could start by downloading the source of the NumberKeyListener and take it from there.
Martin
I am having one solution, which may helps you:
Suppose, You want to enter 2-3 numbers with "-" sign for e.g. 203-304-405.23-232.45,
then Allow user to enter this in EditText without setting any attributes.
and then you can Separate each numbers with "split()" function , but be sure that there should be any separator sign in between the tokens.
then
String tokens[];
strInput = edittext1.getText.toString();
tokens = strInput.split(",");
then you can work with each tokens separately as tokens[0], tokens[1],
for example:
num1 = tokens[0];
num2 = tokens[1];
Hope this helps you.
Enjoy!!