1

My question is not about the number of chars nor the textSize attribute in TextView/EditText.

I have a fixed EditText. And the maxLength is 10. And all the chars must be shown at once. However, each char has different width and height. For example, l and L. If you drag the mouse over(block selection) the l and then the L. You will notice that they have different width.

So, LLLLLLLLLL and llllllllll has different width.

For instance,

LLLLLLLLLL

llllllllll

(both are 10 letters)

Moreover, different font shows different width.

My question is how to get the actual width of chars in PX so that the string won't hidden in the EditText. Let's say, the width of EditText is 200px. And it's limited to 10 letters. And it will show

LLLL(LLLLLL) // (LLLLL) is not shown because of the EditText's width. But you can see when you move the cursor.

llllllllll

How can you get the width of the chars?

c-an
  • 3,543
  • 5
  • 35
  • 82

1 Answers1

0

you can use TextPaint and measure letters, but there is an easier way for your case. get familiar with em unit

you can declare this value for EditText/TextView by setEms(intNum) or android:ems="intNum". set also width to wrap_content and your View will have width of intNum widest characters (usually Ms). there are also useful minEms and maxEms attributes

edit: HERE you have more complex explanation

PS. don't declare sizes of your Views with px unit due to different densities, use dp. some units explanation in HERE

snachmsm
  • 17,866
  • 3
  • 32
  • 74