Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set edittext using only ",0123456789" programmatically in Android?

My question is this.

I can say in xml

android:digits="0123456789,"

But this time I've to it add trough java code.

customEditText.setKeyListener(DigitsKeyListener.getInstance("0123456789,"));

Doesn't work. Application gives me same result as android:InputType="number"

So is there any alternative way to say android:digits using java?

EDITED

So my question wasn't how to get android:InputType="number" using java.

My question is that how I can get android:digits="0123456789," to be used with java.

At this point I don't care if the user can see characters. I just want my edittext field to accept only numbers from 0 to 9 and the decimal comma (,).

Because of the customEdittext is really a customized edittext I can't use xml atm.

like image 339
joonasj Avatar asked Jul 17 '12 09:07

joonasj


People also ask

How do I set EditText digits programmatically?

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

How do you edit text only in numbers?

Using phone inputType EditText has an attribute called android:inputType . Providing android:inputType with value phone can display numbered keyboard when the EditText gets focus. Create an Android Project and replace layout (activity_main. xml) and Kotlin file (MainActivity.

Is there a way to define a min and max value for EditText in android?

Testing the min value is a different story as we cannot be sure if the user has finished typing or not and therefore cannot decide if we should block or not. Then in Activity set the InputFilterMax and the OnFocusChangeListenerMin to EditText note : You can 2 both min and max in onFocusChangeListener .

Which method is used to set the EditText?

In android, we can set the text of EditText control either while declaring it in Layout file or by using setText() method in Activity file.


2 Answers

try customEditText.setRawInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);

like image 130
Haresh Chaudhary Avatar answered Oct 17 '22 14:10

Haresh Chaudhary


EditText ed;
ed.setInputType(InputType.TYPE_CLASS_NUMBER);

also docs may help

like image 3
Andrew F Avatar answered Oct 17 '22 15:10

Andrew F