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?
<html><body></body></html>is supported. Try with<font size="..." color="..." ></font>and see if that works. For color gray use#d3d3d3.<p style="text-align:justify">but sadly it didn't change anything..WebView view = (WebView) findViewById(R.id.webview); String text = "<html><body><p align=\"justify\">"; view.loadData(text, "text/html", "utf-8");WebViewworks now but it doesn't follow its attributes defined inactivity_main.xml, so the solution would be to use HTML and inline CSS in the stringtext_to_justifyto 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..