I have a 32dp * 32dp TextView and a single "A" character in center with size of 10pt. In my device "A" is exactly fit to bounds (from top and bottom and looks good) but in other devices it is smaller or overflows the bounds.
Does dp, ds, px or other units help? (please remind I am not searching for this automatic resizeable font where I have exact height of 32dp)
Use sp as the unit of measurement to set text size to support different densities across devices.
Also, read this for a good explanation of dp/sp.
I occured a problem when using RadioButton. The RadioButton's layout_height must be 15dp, and the offical design guide says that 1sp is 1dp, so I set the font size to 1sp, but the TextView's layout_height is a little bigger than 15dp. It seems that when the font size is 15sp, the layout_height of the TextView is 18dp. So what's the relationship between the font size and the layout_height?
First thing I wanted to mention is the font-padding, which is by default included into the TextView size. So to make TextView's size closer to the size of the text itself - specify android:includeFontPadding="false"
Now, if you compare View with layout_height equals to 20 sp and TextView with TextSize 20sp - they are going to be almost equally high.
(There's small difference, as text might contain very high letter like "Å" or the letter like "y" which goes below the baseline)
About sp vs dp.
Scale-independent Pixels - This is like the dp unit, but it is also
scaled by the user's font size preference. It is recommend you use
this unit when specifying font sizes, so they will be adjusted for
both the screen density and the user's preference.
It depends on how big is your font (Settings->Accessibility->Enable/Disable Large Text). You can change it and see the effect.
Hope, it helps.
I am making a mobile site, and trying to do it so the title h1 tag will adjust to 80% of the screen size.
According to CSS Values and Units Module Level 3 (Candidate Recommendation), you can use the vh unit, which is equal to 1% of the width of the initial containing block. So for example, font-size: 5vh would set font size to 5% of the body width. Browser support is fairly good in newest versions, but this excludes e.g. IE 8. This more or less answers the question in the title.
The description in the body of the question is something rather different. The font size is the height of the font and does not have any defined relationship with the widths of letters (which vary), so there is really no way in CSS to make some text occupy 80% of some width. You can set the width of an h1 element of course, but this is very different from setting the width of its text content. You would need JavaScript to set the text width.
Font size can't be use as percentage (%)
you can use em and px extensions for font size
for example
font-size:2 em;
or
font-size:10px;
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What means Ems? (Android TextView)
In EditText there is an attribute named android:ems. The description
is "Makes the EditText be exactly this many ems wide"
What does ems mean?
Taken from: http://www.w3.org/Style/Examples/007/units:
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.
em is basically CSS property for font sizes.
An "em" is a typographical unit of width, the width of a wide-ish
letter like "m" pronounced "em". Similarly there is an "en".
Similarly "en-dash" and "em-dash" for – and —
-Tim Bray
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.
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.
Ems is a typography term, it controls text size, etc. Check here
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.
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'.
em is the typography unit of font width. one em in a 16-point typeface is 16 points
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.
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
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