Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I fix Html.fromHtml link focus visibility problems (in ICS and Honeycomb)?

To get a TextView to display (and act friendly with) Html strings my code looks something like:

// itemHtml is a String of HTML defined above

TextView itemContent = (TextView) findViewById(R.id.itemContent);
itemContent.setText(Html.fromHtml(itemHtml));
itemContent.setMovementMethod(LinkMovementMethod.getInstance());

If the Html string has a link, TextView results in links that are clickable and focusable. When the user focuses on a specific link (e.g. by using the d-pad), the link text changes in some significant way to show that focus was obtained.

The problem is that when I test this same pattern using devices with a d-pad using Honeycomb (e.g. a Google TV) or Ice Cream Sandwich flavors of Android, the link in the text shows no noticeable indication that the link has focus.

I know it is getting focus, because when you then hit enter it does the specified action. You can even move around between various links in the text; you're just left guessing which link you're currently at, which results in a very bad user experience.

Is there something I'm doing wrong? Is there some way to fix this or work around this?

like image 495
yydl Avatar asked Oct 23 '11 21:10

yydl


People also ask

What can I use instead of fromHtml?

Html. fromHtml(text) is deprecated in Android N. Google has created HtmlCompat which can be used instead of the method below. Add this dependency implementation 'androidx.

What does HTML fromHtml do?

fromHtml. Returns displayable styled text from the provided HTML string. Any <img> tags in the HTML will display as a generic replacement image which your program can then go through and replace with real images.


2 Answers

Edit: After going a bit nuts, I finally thought I found a solution. However, this solution only works for Honeycomb. ICS is still not solved!

As of API 11, Android has a new setting on TextViews for defining whether the text is selectable.

You can set it using setTextIsSelectable(true) on a TextView, or define it in the XML layout android:textIsSelectable="true"

It's probably best to define it in XML, so that keeping the app backwards-compatible is trivial. Just make sure you're targeting version >= 11, or you'll probably get an error.

like image 132
yydl Avatar answered Oct 25 '22 14:10

yydl


The way HTML.fromHTML operates is by creating "spans" with varying effects throughout the various characters of the string. One workaround for this would be to use ClickableSpan coupled with another of the CharacterStyles to colorize the text as clickable. The previous span will allow you to register a callback, and this callback could be to broadcast an intent to view a url (which would open a browser).

like image 42
Blaskovicz Avatar answered Oct 25 '22 15:10

Blaskovicz