How to justify the text in a Textview?

Asked

Viewed 8,611 times

7

It is possible to justify (align) the text of a TextView? Besides justifying, I want to apply other types of formatting.

  • depending on your specific need, take a look at this question: http://stackoverflow.com/questions/1292575/android-textview-justify-text

2 answers

5


The TextView does not support this type of alignment.

You can make an application based on HTML instead of TextView, as suggested by this link:

main java.:

package cz.seal.webview;

import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;

public class Main extends Activity
{
    WebView mWebView;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mWebView = (WebView) findViewById(R.id.webview);    

        String text = "<html><body>"
               + "<p align=\"justify\">"                
               + getString(R.string.lorem_ipsum) 
               + "</p> "
               + "</body></html>";

        mWebView.loadData(text, "text/html", "utf-8");
    }
}

main.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <WebView
        android:id="@+id/webview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />
</RelativeLayout>

If the accents don’t work, you can try changing utf-8 to iso-8859-1, like this:

mWebView.loadData(text, "text/html", "iso-8859-1");
  • in my code it worked, however, could not identify the accents, ie the enconding utf-8...

  • @Everton I think you have to change the mWebView.loadData(text, "text/html", "utf-8"); for mWebView.loadData(text, "text/html", "iso-8859-1");

  • @Guilhermenascimento’s statement in the opening sentence is incorrect. The TextView supports yes, right-to-left and centered alignment. It is best to change otherwise staff may confuse.

  • @Acklay was a lost word, I meant does not support this kind of alignment.

4

@Guilherme’s reply is correct, but in my case, for android identify the enconding used...

mWebView.loadData(text,"text/html;charset=UTF-8",null);

Browser other questions tagged

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