Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to programmatically set EditText's InputType to integer or decimal?

How do I programmatically configure an EditText to allow:

  1. Positive or negative integer values

  2. Positive or negative decimal values

I am having a hard time finding what gets this working even with https://developer.android.com/reference/android/text/InputType.html

like image 929
user7085962 Avatar asked Nov 07 '16 04:11

user7085962


People also ask

How do I set input type programmatically?

This example demonstrates how to set the input type for an Android EditText programmatically using Kotlin. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.

How do I set EditText digits programmatically?

Just add a clarification: editText. setKeyListener(DigitsKeyListener. getInstance(true,true)); to enable decimals and negative numbers.

What is InputType In android?

The android:inputType attribute allows you to specify various behaviors for the input method. Most importantly, if your text field is intended for basic text input (such as for a text message), you should enable auto spelling correction with the "textAutoCorrect" value.

Which attribute of EditText tells the system keyboard what kind of input you are expecting *?

Setting the InputType of an EditText tells the system keyboard what kind of input you are expecting. If your edit text is for inputting a phone number, then you want the keyboard to show numbers. It would be annoying for the user to have to manually switch to a numeric keyboard.


2 Answers

You can use this code:

EditText edt = new EditText(context);
edt.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL); //for decimal numbers
edt.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_SIGNED); //for positive or negative values

If together:

edt.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL | InputType.TYPE_NUMBER_FLAG_SIGNED);
like image 183
Anuja Kothekar Avatar answered Oct 16 '22 02:10

Anuja Kothekar


If someone need, this is the answer in Kotlin:

et_text.inputType = InputType.TYPE_NUMBER_FLAG_SIGNED + InputType.TYPE_CLASS_NUMBER
like image 20
Barbara K Avatar answered Oct 16 '22 00:10

Barbara K