Link color declared in string.xml (Versions below N)

Asked

Viewed 71 times

0

I have the following text stated in xml string.:

<string name="TITLE_EXAMPLE"><![CDATA[
    <p>Para entrar em contato com nossa Central:</p>
    <p><b>Brasil: Capitais</b></p>
    <p> <a href="tel:+5541123123123" style="color:#E29800"> 5541123123123</a> </p>
    ]]>
</string>

How do I apply a certain color to ahref ?

Even with style is not working!

[EDIT]

At the suggestion of aldeir Psr :

TextView contact = findViewById(R.id.contact);

String html;
contact.setMovementMethod(LinkMovementMethod.getInstance());
    if (Build.VERSION.SDK_INT >= Build.VERSIONS_CODE.N) {
        html = Html.fromHtml(getString(R.string.codeHtml), FROM_HTML_OPTION_USE_CSS_COLORS);
    } else {
        html = Html.fromHtml(getString(R.string.codeHtml));
    }
    
    contact.setText(html);

Works only on versions of N upward!

I would like to apply in all versions!

1 answer

1

To use HTML in Textview or similar, you must use the class Html, android native.

This class has a method called fromHtml which serves precisely this function.

In the case of a Textview, you can use the following code:

TextView textView = findViewById(R.id.textViewId);

String html;

if (Build.VERSION.SDK_INT >= Build.VERSIONS_CODE.N) {
    html = Html.fromHtml(getString(R.string.codeHtml), FROM_HTML_OPTION_USE_CSS_COLORS);
} else {
    html = Html.fromHtml(getString(R.string.codeHtml));
}

textView.setText(html);
  • And for versions below the N? can be done tbm?

  • You can. Just use String html = Html.fromHtml(getString(R.string.codeHtml));

  • didn’t work in below the N

  • 1

    Check this lining with the font. https://stackoverflow.com/a/25011685

  • Valdeir Psr, it didn’t work either! So, I realized that he uses the colorAccent for this color, so I created a Theme with <item name="colorAccent">@color/green</item> and applied to Textview, temporarily suits me, but I will research more about it! Thank you!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.