How to validate edittext with integers in android? - android

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.

Related

How to check if user doesn't write numbers or for example punctuation marks in edittext when he had to write his name

I have layout when user have to write name and surname. How can I check if he truly wrote name an surname and doesn't write numbers or other marks instead of it. Please, help!
You can specify inputType of your EditText.
https://developer.android.com/reference/android/widget/TextView#attr_android:inputType
Also you can add TextWatcher to your EditText and check user input
https://developer.android.com/reference/android/text/TextWatcher
Try this
It wont allow any special character or numeric value in EditText
android:digits="abcdefghijklmnopqrstuvwxyz"

Limit the input in an edit text

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
}

EditText default value and variable name

Is there a way to have an edittext show the hint but to have a 0 value and if you click to input your value,the 0 disappears ? I'd like this as a solution to having empty editTexts that make my app crash.
How can i name my EditText with a variable ? For example : EditText cant1=(EditText) findViewById(1);
Instead of cant1 to have something like EditText cant+i=(EditText) findViewById(i);i++; I want to loop and register the EditTexts automatically but i need it to change the names.I mean,to name it cant1 then cant2 then cant3 and so on.
Thanks in advance !
In your XML add android:hint="Username" and what ever is in the brackets will show up in the edit text but won't equal anything.
And in your java you can do a null check or surround it in a try/catch to prevent app crashing:
EditText et = (EditText) findViewById(R.id.yourEditText);
if(et != null) {
//Do stuff
}else{
//Toast an error or a warning
}
or
try{
String value = et.getText();
}catch(NullPointerException e){
//Toast an error or a warning
}
Then,add 0 as hint.But,if you want both hint text and '0' at a time i think it is not possible. Try edittext.setText("0");in coding place and check it out...i am not sure it may work.

How To capitalize the letters in AutoCompleteTextView dynamically?

I am developing the application which consists of AutoCompleteTextView,here is my problem,How I can upper case the letters entering in AutoCompleteTextView.
I Don't want in xml: android:capitalize="characters"
I want to declare in Java code.
You can try like this..In your text watcher in ontextchanged change the text to upper case..and check if the new string in edittext is the old string which you converted to upper case...in order to avoid stackoverflow error..
String upper = mytextview.getText().toString().toUpperCase()
mytextview.setText(upper);
Try this in your code there are some other flags also which you can check and try.
tv.setInputType(InputType.TYPE_TEXT_FLAG_CAP_CHARACTERS);

Display Validation Message

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();
}
}

Categories

Resources