Question

I have a problem with formating text into TextView fields. I am populating strings with data from database and I want to make it line up:

for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
SomeString = SomeString + String.format("|%1$-4s|%2$-12s|%3$-10s|\n",
    cursor.getString(someColumn1),
    cursor.getString(someColumn2),
    cursor.getString(someColumn3));
    }
Return SomeString

Than I set this string into textView field. Actually, it looks like this:

|SomeColumn1|SomeColumn2|SomeColumn3|  
|SomeColumn1|SomeColumn2StringLonger|SomeColumn3|    
|SomeColumn1|ShorterC2|SomeColumn3|

This is what it should look like:

|SomeColumn1-----|SomeColumn2-----|SomeColumn3|  
|SomeColumn1-----|SomeLargerString|SomeColumn3|  
|SomeColumn1-----|Shorter---------|SomeColumn3|  

I've tried with String.format("|%1$.4s|%2$.12s|%3$.10s|\n")

Was it helpful?

Solution

I had a same problem.

Just add this line:

android:typeface="monospace"

into textView xml definition, formatting will work now.

It was all about width of single character. White spaces are obviously shorter then other signs. Monospace makes the characters have the same width. That's all, simple as that.

Cheers!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top