Paresh Mayani is a founder of TechnoTalkative, Manager of GDG Ahmedabad (Google Developer group), JavaCodeGeek, a DZone MVB, Speaker in various Android events, Author, passionate Blogger and a Software Engineer by profession.He is currently working as a Software Engineer (Android, WP7, BlackBerry) in a company located at Ahmedabad, India. He has a very good skill over Android, Java. He is active in supporting the Android developer community, from answering questions onStackOverflow to publishing articles with possible sample code. Currently He is the holder of 20000 reputation on StackOverflow. Paresh is a DZone MVB and is not an employee of DZone and has posted 10 posts at DZone. You can read more from them at their website. View Full User Profile

Android – How to Display Information With Justify Alignment?

01.28.2013
| 1844 views |
  • submit to reddit

Problem: How do i display information with Justify alignment?
Solution:

I saw many developers asking questions on Stackoverflow for this problems. Here are some:

1. Android TextView Justify Text
2. How to justify text on a TextView made easy- Android
3. Justify Text in TextView Android

We can display justified information in WebView, as such there is no any property/attribute for justify alignment in WebView widget but we can use a simple trick. The Trick is to define html tags with justify alignment as below:

<html><body style="text-align:justify"> YOUR DATA </body></Html>

Android justify text

FYI, the same justify trick will not work for TextView. Mark Murphy (@commonsware) has already posted answer for TextView: “I do not believe Android supports full justification.”

MainActivity.java

package com.technotalkative.justifytext;
 
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.webkit.WebView;
 
public class MainActivity extends Activity {
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        String htmlText = " %s ";
        String myData = "Hello World! This tutorial is to show demo of displaying text with justify alignment in WebView.";
 
        WebView webView = (WebView) findViewById(R.id.webView1);
        webView.loadData(String.format(htmlText, myData), "text/html", "utf-8");
    }
}


activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >
 
    <WebView
        android:id="@+id/webView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
 
</RelativeLayout>


Download this example from here: Android – JustifyText



 

Published at DZone with permission of Paresh Mayani, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)