What is meant by Ems? (Android TextView)
Asked Answered
C

8

342

What is meant by Ems (related to a TextView)? For example in

android:ems     setEms(int)

Makes the TextView be exactly this many ems wide.

Calderon answered 13/8, 2011 at 22:35 Comment(3)
en.wikipedia.org/wiki/Em_(typography)Cherimoya
Anyway, that's good to have a look at google before asking here: the answer is on the top results if you enter "ems android".Whaleback
Yes I searched some terms didnt use "android" keyword, Thanks all for your answersCalderon
W
426

android:ems or setEms(n) sets the width of a TextView to fit a text of n 'M' letters regardless of the actual text extension and text size. See wikipedia Em unit

but only when the layout_width is set to "wrap_content". Other layout_width values override the ems width setting.

Adding an android:textSize attribute determines the physical width of the view to the textSize * length of a text of n 'M's set above.

Wallace answered 24/6, 2012 at 13:36 Comment(2)
Why does my EditText can have 17 "M" letters even though I set it to 10ems? I have three EditText in a horizontal LinearLayout and all of them set to "wrap_content"Barcelona
set minEms and maxEmsIcefall
R
60

Ems is a typography term, it controls text size, etc. Check here

Rebeca answered 13/8, 2011 at 22:38 Comment(1)
It relates text size to sizes of other things.Baffle
E
35

It is the width of the letter M in a given English font size.
So 2em is twice the width of the letter M in this given font.
For a non-English font, it is the width of the widest letter in that font. This width size in pixels is different than the width size of the M in the English font but it is still 1em.
So if I use a text with 12sp in an English font, 1em is relative to this 12sp English font; using an Italian font with 12sp gives 1em that is different in pixels width than the English one.

Emsmus answered 24/6, 2012 at 13:24 Comment(0)
C
12

While other answers already fulfilled the question (it's a 3 years old question after all), I'm just gonna add some info, and probably fixed a bit of misunderstanding.

Em, while originally meant as the term for a single 'M' character's width in typography, in digital medium it was shifted to a unit relative to the point size of the typeface (font-size or textSize), in other words it's uses the height of the text, not the width of a single 'M'.

In Android, that means when you specify the ems of a TextView, it uses the said TextView's textSize as the base, excluding the added padding for accents/diacritics. When you set a 16sp TextView's ems to 4, it means its width will be 64sp wide, thus explained @stefan 's comment about why a 10 ems wide EditText is able to fit 17 'M'.

Careworn answered 8/11, 2020 at 5:35 Comment(0)
V
7

em is the typography unit of font width. one em in a 16-point typeface is 16 points

Vincentvincenta answered 2/8, 2016 at 12:47 Comment(0)
C
5

ems is a unit of measurement

The name em was originally a reference to the width of the capital M. It sets the width of a TextView/EditText to fit a text of n 'M' letters regardless of the actual text extension and text size.

Eg :

android:ems Makes the EditText be exactly this many ems wide.

<EditText
    android:ems="2"
/>

denotes twice the width of letter M is created.

Crude answered 23/9, 2017 at 8:41 Comment(0)
U
1

em is basically CSS property for font sizes.

The em and ex units depend on the font and may be different for each element in the document. The em is simply the font size. In an element with a 2in font, 1em thus means 2in. Expressing sizes, such as margins and paddings, in em means they are related to the font size, and if the user has a big font (e.g., on a big screen) or a small font (e.g., on a handheld device), the sizes will be in proportion. Declarations such as text-indent: 1.5em and margin: 1em are extremely common in CSS.

Source:https://www.w3.org/Style/Examples/007/units

Uyekawa answered 3/8, 2017 at 6:8 Comment(0)
B
0

To add to the other answers in Android, Ems size, can, by default, vary in each language and input.

It means that if you want to set a minimum width to a text field, defined by number of chars, you have to calculate the Ems properly and set it, according to your typeface and font size with the Ems attribute.

To those of you struggle with this, you can calculate the hint size yourself to avoid messing with Ems:

val tf = TextField()
val layout = TextInputLayout()
val hint = "Hint"

val measureText = tf.paint.measureText(hint).toInt()
tf.width = tf.paddingLeft + tf.paddingRight + measureText.toInt()
layout.hint = hint
Blowup answered 8/12, 2020 at 8:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.