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
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:
Kotlin:
Styling the hint
Change the hint color with android:textColorHint. android:textSize sets the text size of the field:
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:
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:
Then reference it from XML or code:
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.
- Clarity: say exactly what action or information is required.
- Brevity: keep it short.
- Context: make it relevant to the current app state or user session.
- 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.
| Feature | Implementation | Additional Notes |
| XML Placeholder | android:hint="Enter text" | Static hints easily defined in XML |
| Programmatic Placeholder | editText.setHint("Enter text") | Useful for dynamic hints |
| Styling Placeholder | android:textColorHint="@color/gray"
android:textSize="18sp" | Customize color and size for better UX |
| Localization | Use strings.xml resources | Enables multi-language support |
Related reading
- Android adding simple animations while setvisibility(view.Gone)
- Android adding simple animations while setvisibilityview.Gone
- Android allow portrait and landscape for tablets, but force portrait on phone?
- Android and setting width and height programmatically in dp units
- Android API 21 Toolbar Padding
- Android app crashes when launched in debug mode
- Android AsyncTask threads limits?
- Android basics running code in the UI thread
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseNo 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 courseData 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.