How to Justify text in Android.

Justified text in Android. Justify text in an Android app using a WebView.

Create A New Android Project

1. First create a new project in Android Studio (File => New Project)
2. Open activity_main xml and edit with below code.

activity_main.xml

									   
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="10dp">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:padding="10dp"
        android:text="Justify Text in android" />

    <WebView
        android:id="@+id/webView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

Open MainActivity.java class and add below code.

MainActivty.java

									   
package com.narendra.justiytext;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;

public class MainActivity extends AppCompatActivity {

    WebView webView;
    WebSettings webSettings;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        webView= (WebView) findViewById(R.id.webView);
        webSettings=webView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        String htmlText = " %s ";
        String myData = "<html><body style=\"text-align:justify\">The E-Learning Application is a Mobile based application.
            The main objective of this application is to make it interactive 
	    and its ease of use.The main features that the application provides are 
	    online tutorial and tests, once the registered people select their 
		interested subjects. 
This helps to establish incremental learning process. Users can also discuss an issue/topic by posting messages in the discussion forum. Along with this they can also take real time simulations of the most widely known competitive exams.</body></Html>"; webView.loadData(String.format(htmlText,myData),"text/html","utf-8"); } }

Output :


Tags

  • Android

  • Android Studio

  • Android development

  • Java