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 --> />

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

原文链接:Quick Tip – How to tell the user what to put in an EditText ?

© 版权声明
THE END
喜欢就支持一下吧
点赞5 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容