How to justify text in a TextView?

7

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

    
asked by anonymous 21.04.2015 / 04:42

2 answers

5

TextView does not support this type of alignment.

You can make an HTML-based application 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 accents do not work, you can try to trade utf-8 with iso-8859-1, like this:

mWebView.loadData(text, "text/html", "iso-8859-1");
    
21.04.2015 / 05:22
4

The answer from @Guilherme is correct, but in my case, for android to identify the enconding I used ...

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