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

4

14

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?

Uptotheminute answered 23/10, 2011 at 21:53 Comment(0)
U
2

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.

Uptotheminute answered 31/10, 2011 at 21:52 Comment(0)
F
1

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).

Frazzle answered 24/10, 2011 at 12:34 Comment(1)
I think you misunderstood. As I mentioned, the action (ie opening the URL) is invoked. The problem is there is no visual representation that the link is selected.Uptotheminute
C
1

The text colour state lists for Honeycomb+ might not set the focused state to a different colour, or you override the colour to be constant.

Check the colors + styles in your_android_sdk_directory/android-14/data/res/

Setting the text to android:autoLink="web" might also help?

Clover answered 28/10, 2011 at 14:13 Comment(0)
T
1

The best way to do that is to add CSS styling to your html. I know Android supports :hover selector. So you might right something like this:

String myLink = "<a href=\"http:\/\/google.com\">your link</a>"
Html.fromHtml(myLink);

and find a way to include CSS data to it: (I'm not sure how but I think it's possible)

a :hover {
   color: red;
}

UPDATE:

I think the answer of your question is there.

Tarkington answered 31/10, 2011 at 14:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.