Hello ! I’m Xavier Jouvenot and in this small post, I am going to explain how to create an EditText
which takes only numbers.
Self promotion: You can find other articles on Android development on my website 😉
Numbers only by default
To make sure that a user only enter numbers in an EditText
, there are several solutions.And the first one we are going to see is to specify that the EditText
should only take numbers by default.
To do so, we are going to modify the EditText
XML definition, and add one attribute to it.This attribute is android:inputType
and can be set to a lot of different values depending on what you need. Here is a link to the documentation if you want to learn more about them.This attribute is in fact a TextView
attribute, but since EditText
inherits from TextView
, then it works.
Here is how an EditText
XML definition looks like, when you want to allow only numbers:
<EditText
<!--Some attributes-->
android:inputType="number"
<!--Some other attributes --> />
If not specified, this attribute is set to text
for an EditText
.
Modifying the input type dynamically
Specifying the attribute in the XML is great!But if you want to modify the style of a EditText
or a TextView
while the program is running, then, you must use some Java code and the method setRawInputType
.Here is how it looks like:
EditText et = findViewById(R.id.my_edit_text_id);
et.setRawInputType(InputType.TYPE_CLASS_NUMBER);
If you want to know what other option you can pass to setRawInputType
, I encourage you to look at the InputType documentation.
Thank you all for reading this article,And until my next article, have an splendid day 😉
Top comments (0)