Reputation: 5135
how to make dynamically edittext field for accepting only double and float values?
Upvotes: 9
Views: 9771
Reputation: 1
et.setRawInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
where setRawInputType it corresponds to the android:inputType attribute.
Upvotes: 0
Reputation: 43359
Use this method in your Activity:
EditText et = (EditText) findViewById(R.id.text01);
et.setInputType(0x00002002);
or
EditText et = (EditText) findViewById(R.id.text01) ;
et.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
here text01
is id of EditText
field in your R.java
file;
Reference:
http://developer.android.com/reference/android/widget/TextView.html#attr_android:inputType
Upvotes: 16
Reputation: 72341
Maybe a nicer solution would be using
et.setInputType(InputType.TYPE_CLASS_NUMBER
| InputType.TYPE_NUMBER_FLAG_DECIMAL);
Edit: I have also added the flag InputType.TYPE_NUMBER_FLAG_DECIMAL
to accept dobules too.
Upvotes: 4
Reputation: 89
You can also use the setInputType(int type)
function:
EditText e
e.setInputType(InputType.Number) //Many other options are also available
Upvotes: 0