Friday, January 2, 2009

numeric editText and EditText with max number of characters... yes, again !

Sometimes, being a lone developer is a very depressing status.
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);


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


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 !

36 comments:

The real Z said...

Thanks for that , i was looking for same thing

AndroidBlogger said...

Glad to hear I could help you !

bo said...

Yep - learn a lot, wasted bunch of time. Thanks for the tip!

verdebreuk said...

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

highvista said...

Thanks much! Exactly what I was looking for. I almost embarked down the long road as well... =)

No?Ya said...
This comment has been removed by the author.
No?Ya said...

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!

Andy said...

just when I was going to do a keystroke listener... Thanks!

ralphkw said...

It looks like this has been deprecated. Use android:inputType="numberDecimal" instead

AndroidBlogger said...

@ralphkw :
Yes, you are very right !
Thanks to bring it up !!

hum...
Actually No?Ya had already brough it up...

Anonymous said...

MyEditText.setInputType(2);

mw88 said...

Thank you

vinith said...

hey tat was cooll....

Jackie said...

Thanks A lot for this!

Anonymous said...

Nice! Thanks man!

RGB tool said...

Thanks!

Anonymous said...

thank you this is what i've been looking for

Anonymous said...

Thanks man, I had the reverse problem knowing how to set an edittext field to numeric in xml but not in code.

Anonymous said...

but what do we do for minimum length??

Wisl said...

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.

David Underwood said...

Found this post useful, thanks!

טל וינטר said...

You're the man :)

jokerx said...

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.

Wagner Pinto said...

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

Anonymous said...

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

Anonymous said...

Thanks for useful info.
Is these a trick to preventing user from inputting 0.

Stra said...

But you do have google and loads of other lone android developers to help out. Was looking to do the same thing thanks man!

Ready4Fajir said...

Thanks! Funny lol I came about this article TWICE in search for two different issues :)

MAD'u CrY said...

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

MAD'u CrY said...

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/

Anonymous said...

This was exactly what I was looking for. Thanks!

The XCoder said...

cool! tnx

Anonymous said...

totally agree... thanks, you saved me another time waste... erm... learning experience...
Cheers

Nayana Hettiarachchi said...

use android:inputType now

dit j. said...

R.I.P. code formating
R.I.P. code highlighting

Unknown said...

Thank you! this was really useful!