Check if insert text are numbers

Put your problem here if it does not fit any of the other categories.

Check if insert text are numbers

Postby mana » Sat May 10, 2008 10:03 pm

Hi,
I'm a newbie in android. And i have a question.
How can I check if the insert text (in a edittext) are numbers (Integer).
I have no idea.
Please help me.
Thanks in advance.
mana
Freshman
Freshman
 
Posts: 6
Joined: Sat May 10, 2008 9:56 pm

Top

Postby plusminus » Fri May 16, 2008 10:57 am

Hello mana,

you could disallow non-numeric input to the EditText like this:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.                 EditText e = new EditText(this);
  2.  
  3.                 e.setInputMethod(new DigitsInputMethod());
Parsed in 0.030 seconds, using GeSHi 1.0.8.4


(Such things can also be done via xml):
public void setInputMethod(InputMethod input)
Sets the input method to be used with this TextView. This can be null to disallow user input.
Be warned that if you want a TextView with an input method or movement method not to be focusable, or if you want a TextView without an input method or movement method to be focusable, you must call setFocusable(boolean) again after calling this to get the focusability back the way you want it.

Related XML Attributes
android:numeric
android:digits
android:phoneNumber
android:inputMethod
android:capitalize
android:autoText


If you do not want to disallow, just check if a String is numeric, do this:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.                 boolean isNumeric;
  2.  
  3.                 String s = "13235XXX";
  4.  
  5.                 try{
  6.  
  7.                         int i = Integer.parseInt(s);
  8.  
  9.                         isNumeric = true;
  10.  
  11.                 }catch(NumberFormatException nfe){
  12.  
  13.                         isNumeric = false;
  14.  
  15.                 }
Parsed in 0.031 seconds, using GeSHi 1.0.8.4

But this will only work for numbers that would fit into the Integer number-range.

This will work for any input:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.                 boolean isNumeric = true; // assume it is true
  2.  
  3.                 String s = "13235XXX";
  4.  
  5.                 for(char c : s.toCharArray())
  6.  
  7.                         if(Character.isDigit(c))
  8.  
  9.                                 isNumeric = false;
Parsed in 0.034 seconds, using GeSHi 1.0.8.4

(Yes I know performance could be improved)

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

How to disallow other characters and allow only digits

Postby bharath » Tue Nov 11, 2008 3:07 pm

Hi plusminus,
Could u please tell me the value i need to specify for android:digits attribute for EditText in xml layout .
I have an EditText where it should only digits.
setInputMethod is removed as per sdk 1.0

Regards
Bharat.
bharath
Junior Developer
Junior Developer
 
Posts: 10
Joined: Sat Sep 27, 2008 11:21 am

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 12 guests