0

My goal here is to apply justify to the text through CSS in the HTML or some other way, while still allowing formatting like textColor, textFont, and so on..

There's an old thread with some good answers but it doesn't really fulfill all of the requirements above, particularly the allowance of formatting the text.

While this solution may still have some potential and I may just have overlooked something in the implementation. I've seen a similar question with an accepted answer but I'm not that confident on how it works as it's in Kotlin..

For context, I only need this to work for API level 19 and above.

Here's what I got so far:

MainActivity.java:

public class MainActivity extends AppCompatActivity {

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TextView justify = (TextView) findViewById(R.id.content_to_justify);
        justify.setText(HtmlCompat.fromHtml(getString(R.string.text_to_justify), 0));
...

activity_main.xml:

...
            <androidx.constraintlayout.widget.ConstraintLayout
                android:id="@+id/justify_container"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <TextView
                    android:id="@+id/content_to_justify"
                    android:layout_height="wrap_content"
                    android:fontFamily="@font/roboto_regular"
                    android:textColor="@color/grayxxxx"
                    android:textSize="14sp"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toBottomOf="parent" />

            <\androidx.constraintlayout.widget.ConstraintLayout>
...

strings.xml:

<resources>
...
    <string name="text_to_justify" translatable="false">
        <![CDATA[
            <html>
                <body style="text-align:justify;color:gray;background-color:black;">
                   Lorem ipsum..
                </body>
            </html>
        ]]>
    </string>
...
</resources>

Take note of the additional CSS stylings. I just added it there to see if it indeed works and not just an issue unique with text-align:justify. None of these worked.

I tried to cast CharSequence to HtmlCompat.fromHtml like so:

justify.setText((CharSequence) HtmlCompat.fromHtml(getString(R.string.text_to_justify), 0));

But there's no change.

Am I missing something or am I digging the wrong hole?

4
  • 1
    Andorid TextView doesn't support all types of html tags. Refer here link.I don't think <html><body></body></html> is supported. Try with <font size="..." color="..." ></font> and see if that works. For color gray use #d3d3d3. Commented Mar 8, 2021 at 9:39
  • @PadminiS Hello, my goal is to apply justify while maintaining formatting defined either from xml or from the html tag itself, I tried <p style="text-align:justify"> but sadly it didn't change anything.. Commented Mar 8, 2021 at 9:49
  • 1
    Then try webView, WebView view = (WebView) findViewById(R.id.webview); String text = "<html><body><p align=\"justify\">"; view.loadData(text, "text/html", "utf-8"); Commented Mar 8, 2021 at 11:02
  • @PadminiS WebView works now but it doesn't follow its attributes defined in activity_main.xml, so the solution would be to use HTML and inline CSS in the string text_to_justify to use my custom fonts and colors.. The problem rn is idk how to refer to the font file so it can be defined in the inline CSS.. Commented Mar 8, 2021 at 11:43

1 Answer 1

1

As android Textview doesn't support all html tags, Webview is a better solution as html and inline CSS styles can be applied. So try like below,

webView=findViewById(R.id.webview);

    String text= "<html>"
            + "<body>"
            + "<style type=\"text/css\">body{color: #d3d3d3; background- 
             color:#fffff;}"
            + "</style>"
            +"<p align=\"justify\">"
            + text_to_justify
            + "</p></body></html>";

    webView.loadData(text, "text/html", "utf-8");

Also WebView will not accept properties like textColor which are supported in TextView xml.

Sign up to request clarification or add additional context in comments.

1 Comment

I was averse to using WebView as it forces its parent ConstraintLayout to have its layout_height to match_parent.. this messes up for some reason the ConstraintLayout above it in a LinearLayout with layout_height and layout_width set to match_parent so it could occupy the whole screen.. for some reason this ConstraintLayout above it doesn't occupy the whole screen anymore.. Oh well..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.