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 !
Thanks for that , i was looking for same thing
ReplyDeleteGlad to hear I could help you !
ReplyDeleteYep - learn a lot, wasted bunch of time. Thanks for the tip!
ReplyDeletecheers, thanks,
ReplyDeletewas 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... =)
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteHey man! thanks for this hint... I work on properties and found that "numeric" is deprecated. It's better to use android:inputType="number" instead.
ReplyDeletebtw thanks for your help!
just when I was going to do a keystroke listener... Thanks!
ReplyDeleteIt looks like this has been deprecated. Use android:inputType="numberDecimal" instead
ReplyDelete@ralphkw :
ReplyDeleteYes, you are very right !
Thanks to bring it up !!
hum...
Actually No?Ya had already brough it up...
MyEditText.setInputType(2);
ReplyDeletehey tat was cooll....
ReplyDeleteThanks A lot for this!
ReplyDeleteNice! Thanks man!
ReplyDeleteThanks!
ReplyDeletethank you this is what i've been looking for
ReplyDeleteThanks man, I had the reverse problem knowing how to set an edittext field to numeric in xml but not in code.
ReplyDeletebut what do we do for minimum length??
ReplyDeleteThere 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.
ReplyDeleteFound this post useful, thanks!
ReplyDeleteYou're the man :)
ReplyDeleteYou 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.
ReplyDeleteOne think I needed it was to allow only numbers decimal and signed, what I did was in the xml of the EditText I added:
ReplyDeleteandroid: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:
ReplyDeletehttp://itdumka.com.ua/index.php?cmd=shownode&node=32
Thanks for useful info.
ReplyDeleteIs 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!
ReplyDeleteThanks! Funny lol I came about this article TWICE in search for two different issues :)
ReplyDeleteHi,
ReplyDeleteThanks 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 ,
ReplyDeleteI 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!
ReplyDeletecool! tnx
ReplyDeletetotally agree... thanks, you saved me another time waste... erm... learning experience...
ReplyDeleteCheers
use android:inputType now
ReplyDeleteR.I.P. code formating
ReplyDeleteR.I.P. code highlighting
Thank you! this was really useful!
ReplyDelete