Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to limit EditText to input only float values in Java?

I have found answers to limit it to Integer/float in xml, I want to do it programmatically. I can set setInputType(InputType.TYPE_CLASS_NUMBER) for integer values, but what for float values?

I want them in xxxx.xxx format.

like image 635
Ayush Goyal Avatar asked Aug 03 '12 05:08

Ayush Goyal


1 Answers

I think you are looking for

android:inputType="numberDecimal"

in the xml file and

InputType.TYPE_NUMBER_FLAG_DECIMAL

in the java source file.

TYPE_NUMBER_FLAG_DECIMAL is a flag of TYPE_CLASS_NUMBER and allows decimal inputs.

For more information see this

In another way you can test the input value using

if(editText.getText().toString().contains("."))
     //Its a float value
else
     //Not a float value
like image 182
Chandra Sekhar Avatar answered Oct 13 '22 01:10

Chandra Sekhar