99

Really looking for an answer to this.

Want to set the width of a TextView based on a number or characters. So if I want it to always be 3 characters wide, I could set that and it will adjust based on the textSize setting. Without this, I have to set a width to 50dip or something, which may or may not be right depending on the font size.

Community
  • 1
  • 1
Fraggle
  • 8,607
  • 7
  • 54
  • 86
  • There is possibly a way to do this via specifying your dimension in `sp`. – CommonsWare May 28 '11 at 14:51
  • 1
    @CommonsWare, yeah, if you can tell me how many SP equal one character that might work (but see my answer below using minEms as I think that is what I want). So if text size=30sp and I want 3 characters wide, do I set width to 90sp? I guess I could try it. – Fraggle May 30 '11 at 12:45
  • 2
    Yeah, `minEms` is probably a better answer. – CommonsWare May 30 '11 at 12:55

4 Answers4

191

Answering my own question...

And the winner is: set the minEms attribute (android:minEms) !!!

So "ems" it turns out refers to the size of the widest character, typically an "M", get it? So setting minEms to an integer value say 3, on an EditText or TextView should ensure it's at least 3 characters wide. You can set the maxEms as well.

So Kyle's answer on this thread, although wrong, caused me to find the answer, so thanks Kyle.

Jonik
  • 80,077
  • 70
  • 264
  • 372
Fraggle
  • 8,607
  • 7
  • 54
  • 86
  • 3
    of course I'm still testing to see if this actually works, but so far it looks like it. – Fraggle May 29 '11 at 01:45
  • 8
    Also note that `layout_width` needs to be `wrap_content` for minEms and maxEms to work as intended, as [this answer](http://stackoverflow.com/a/11177925/56285) points out. – Jonik Dec 09 '14 at 13:07
5

ems, contrary to popular beliefs (or at least from most thread about ems here), is not based from the width of a single 'M'.

It was originally like that in typography, but in digital medium, including Android, its meaning was shifted to the size of the typeface used, or in other word, its height (excluding any padding for accents/diacritics).

So that means when you specify the ems for a TextView, it will used its textSize as a base and multiply it by the ems specified.

As a sample, if you set a 16sp TextView's ems to 4, its width will be 64sp wide. You can easily test it by using two TextView (with includeFontPadding set to false) side-by-side inside a ConstraintLayout (to leverage its layout_constraintDimensionRatio).

Andrei Stakov
  • 177
  • 2
  • 2
0

Most fonts do not set all character widths to be identical. Courier is perhaps the most used exception to this, but generally look at the difference between an i and an A, for example. This makes what your asking to do a bit of a problem.

mah
  • 39,056
  • 9
  • 76
  • 93
  • right, so I was hoping that this problem had been solved. So set the width in terms of characters and then at display time find out the font characteristics for that field and determine how big it should be based on that. So really asking if this problem had been solved, in a nice easy way. Sounds like not. – Fraggle May 28 '11 at 15:13
-3

Try using: android:maxLength="3"

This will limit your text to a specific number of characters.

Renetik
  • 5,887
  • 1
  • 47
  • 66
KyleM
  • 618
  • 5
  • 17