Android
EditText
Placeholder Text
User Interface
Mobile Development

Add placeholder text to an Android EditText

Codemia Knowledge Hub ·

Android calls placeholder text a hint. Add it to an EditText with the android:hint attribute in XML, or with setHint() (Java) or the hint property (Kotlin) in code. The hint gives users a subtle cue about the expected input format or content, and it disappears as soon as they start typing. It is simple to add, but a well-chosen hint noticeably improves the experience of filling in a form.

Setting the hint in XML

xml
1<EditText
2    android:id="@+id/editText"
3    android:layout_width="match_parent"
4    android:layout_height="wrap_content"
5    android:hint="Enter your name" />

Setting the hint in code

You can also set the hint programmatically in Java or Kotlin, which is useful when the text needs to be dynamic rather than fixed in the layout file.

Java:

java
EditText editText = findViewById(R.id.editText);
editText.setHint("Enter your name");

Kotlin:

kotlin
val editText: EditText = findViewById(R.id.editText)
editText.hint = "Enter your name"

Styling the hint

Change the hint color with android:textColorHint. android:textSize sets the text size of the field:

xml
1<EditText
2    android:id="@+id/editText"
3    android:layout_width="match_parent"
4    android:layout_height="wrap_content"
5    android:hint="Enter your name"
6    android:textColorHint="@color/gray"
7    android:textSize="18sp" />

Changing the hint based on user state

For a more interactive app, the hint can change based on user state or preferences, for example by including the user's name when you already know it:

kotlin
val userName = getPreferences(MODE_PRIVATE).getString("user_name", "").orEmpty() // obtain user name from preferences or previous sessions
val placeholderText = "Enter your ${if (userName.isNotEmpty()) userName else "name"}"
editText.hint = placeholderText

Localizing the hint

For apps that support several languages, localizing the hint is essential. Define it as a string resource in strings.xml, with one file per language:

xml
1<!-- res/values/strings.xml -->
2<string name="hint_placeholder_name">Enter your name</string>
3
4<!-- res/values-es/strings.xml -->
5<string name="hint_placeholder_name">Ingrese su nombre</string>

Then reference it from XML or code:

xml
1<EditText
2    android:id="@+id/editText"
3    android:layout_width="match_parent"
4    android:layout_height="wrap_content"
5    android:hint="@string/hint_placeholder_name" />

Writing a good hint

A well-thought-out hint does more than explain the field: it improves input accuracy, because mistakes and misunderstandings are less likely when users have a clear example or expectation.

  1. Clarity: say exactly what action or information is required.
  2. Brevity: keep it short.
  3. Context: make it relevant to the current app state or user session.
  4. Accessibility: keep enough color contrast for users with visual impairments.

In performance-sensitive places such as lists or dynamic forms, use EditText fields judiciously and load them lazily when you create many of them.

FeatureImplementationAdditional Notes
XML Placeholderandroid:hint="Enter text"Static hints easily defined in XML
Programmatic PlaceholdereditText.setHint("Enter text")Useful for dynamic hints
Styling Placeholderandroid:textColorHint="@color/gray" android:textSize="18sp"Customize color and size for better UX
LocalizationUse strings.xml resourcesEnables multi-language support

Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Basis Lab
No course covers this one yet

Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.

Build my course
Data Structures & Algorithms practice on Codemia

Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.

Practice algorithms