Search code examples
androidtextviewalignmentspacing

2-column alignment in a Textview


I've got a TextView in my app that will, on each line, have a label and then a data value. For instance, some line could look like:

Pressure (atm) 0.983
Acceleration 10.277

Now, I have a handful of data values at once, the labels all of various character lengths. I want to data values themselves to be spaced over a bit from the labels, and all lined up, like so:

Pressure (atm)   0.983
Acceleration     10.277

Is there any way to do this? Thanks!


Solution

  • In my experience this is easier to do by separating value and label into two separate TextViews that you add to a LinearLayout for each row. And in the LinearLayout you can use layout weights to distribute it how you like.