Android UI How to - Set hint text for EditText








You can specify hint text for EditText. This text will be displayed slightly faded and disappears as soon as the user starts to type text.

The purpose of the hint is to let the user know what is expected in this field, without the user having to select and erase default text.

In XML, this attribute is

android:hint="your hint text here"

or

android:hint="@string/your_hint_name"

where your_hint_name is a resource name of a string to be found in /res/values/strings.xml.

In code, you would call the setHint() method with either a CharSequence or a resource ID.

Example

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">
// w  w  w. j a  v a 2  s. c om
    <EditText
        android:id="@+id/myEditText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="your hint text here" />

</LinearLayout>

Java code

package com.java2s.app;
/*  w  ww.ja v  a2  s  .c  o m*/
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends Activity
{
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    }

}
null