I am new to the android application development. I am developing an application. While doing design for this app i am stuck in android:ems. What is this components.
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
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.
An em is a unit in the field of typography, equal to the currently specified point size.
More information about ems: wikipedia
More information about ems in Android doc
Makes the TextView exactly this many ems wide by using setEms (int ems)
There is an issue with English text in TextView and WebView when using Arabic or Persian typeface, The following image is an example of Persian and English text in both TextView (2 views in the header) and WebView (at the bottom) when using default typeface : Shot 1
But when applying a custom typeface it looks like this (Iran Sans Serif is applied here but almost all Persian fonts have the same issue) : Shot 2
As you can see, in TextView with Persian typeface, English texts are not aligned correctly and in WebView there is extra padding between some lines due to difference between Persian texts size and English texts size.
Be sure that there is no problem with the fonts because in Websites, they're working like a charm!
I've tried like this:
For five spaces used: &#npsp; &#npsp; &#npsp; &#npsp; &#npsp; but it is not provided proper space for all lines in my passage and I thought it is a bad coding practice.
I've used , , , (someone wrote in Yahoo Answers). Although some are wrong in this but not anyone of this worked for me
I've used <pre> (my text)</pre> worked fine but the font of the text is changed to some ugly format of font.
<tab align=6> also used but not worked for me.
I am doing coding in webview in Android to display the text on the screen.
Can anyone please help me out to provide proper horizontal tab space in front my text.
Thank you..
You can use the CSS style text-indent to indent the text.
Add a style like text-indent: 40px to the element that contains the text, for example using the style attribute:
<p style="text-indent:40px"></p>
You can also put the CSS in a style sheet and load the style sheet in the webview. In the style sheet you can for example specify the text-indent for all paragraph tags:
p { text-indent: 40px; }
Another option is using the tag <blockquote></blockquote>. But it is not its intended use, it is meant for quoting text.
Html3 had a <tab> element that did not survive into html4 or html5.
However, it is easy to define this element in CSS:
tab:before {content:"\9"; white-space:pre;}
This has the correct content - a tab character with Unicode code 9, and preserved white space. The preserved white space setting only operates on that one character, so you can write html like this:
<div>
This is a<tab/><tab/><tab/>Test<tab/><tab/>and Test<br>
Another<tab/><tab/><tab/>Test<tab/><tab/>and Test<br>
Yet Another<tab/><tab/>Test<tab/><tab/>and Test
</div>
And everything will work as you expect.
See this jsfiddle for the same example.
By default css defines the width of a tab character to be 8 times the width of a space. But you can change this in modern browsers using the CSS tab-size property.
My app uses both TextViews and WebViews. In both I set the size of text in pt. The problem is that the text in WebView is usually half the size as the text in TextView. I have tested on multiple screen sizes and desnsities but it makes no difference.
I set TextView height using:
textView.setTextSize( TypedValue.COMPLEX_UNIT_PT, 14 );
But in WebView I use CSS:
font: 14pt;
Both work, but it seems like TextView's display is bigger than WebView's. Any clue what is going on? I want to display the text in TextView as the same size as in my WebView.
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