askimp
askimp

Reputation: 155

How to add a line break and space in TextView?

I am setting following string as a input for my txtMessage :

protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.breaknotification);

    TextView txtMessage = (TextView)findViewById(R.id.txtMessage);
    String msg = "TIME   RECORD        VALUE\n" +
                 "10 AM    1            20\n"+
                 "11 AM    2            30\n"+
                 "12 PM    3            40";

    txtMessage.setText(msg);

}

my breaknotification.xml file is as per following:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

<TextView
    android:id="@+id/txtMessage"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="5dp"
    android:text=""
    android:textColor="#FFFFFF"
    android:textSize="21sp"
    android:textStyle="bold" />

</LinearLayout>

and I want to print my string as per following format.

TIME   RECORD     VALUE
10 AM     1        20
11 AM     2        30
12 PM     3        40

But it is not showing me exactly what I want; it shows me following in my message,

TIME     RECORD      VALUE
`10 AM    1       20`
`11 AM    2       30`
`12 PM    3       40`

So how can I achieve it?

Upvotes: 1

Views: 6840

Answers (4)

askimp
askimp

Reputation: 155

I found the following solution :
added textView property : android:typeface="monospace"

<TextView
  android:id="@+id/txtMessage"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_marginLeft="5dp"
  android:text=""
  android:textColor="#FFFFFF"
  android:typeface="monospace"
  android:textSize="21sp"
  android:textStyle="bold" />

Upvotes: 0

Rejinderi
Rejinderi

Reputation: 11844

txt_Message.setText("TIME\tRECORD\tVALUE\n10 AM\t1\t20\n11 AM\t2\t30\n12 PM\t3\t40");

can u try tab characters?

Upvotes: 0

ChapMic
ChapMic

Reputation: 28144

Here is you answer :

txt_Message.setText("TIME    RECORD    VALUE \n10 AM   1    20 \n11 AM   2    30 \n12 PM   3    40");

You can also use HTML code in a textView with Html.fromHtml :

txt_Message.setText(Html.fromHtml("Test<br>Text2"));

It is not the best to use but it will let you have a linebreak too.

Upvotes: 1

Hesham Saeed
Hesham Saeed

Reputation: 5378

txt_Message.setText("TIME    RECORD    VALUE \n10 AM   1    20 \n11 AM   2    30 \n12 PM   3    40");

Upvotes: 3

Related Questions