0

String in strings.xml

<string name="lblAuthor1">Icon made by  <a href="https://www.flaticon.com/authors/smashicons">Smashicons</a> from
         <a href="https://www.flaticon.com">www.flaticon.com</a></string>

In Activity

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

lblAuthor1.setText(getText(R.string.lblAuthor1));
lblAuthor1.setMovementMethod(LinkMovementMethod.getInstance());

In this answer i read that

I should only use <b>, <i> and <u> as they are listed in the documentation.

But for some reason, the a href tag works in my app, so i was wondering if this could cause any issues because the docs don't include that a href is supported when using it in strings.xml

Niraj
  • 903
  • 8
  • 23
Vince VD
  • 1,506
  • 17
  • 38

2 Answers2

0

Method 1

change your code like this

   if (Build.VERSION.SDK_INT >= 24) {
        lblAuthor1.setText(Html.fromHtml(getText(R.string.lblAuthor1), Html.FROM_HTML_MODE_LEGACY));
    } else {
        lblAuthor1.setText(Html.fromHtml(getText(R.string.lblAuthor1)));
    }

instead of

lblAuthor1.setText(getText(R.string.lblAuthor1));

Method 2

Follow this answer

Niraj
  • 903
  • 8
  • 23
Arunachalam k
  • 734
  • 1
  • 7
  • 20
  • Any idea why my code works even though docs say it's not supported? – Vince VD Dec 24 '19 at 03:14
  • as per documentation If you aren't applying to format, you can set TextView text directly by calling setText(java.lang.CharSequence). In some cases, however, you may want to create a styled text resource that is also used as a format string. Normally, this doesn't work because the format(String, Object...) and getString(int, Object...) methods strip all the style information from the string. The workaround to this is to write the HTML tags with escaped entities, which are then recovered with fromHtml(String), – Arunachalam k Dec 24 '19 at 03:27
  • try to add , like this html tag to your string this won't work without using Html.fromHtml(getText(R.string.lblAuthor1), Html.FROM_HTML_MODE_LEGACY) – Arunachalam k Dec 24 '19 at 03:31
  • And if i only want to use the href tag for links? – Vince VD Dec 24 '19 at 04:49
  • And thats false, does work without using html.fromHtml as you can read in the answer in my post – Vince VD Dec 24 '19 at 04:50
  • I'm just confused why , and are supported – Vince VD Dec 24 '19 at 04:51
  • did u added this attributes in your layout android:autoLink="web" android:linksClickable="true" – Arunachalam k Dec 24 '19 at 14:23
  • yes but even when i remove them and still just use getText it works without using html.fromhtml – Vince VD Dec 26 '19 at 01:12
0

android:autoLink="web";

set this on your TextView in the layout xml.

or

replace '<' symbol with &lt ; and try