Set hint text for EditText

Description

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  va2s  .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;
/*from www . j  a  v a2 s  . c  om*/
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);

    }

}
Set hint text for EditText




















Home »
  Android »
    Android UI »




UI Basics
Action Bar
Animation
Button
Canvas
CheckBox
Clock Date Picker
Dialog
EditText
Event
Fragment
Gesture
GridView
ImageView
Layout
ListView
Map
Menu
Model
OpenGL
ProgressBar
RadioButton
Spinner
Tab
TextView
Thread
Toast
Video
View
WebView