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 !

35 comments:

  1. Thanks for that , i was looking for same thing

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

    ReplyDelete
  3. 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

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

    ReplyDelete
  5. This comment has been removed by the author.

    ReplyDelete
  6. 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!

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

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

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

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

    ReplyDelete
  10. MyEditText.setInputType(2);

    ReplyDelete
  11. Nice! Thanks man!

    ReplyDelete
  12. thank you this is what i've been looking for

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

    ReplyDelete
  14. but what do we do for minimum length??

    ReplyDelete
  15. 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.

    ReplyDelete
  16. 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.

    ReplyDelete
  17. 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

    ReplyDelete
  18. 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

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

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

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

    ReplyDelete
  22. 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

    ReplyDelete
  23. 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/

    ReplyDelete
  24. This was exactly what I was looking for. Thanks!

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

    ReplyDelete
  26. R.I.P. code formating
    R.I.P. code highlighting

    ReplyDelete