5

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!

Nick
  • 6,900
  • 5
  • 45
  • 66

2 Answers2

4

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.

Manfred Moser
  • 29,539
  • 13
  • 92
  • 123
  • I have different layouts for various screen sizes and densities, that would be an extra pain to modify all of. Is there no good way to do this is a single textview? – Nick Mar 05 '12 at 19:13
  • use include.. and the included file would have the linearlayout.. trying to make a single textview work nicely will be more painful. I have tried.. – Manfred Moser Mar 05 '12 at 19:23
  • Okay, fair enough. Thank you for your help – Nick Mar 07 '12 at 18:41
1

You should add tabulator to the text. The problem is how to add it in XML, because \t don't work. The solution is add 	 that represents a tabulator.

For example in XML definition on Strings.xml

<string name="hello">Hello World, &#9;&#9; TesttabsActivity!</string>

In code you can also use the \t option:

TextView hello = (TextView) findViewById(R.id.helloTextView);
hello.setText("Hello\t\t\tWorld");

But this option don't align, only adds spaces (the XML solution also align)

Hope it helps.

sabadow
  • 5,095
  • 3
  • 34
  • 51
  • No, I'm already using \t, as you can see here: testResult.append("\nTime (GMT)\t" + str); however this doesn't space them the way I want. It just spaces the data over from the label, but doesn't align – Nick Mar 05 '12 at 18:51
  • Is there a similar character for Newline. – Mohit Sehgal Mar 22 '14 at 18:51