nirmani Deveraj
nirmani Deveraj

Reputation: 31

String tab in textview programmatically

I need to put String tab in textview. how to do it with android Text view

textview1.setText("ABC -  answer");
textview2.setText("ABSDE -  answer");
textview3.setText("SOMETEXT -  answer");

//Required out put
ABC        -  answer
ABSDE      -  answer
SOMETEXT   -  answer

Upvotes: 2

Views: 2355

Answers (2)

jhkuperus
jhkuperus

Reputation: 1469

If you are displaying the text in a fixed-width font, there are ways to pad Strings with spaces. You can use Apache Commons Lang StringUtils for that. It has leftPad and rightPad methods.

Another option is by using String.format(). For example:

System.out.println(String.format("%-10s - %s", "ABC", "answer"));
System.out.println(String.format("%-10s - %s", "ABCDE", "answer"));
System.out.println(String.format("%-10s - %s", "SOMETEXT", "answer"));

Produces:

ABC        - answer
ABCDE      - answer
SOMETEXT   - answer

See Java Format String syntax.

Upvotes: 5

Kawawa Dennis
Kawawa Dennis

Reputation: 31

Do this

 textview.setText(String.format("%s \t -  %s", "ABC ", " answer"));

Upvotes: 0

Related Questions