Problem with string alignment

Asked

Viewed 296 times

-1

I have a problem in my Android project that I am not able to solve. I have a series of strings that are summed to form a larger string as the following example:

String teste1 = "O.P.                Processo            Célula";
String teste2 = "20350               A                   005";

However, when displayed on screen the letter A and 005 are not under the words "Process" and "Cell". I even pasted this code into Skype to pass to a person and it didn’t come lined up either. Because this occurs?

  • @Alexandremarcondes, I decided to reformulate my question to better express my problem

  • @I transferred the answer to another, I only saw that it was duplicated when they started voting to close. ( http://answall.com/questions/2468/comoralinr-strings-para-utilizar-em-um-listview )

  • OK @Bacco I tried to delete this question but I couldn’t, so I marked it as duplicate and I voted to close it too

  • 1

    I don’t think this is a duplicate. The other question asks how to line up columns in one ListView, while this questions why aligning characters with columns does not result in aligned text. The other asks for the right way to do one thing, this asks why the wrong way is wrong.

  • @Danielc.Sobral himself commented that he decided to reformulate to better express the problem. I believe that this question should be closed even though it is not technically a duplicate, and the author has done both to solve the same problem. This I took into consideration to score. The other best expresses his problem and solves.

  • @Alexandremarcondes I do not agree that it is the intention that is worth. The usefulness of Stack Overflow is that this question will be useful to other people, and even if the author is trying to solve a single problem, other people might wonder why character alignment doesn’t work without it being related to ListView.

  • Well, if the question has clearer answers with code I even agree @Danielc.Sobral, but as it is seems much more superficial than the other and the two end up answering the same thing, even though they are different. Give an edited then to have something better then. Sincerely the way it is I don’t think it’s worth keeping.

Show 2 more comments

1 answer

9

For this you need to use monospaced fonts, such as Courier.

Monospaced fonts, unlike proportional fonts, are those where all letters have the same widths. See just the comparison:

Example 1

XMILSW
WWMWMW
i01 . m

Example 2

XMILSW
WWMWMW
i01 .m

Note that in the first case the letters do not align and in the second yes.

Take a look at the documentation of typeface MONOSPACE to see some options in this direction.

Consider the Grid View as an alternative. Grid View allows you to have multiple separate columns, aligned independently of the content.

  • Sorry to ask, but would you know where I change the font then on an android project? I’m using a listview

  • I added a reference to the API in the reply, I hope it helps.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.