EMs
Android
TextView
Android Development
User Interface

What is meant by Ems? Android TextView

Interview Questions practice on Codemia

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

Browse interview questions

Introduction

In Android, ems is a width measurement based on text size rather than raw screen pixels. On a TextView, it tells the view to reserve space roughly equal to a chosen number of M character widths in the current font.

What ems actually controls

The important word is "roughly." ems does not mean the view will always display exactly that many characters. It means the width is based on the font's em size, which is tied to typography, not to the exact string currently shown.

That makes ems useful when you want a text field or label to feel like it was sized in character units instead of dp. For example, a login form might want an input that visually fits about 12 characters regardless of the device density.

XML and code examples

Here is a simple XML example:

xml
1<TextView
2    android:layout_width="wrap_content"
3    android:layout_height="wrap_content"
4    android:ems="10"
5    android:text="Hello" />

And the same idea in code:

kotlin
textView.setEms(10)

In both cases, the width is based on the current text appearance. If the font size changes, the actual on-screen width changes too.

Why ems is different from dp

dp measures physical UI space in a density-independent way. ems measures typographic space. That distinction matters because text-heavy UI often wants sizing that scales naturally with the font rather than staying fixed in raw layout units.

If you increase the text size for accessibility, a width specified in ems grows with it. A width specified in dp does not, which can make text fields feel cramped or produce more wrapping than expected.

ems, minEms, and real layout behavior

ems is most useful when the view is allowed to size itself from content. If the parent layout already forces a fixed width or uses match_parent, the ems value may have little visible effect. Android also provides minEms and maxEms, which are often better when you want bounds instead of one exact typographic width.

This is especially relevant for EditText. A text input with minEms can stay readable without forcing one exact width in every orientation. In contrast, a hard dp width can feel too small at large font sizes or too wide at small ones.

When ems helps and when it does not

ems is useful for text inputs, labels with predictable short content, and form-like layouts. It is less useful when the view already has a fixed or constrained width from the parent, such as match_parent inside a full-width container. In that case, the parent layout usually wins and the ems value may not matter much.

Android also provides related properties such as minEms and maxEms, which are helpful when you want bounds rather than one exact typographic width.

Common Pitfalls

  • Thinking ems="10" means exactly ten arbitrary characters will always fit.
  • Mixing ems with a parent layout rule that already determines the width completely.
  • Using ems when you really wanted a density-based physical size in dp.
  • Forgetting that font size changes also change the measured width.
  • Assuming proportional fonts behave like monospace fonts for character fit.

Summary

  • 'ems sizes a TextView using typographic width, not raw pixels.'
  • One em is based on the current font's character metrics, traditionally the width of M.
  • It is useful when you want width to scale naturally with text size.
  • It does not guarantee an exact number of displayed characters.
  • Use ems, minEms, or maxEms when text-relative sizing is more appropriate than dp.

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.