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.
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:
And the same idea in code:
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
emswith a parent layout rule that already determines the width completely. - Using
emswhen you really wanted a density-based physical size indp. - Forgetting that font size changes also change the measured width.
- Assuming proportional fonts behave like monospace fonts for character fit.
Summary
- '
emssizes aTextViewusing typographic width, not raw pixels.' - One
emis based on the current font's character metrics, traditionally the width ofM. - 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, ormaxEmswhen text-relative sizing is more appropriate thandp.
Related reading
- What is NSLayoutConstraint UIView-Encapsulated-Layout-Height and how should I go about forcing it to recalculate cleanly?
- What is NSZombie?
- What is Prefix.pch file in Xcode?
- What is the AppDelegate for and how do I know when to use it?
- What is the best IDE to develop Android apps in?
- What is the best solution for background task using Swift async, await, MainActor
- What is the best way to check if a UIAlertController is already presenting?
- What is the best way to deal with the NSDateFormatter locale feature?
.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 courseTrack 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.