Android Studio, How to

Quick Tip – How to tell the user what to put in an EditText ?

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 --> /&gt;

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 😉

Interesting links

Votre commentaire

Entrez vos coordonnées ci-dessous ou cliquez sur une icône pour vous connecter:

Logo WordPress.com

Vous commentez à l’aide de votre compte WordPress.com. Déconnexion /  Changer )

Photo Google

Vous commentez à l’aide de votre compte Google. Déconnexion /  Changer )

Image Twitter

Vous commentez à l’aide de votre compte Twitter. Déconnexion /  Changer )

Photo Facebook

Vous commentez à l’aide de votre compte Facebook. Déconnexion /  Changer )

Connexion à %s