Android EditText accept 2 values only - android

I want an EditText to accept two characters only, a one(1) and a zero(0). I was reading about input filters and know how to accept numeric values only but what about specifying which characters to accept.
Can someone please give me an example as to how this is done?

You may need to use something like TextWatcher, inside TextWathcer onTextChanged, if entered value is not either 1,0 display message.

Related

Is it possible to set EditText only accept unicode characters in android?

Is it possible to make an android EditText accept only unicode characters? If so, how?
Currently, unicode only is not a restriction option for the EditText component, but you can dynamically check every symbol to see if it falls into that category or not, and if not remove it.

How to set EditText input type like "X123"?

I want to enter the text in the format of "X123". i.e first letter is alphatbetical and remaining should be numericals. So is there any input type which fullfill this requirement or I have to use textwatcher for edit text and then change input type of edit text at run time.Is it possible?
Thanks in advance.
There is no standard input type like this.
You can use TextWatcher if you want to force it upon your users while typing, but that might not really be nice neither. You'll only get the effect that they press 'wrong' input and nothing happens.
Perhaps its a much easier, and still nicer way to simply put in a hint which shows the expected input, and once they 'submit' the input you check it against a regex.
If it doesn't match the regex, you can show a Toast, shake the input field, surround the edittext with red lines or whatever you want to show what field has wrong input.
use this RegExp to match the string
string.matches("[a-zA-Z]\\d{3,}") // for compulasory one char and min three digit
string.matches("[a-zA-Z]\\d{2,}") // for compulasory one char and min two digit
string.matches("[a-zA-Z]\\d{1,}") // for compulasory one char and min one digit
string.matches("[a-zA-Z]\\d*") // for compulasory one char and zero or more digit
Hope it will help you.

Input Type Number, no negative sign or decimals allowed?

I have two edittext views that I using for a calculation and both need to be limited to numbers only so I'm using the InputType to do this, but now I cannot enter a negative number or any numbers containing decimals!
Any ideas or solutions?
Thanks!
You should use numberSigned or numberDecimal, or both numberDecimal|numberSigned to enable features you need.
Simply use the below and even the device input keyboard will contain numbers only:
myTextView.setInputType(
InputType.TYPE_CLASS_NUMBER);

EditText Input type Issue

There is a name EditText in my screen. I want to accept only alphabets means from a-z,A-Z
and should not accept numbers, special chars.
I'am using inputtype="textPersonName" for that but its notworking, it accepts means showing all numbers, chars etc..
For phone field inputtype="phone" is working it accepting means showing numbers in field` only.
Why like that?
Your best bet is to use an InputFilter to restrict the characters that are permitted in your EditText control. You should be able to easily adapt the answer from How do I use InputFilter to limit characters in an EditText in Android? to do what you require.
You will need to subclass EditText and detect key events.
Reject any character other than a-zA-Z
This is the only way to limit the acceptable characters.
A person's name could contain a number so textPersonName is working correctly.
Try using TYPE_TEXT_VARIATION_PHONETIC

EditText text formatting for forms

I'm looking for a simple way for Android, when a user taps a in a edit text box, it will add a hyphen inbetween the number set. Sort of like a phone number, but will be set in a custom position.
For example Book number 05, CD number 15, track number5 = 0515-5. So when they start typing, the app will automatically enter the hyphen.
Should I use some type of listener and count how many characters are entered, then when it hits that number of characters, it will add the hyphen?
Thanks!
Use an InputFilter.
See my answers here: press "." many times (validate ip address in EditText while typing) and here: How to set Edittext view allow only two numeric values and two decimal values like ##.## for ideas on how to implement it

Categories

Resources