Home » Android » android – How insert number (double) in EditText

android – How insert number (double) in EditText

Posted by: admin June 15, 2020 Leave a comment

Questions:

I use this code

 <EditText
     android:id="@+id/price_for_liter_value_edit_text"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"
     android:background="@android:color/transparent"
     android:textColor="@color/dn_14_date"
     android:fontFamily="sans-serif-regular"
     android:textSize="18sp"
     android:text=""
     android:padding="4dp"
     android:inputType="number"/>

and at this line I set inputType:

  android:inputType="number"

but I cant enter double number (with . – like as 1.3 or 20.5).
What type should I use in inputFormat ?

How to&Answers:

You can use

  android:inputType="numberDecimal"

Answer:

You can see this reference for all variations of allowed inputType. In your case

android:inputType="numberDecimal"

or

int type = InputType.TYPE_CLASS_NUMBER |  InputType.TYPE_NUMBER_FLAG_DECIMAL;

((EditText)findViewById(R.id.price_for_liter_value_edit_text)).setInputType(type);

Answer:

For Edittext, there is an attribute digits :-

 android:digits="0123456789." 

If this param is set, specifies that this TextView has a numeric input method
and that these specific characters are the ones that it will
accept. [string] – Text snippet from developer.android.com

This will allow only numbers and “.” to be added in the textview