Skip to main content

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?



Source: Tips4all

Comments

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

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

    ReplyDelete
  3. 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?

    ReplyDelete
  4. 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.

    ReplyDelete

Post a Comment

Popular posts from this blog

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...

Is it possible to have IF statement in an Echo statement in PHP

Thanks in advance. I did look at the other questions/answers that were similar and didn't find exactly what I was looking for. I'm trying to do this, am I on the right path? echo " <div id='tabs-".$match."'> <textarea id='".$match."' name='".$match."'>". if ($COLUMN_NAME === $match) { echo $FIELD_WITH_COLUMN_NAME; } else { } ."</textarea> <script type='text/javascript'> CKEDITOR.replace( '".$match."' ); </script> </div>"; I am getting the following error message in the browser: Parse error: syntax error, unexpected T_IF Please let me know if this is the right way to go about nesting an IF statement inside an echo. Thank you.