Reputation: 461
When creating an EditText in the java portion of the application, how do you limit it to numbers like you would in the xml? For example:
new EditText(this);
set like
<EditText
android:id="@+id/h1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="numberDecimal"/>
Upvotes: 13
Views: 19168
Reputation: 13535
Use of android:numeric
is deprecated, use android:inputType="number"
Upvotes: 12
Reputation: 741
Simply use the below lines in the xml file
To accept only Numbers put:
android:inputType="number"
To limit the length of the input numbers:
android:maxLength="10"
To accept only specific numbers:
EX:1 The below line accept only 1 or 0.
android:digits="10"
EX:2 The below line accept only 2 or 3.
android:digits="23"
Upvotes: 1
Reputation: 11451
Something like this perhaps ?
EditText text = new EditText(this);
text.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL);
Upvotes: 14