Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 741
Nick Avatar asked Mar 05 '12 18:03

Nick


2 Answers

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.

like image 64
Manfred Moser Avatar answered Oct 16 '22 23:10

Manfred Moser


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.

like image 1
sabadow Avatar answered Oct 17 '22 00:10

sabadow