Hello ! I’m Xavier Jouvenot and in this small post, I am going to explain how to tell the user what to put in an EditText.
Self promotion: You can find other articles on Android development on my website 😉
Specifying your message by default
To add a indication into an empty EditText
, to guid the user, there are several solutions. And the first one is to specify a default message as an hint to be displayed in an EditText
.
To do so, we are going to modify the EditText
XML definition, and add one attribute to it.
This attribute is android::hint
and you can specify the text you want to be shown as a indication to this attribute.
Here is how such EditText
XML definition looks like:
<EditText
<!--Some attributes-->
android:hint="A very useful hint."
<!--Some other attributes --> />
If not specified, this attribute is set to an empty string.
Modifying the hint dynamically
Specifying the attribute in the XML is great!
But if you want to modify the text displayed as a hint in a EditText
while the program is running, then, you must use some Java code and the method setHint
.
Here is how it looks like:
EditText et = findViewById(R.id.my_edit_text_id);
et.setHint(R.string.hello);
Since the method setHint
takes an integer as a parameter, you should definitely use the resources from your application to store the text of the hints.
Thank you all for reading this article,And until my next article, have an splendid day 😉
Top comments (2)
What do you mean by placeholder?
The hint is the text displayed when the editText is empty to hint the user about what to type.
If you want to see some text in the editor there is also the attribute
tools:text
.tools:text documentation
I though that a hint or a placeholder where the same in this case, meaning that it was an element place to indicate what to put in an empty field.
Your comment had me look to the definition of placeholder, and my definition was not good ! I will edit this article to remove this error.
Thank you for pointing it out :-D