Open top menu

How to display Justify text in android ?

In this blog explain how to justify text in android show below code
the blog used align & justify text in about us in android app .

Step 1 You can create activity_main.xml class inside res folder...

activity_main.xml. 

In this class create webview .
...................................................................................................................................................................

<LinearLayout 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"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/logoweb" />

    <WebView
        android:id="@+id/web_history"
        android:layout_width="215dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="30dp"
        android:background="#cfcfcf"
        android:paddingBottom="5dp"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:src="@drawable/logoweb" />

</LinearLayout>




Step 2. you can add html tag in string class inside res/string....
String.xml
..................................................................................................................................................................
<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">Test Code</string>
    <string name="hello_world">Hello world!</string>
    <string name="action_settings">Settings</string>
  
    <string name="Yellow">Yellow</string><string name="History">
<![CDATA[
<html>
 <head></head>
 <body style="text-align:justify;color:white;background-color:green;">
 Mohandas Karamchand Gandhi, more commonly known as ‘Mahatma’ (meaning ‘Great Soul’) was born in Porbandar, Gujarat, in North West India, on 2nd October 1869, into a Hindu Modh family. His father was the Chief Minister of Porbandar, and his mother’s religious devotion meant that his upbringing was infused with the Jain pacifist teachings of mutual tolerance, non-injury to living beings and vegetarianism
 </body>
</html>
]]>
    </string

</resources>


Setp 3. create MainActivity.java class inside src folder...


MainActivity.java

In this class load html text in webview from String class.
...................................................................................................................................................................

 package com.androidbeginner.testcode;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.webkit.WebView;

@SuppressLint("ResourceAsColor")
public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getActionBar().setTitle("Justify text ");
setContentView(R.layout.activity_main);
WebView view = (WebView) findViewById(R.id.web_history);
view.getSettings();
view.setBackgroundColor(Color.GREEN);
view.loadData(getString(R.string.History), "text/html", "utf-8");
}

}




0 comments