You want a feature, you fight with the system, the tools, the documentations ( no comment ), the forums, ...
And you found a solution. So you're happy with all your code that do what you wanted.
Until you finally find you could achieve the same thing with only one line of code !
This is exactly what happened to me !
I found that a numeric editText ( or a textView) is done with this XML tag :
android:numeric="decimal"
Then I found that limiting the number of character is also very simple :
android:maxLength="10"
and that's all !
Why didn't I find that in the first time ( and spend so many time on my own solution ? ).
Because, I was looking at a way to do this by code.
And it is less obvious in code.
For the limited number of character, you can add a Android API filter :
InputFilter[] FilterArray = new InputFilter[1];
FilterArray[0] = new InputFilter.LengthFilter(8);
editEntryView.setFilters(FilterArray);
FilterArray[0] = new InputFilter.LengthFilter(8);
editEntryView.setFilters(FilterArray);
For the numeric editText, there is also a obscur solution :
DigitsKeyListener MyDigitKeyListener =
new DigitsKeyListener(true, true); // first true : is signed, second one : is decimal
editEntryView.setKeyListener( MyDigitKeyListener );
new DigitsKeyListener(true, true); // first true : is signed, second one : is decimal
editEntryView.setKeyListener( MyDigitKeyListener );
So all my work was useless ?
Possibly ( the politically correct answer is : oh no, I learned so many things in the process ), there is still a feature I have with my solution that is not 'that simple' with those solution, but I think I can do it also with implementing my own filter.
Now should I do it ? Filters look like they are no user friendly beasts, and my solution is already up and working !
35 comments:
Thanks for that , i was looking for same thing
Glad to hear I could help you !
Yep - learn a lot, wasted bunch of time. Thanks for the tip!
cheers, thanks,
was half way through my own NumericTextEdit class when I saw this. Sadly now I'll have to wait for another opportunity to delve deeply into input filters
Thanks much! Exactly what I was looking for. I almost embarked down the long road as well... =)
Hey man! thanks for this hint... I work on properties and found that "numeric" is deprecated. It's better to use android:inputType="number" instead.
btw thanks for your help!
just when I was going to do a keystroke listener... Thanks!
It looks like this has been deprecated. Use android:inputType="numberDecimal" instead
@ralphkw :
Yes, you are very right !
Thanks to bring it up !!
hum...
Actually No?Ya had already brough it up...
MyEditText.setInputType(2);
hey tat was cooll....
Thanks A lot for this!
Nice! Thanks man!
Thanks!
thank you this is what i've been looking for
Thanks man, I had the reverse problem knowing how to set an edittext field to numeric in xml but not in code.
but what do we do for minimum length??
There is a heavyweighten difference between coding your validation and using inputType="numeric(Decimal/Signed)". inputType isn't usable outside US because the accepted chars are hardcoded to char[]{"0", "1", ..., "9", "-", "."} instead of using a locale. This problem is in all SDK versions. My actual workaround is the Listener "setKeyListener(DigitsKeyListener.getInstance(digits))" but thats no beauty solution.
Found this post useful, thanks!
You're the man :)
You are right, and we are spending so many time searching for solutions because the classic documentation format is very low in examples. And is not enough that the doc is low in examples but some sections are collapsed in a way that this search for info is HARDER, wtf.
One think I needed it was to allow only numbers decimal and signed, what I did was in the xml of the EditText I added:
android:inputType="numberSigned|numberDecimal"
and it worked perfectly
also what I did was add a android:hint to show the users how they must fill that field
hope it helps someone
Input Filter is very flexible. You can find Price Input Filter in my example there:
http://itdumka.com.ua/index.php?cmd=shownode&node=32
Thanks for useful info.
Is these a trick to preventing user from inputting 0.
But you do have google and loads of other lone android developers to help out. Was looking to do the same thing thanks man!
Thanks! Funny lol I came about this article TWICE in search for two different issues :)
Hi,
Thanks for useful post..
I have requirement like " The user should enter only numbers that to <=20[not more than that]. So, How can I restrict the user to enter the number within our limit(<=20).
Please suggest any ways to do..
Thanks
Hi ,
I found the solution for my problem.Just check
this link
http://tech.chitgoks.com/2011/06/27/android-set-min-max-value-an-edittext-accepts/
This was exactly what I was looking for. Thanks!
cool! tnx
totally agree... thanks, you saved me another time waste... erm... learning experience...
Cheers
use android:inputType now
R.I.P. code formating
R.I.P. code highlighting
Thank you! this was really useful!
Post a Comment