Home » Android » Justify Text in TextView Android

Justify Text in TextView Android

Posted by: admin June 15, 2020 Leave a comment

Questions:

How do you get textview to be justified ? is it possible to get the solution? I have searched most of the forums but i didn’t make it up. please help me out.

How to&Answers:

Android doesn’t support text justification, sorry. You can use android:gravity attribute to align text to left or right or center etc, but you can’t really justify it. The only option I guess is to use a WebView to show text and then use CSS to get the text justified.

Answer:

UPDATED

We have created a simple class for this. There are currently two methods to achieve what you are looking for. Both require NO WEBVIEW and SUPPORTS SPANNABLES.

LIBRARY: https://github.com/bluejamesbond/TextJustify-Android

SUPPORTS: Android 2.0 to 5.X

SETUP

// Please visit Github for latest setup instructions.

SCREENSHOT

Comparison.png

Answer:

<RelativeLayout android:id="@+id/RelativeLayout01"
    android:layout_height="wrap_content" android:layout_width="fill_parent">

    <TextView android:id="@+id/TextView2" android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:text="I'm TextView- Left" android:layout_alignParentLeft="true">
    </TextView>

    <TextView android:id="@+id/TextView1" android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:text="I'm TextView- Right" android:layout_alignParentRight="true">
    </TextView>


</RelativeLayout>

ScreenShot:

alt text

Hope this helps. If you have any difficulties write back.- Thanks