androidandroid-layouttextviewtext-formattingjustify

How to add space between words of paragraph to remove space from end in Textview(i.e. Justify Text) in Android?


I am creating a UI for my application, where I need to show paragraph like this

enter image description here

And for doing this I have used Textview like this

<TextView
    android:id="@+id/description"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="@dimen/dp24"
    android:fontFamily="sans-serif-thin"
    android:text="@string/sample_description"
    android:textColor="@color/black"
    android:lineSpacingExtra="@dimen/dp4"
    />

But my output is like this

enter image description here

If you notice in my output at the end of some lines there is some space left. And I don't want it.

And if you saw in the 1st image there is no space left at the end. They added space between words to remove space from the end.

So, My question is how to achieve the 1st image output?


Solution

  • If you use Android 8+ you can obtain the justification using

    android:justificationMode="inter_word".
    

    As alternative you can use a WebView to place your text into in HTML format:

    <string name="sporttext">
    <![CDATA[
    <html>
     <head></head>
     <body style="text-align:justify;color:gray;background-color:white;">
      This sport shirt is crafted from scratch ...
     </body>
    </html>
    ]]>
    </string>