android – Using tabulator / tabspace in a TextView-ThrowExceptions

Exception or error:

I wish to use some tabspace in a line where I call setText

if(id==R.id.radioButton1){
            title.setText(numbertext.getText()+" Grams");

In this case I would like to tab between numbertekst.getText() and the grams.
The output is part of a simple row with values. But as I can’t find the tab, the row is a little ugly.

I know I can use multiple textfields or even a table. But as I have a lot of code already I don’t want to go that way yet.

I tried the Java way with putting \t, however that seems not to work.

Any help would be appreciated.

How to solve:

You should add tabulator to the text, but \t don’t work in settext. The solution is add the character \u0009 that represents in unicode char the tabulator, in a string resource.

<string name="tab">\u0009</string>

TextView hello = (TextView) findViewById(R.string.helloTextView);

hello.setText("e"+getString(R.string.tab)+"e");

just combine more tab to have more space

getString(R.string.tab)+getString(R.string.tab)+getString(R.string.tab)

###

“\t” works as well, and “\n” is used to start new line

###

If “\t” doesn’t work and \u0009 (Unicode for a tab) only shows 1 space:

In values/strings.xml add

<string name="tab">\u0009\u0009\u0009\u0009</string>

Then in your Java file add

textView.setText("text" + getString(R.string.tab) + "moretext");

Leave a Reply

Your email address will not be published. Required fields are marked *