Aswan
Aswan

Reputation: 5135

how to make edittext field for decimals

how to make dynamically edittext field for accepting only double and float values?

Upvotes: 9

Views: 9771

Answers (4)

Jose Manuel Vega
Jose Manuel Vega

Reputation: 1

et.setRawInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);

where setRawInputType it corresponds to the android:inputType attribute.

Upvotes: 0

Vikas Patidar
Vikas Patidar

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

Ovidiu Latcu
Ovidiu Latcu

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

john
john

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

Related Questions