Satheesh Kumar
Satheesh Kumar

Reputation: 716

How to Align the Text Properly in TextView?

I have a Textview in which i need to show large information in proper format.How can i achieve it?

I want to display like this:

Title  :       Some Title.
Date   :       Date

More Info about the title.
......
......

Contact :      Contact Details.

Upvotes: 0

Views: 816

Answers (4)

Lithu T.V
Lithu T.V

Reputation: 20021

Well you have to do it by calculating the space between the options.

The better way

Use a Webview make an html string with contents aligned properly using html formattings and load webview and display it.

Upvotes: 1

appsroxcom
appsroxcom

Reputation: 2821

As suggested by others, WebView is the option for you. See How to programmatically set / edit content of webview

However, it is possible to do it in a TextView if the text doesn't need special formatting (ex. different color, font sizes) and the only formatting required is indentation.

In layout:

<TextView
    android:id="@+id/text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:singleLine="false" />

In Activity:

    TextView tv = (TextView) findViewById(R.id.text);
    tv.setText("Title  :       Some Title.\nDate  :       Date\n\nMore Info about the title.\n......\n......\n\nContact :      Contact Details.");

Upvotes: 0

zubke
zubke

Reputation: 53

I think there are three options.

  1. If font doesn't matter use android:typeface="monospace" in your textview and align through spaces
  2. If font does matter design it in html and use a WebView
  3. If you don't want to work with html, you need to make the layout through different TextView's which is imo always better.

Upvotes: 0

vokilam
vokilam

Reputation: 10313

If using a TextView is not essential you can use TableLayout:

<TableLayout 
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <TableRow>
        <TextView 
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:text="Title"
            android:layout_weight="1"/>

        <TextView 
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:text="Some Title"
            android:layout_weight="2"/>
    </TableRow>

    <TableRow>
        <TextView 
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:text="Date"
            android:layout_weight="1"/>

        <TextView 
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:text="Some Date"
            android:layout_weight="2"/>
    </TableRow>

</TableLayout>

Upvotes: 0

Related Questions