How can I set a minimum width (in characters) for a TextView?

If This Is Art picture If This Is Art · Jul 6, 2010 · Viewed 22.2k times · Source

I've had a good search for this on here and can't find a solution.

I have a TextView in a RelativeLayout which contains an integer number. The number will range between 1 and 99 - can anyone tell me how to size the TextView so that its width is always the width of the string "99" even if it only contains "1"?

I need this because the positions of the components to the right of this TextView depend on its width, so all are position depending on how many digits the TextView contains.

I don't mind if this is done in XML or code - I just want to avoid having to set the width of a TextView in pixels!

Thanks for any possible solutions; Please ask if I've missed out any important info!

Answer

Adrian picture Adrian · Jun 4, 2011

try this:

android:layout_width="wrap_content"
android:minEms="2"