EditText
Capitalization
Android Development
User Input
Text Formatting

First letter capitalization for EditText

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

Capitalization in EditText can mean two different things: asking the keyboard to suggest uppercase input, or actually enforcing a capitalized value in the text itself. Android supports both, but they solve different problems and should not be treated as interchangeable.

Start With inputType

If your goal is standard text entry behavior, the simplest solution is to choose the right inputType. Android keyboards read this hint and adjust their capitalization behavior accordingly.

xml
1<EditText
2    android:id="@+id/nameInput"
3    android:layout_width="match_parent"
4    android:layout_height="wrap_content"
5    android:hint="Full name"
6    android:inputType="textCapWords" />

Useful options are:

  • 'textCapSentences for the first letter of each sentence'
  • 'textCapWords for the first letter of each word'
  • 'textCapCharacters for every character'

This is usually enough for names, addresses, and message-style input fields.

Understand What inputType Does Not Guarantee

inputType is a keyboard hint, not a hard validation rule. A hardware keyboard, pasted text, autofill, accessibility service, or third-party input method can still insert lowercase text.

If you need the stored value to be capitalized regardless of how it was entered, you need code that transforms the text.

Enforce First-Letter Capitalization With an InputFilter

An InputFilter is a good choice when you want to modify input as it is typed. The example below uppercases only the very first character of the field.

kotlin
1import android.text.InputFilter
2import android.text.Spanned
3import java.util.Locale
4
5class FirstLetterCapsFilter(
6    private val locale: Locale = Locale.getDefault()
7) : InputFilter {
8    override fun filter(
9        source: CharSequence?,
10        start: Int,
11        end: Int,
12        dest: Spanned?,
13        dstart: Int,
14        dend: Int
15    ): CharSequence? {
16        if (source.isNullOrEmpty()) return null
17
18        val insertingAtStart = dstart == 0 && (dest == null || dest.isEmpty())
19        if (!insertingAtStart) return null
20
21        val first = source.subSequence(start, end).toString()
22        if (first.isEmpty()) return null
23
24        return first.replaceFirstChar { char ->
25            if (char.isLowerCase()) char.titlecase(locale) else char.toString()
26        }
27    }
28}

Attach it like this:

kotlin
editText.filters = arrayOf(FirstLetterCapsFilter())

This enforces the first character, even when the keyboard itself does not cooperate.

Use a TextWatcher for Richer Rules

If the rule is more complex, such as capitalizing after punctuation or normalizing pasted content, a TextWatcher may be easier to reason about.

kotlin
1editText.addTextChangedListener(object : android.text.TextWatcher {
2    override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) = Unit
3
4    override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) = Unit
5
6    override fun afterTextChanged(s: android.text.Editable?) {
7        if (s.isNullOrEmpty()) return
8
9        val updated = s.toString().replaceFirstChar { char ->
10            if (char.isLowerCase()) char.titlecase() else char.toString()
11        }
12
13        if (updated != s.toString()) {
14            editText.removeTextChangedListener(this)
15            editText.setText(updated)
16            editText.setSelection(updated.length)
17            editText.addTextChangedListener(this)
18        }
19    }
20})

This is more flexible, but it is also easier to get wrong because you have to avoid recursive updates and cursor jumps.

Choose the Right Level of Enforcement

A name field often needs only textCapWords. A promo code field may need textCapCharacters. A title field might need post-processing before saving, rather than forcing every keystroke through a filter.

The key question is whether capitalization is a typing convenience or a business rule. If it is only a convenience, prefer inputType. If it is a rule, validate or transform the text yourself.

Common Pitfalls

  • Assuming inputType guarantees capitalization for pasted or programmatically assigned text leads to inconsistent data.
  • Using a TextWatcher without temporarily removing it can cause endless update loops.
  • Forcing capitalization on fields such as email addresses or passwords creates a bad user experience.
  • Ignoring locale-sensitive casing rules can produce incorrect results in some languages.

Summary

  • Use android:inputType when you want the keyboard to suggest capitalized input.
  • Use an InputFilter or TextWatcher when capitalization must be enforced in the actual text value.
  • Treat keyboard hints and validation rules as separate concerns.
  • Match the capitalization strategy to the field's real purpose instead of enforcing one rule everywhere.

Related reading
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
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.