Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 639
Shivam Jamaiwar Avatar asked Oct 18 '25 07:10

Shivam Jamaiwar


2 Answers

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>
like image 195
Graziano Avatar answered Oct 19 '25 22:10

Graziano


The thing you need is called justified content. You can achieve this using

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:justificationMode="inter_word" />
like image 32
Muhammad Subhan Avatar answered Oct 19 '25 21:10

Muhammad Subhan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!