How to Justify Text with Image in Android.

Justify text alignment with image in Android

Create New Android Project

Add Internet permission in AndroidManifest.xml file.

AndroidManifest.xml

									   
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.narendra.justiytext">

    <uses-permission android:name="android.permission.INTERNET"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        
		<activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Open activity_main.xml new Project.

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\">
        <img align=\"right\"src=\"https://image.flaticon.com/teams/new/1-freepik.jpg\" 
	    height=\"100px\" width=\"90px\">
        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.<br/>
        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