Question

In my Android app I've got text views with links. Each of them is toggling a particular event (opening browser, starting another activity, etc...). To handle that, I haven't found any better solution than doing this:

hi there!, this is <u>a action</u> and this is <b>another action</b>. This third one goes to <a href="http://google.com>Google</a>

Then, when displaying the textview, I convert each link to a custom spannable with a SpannableStringBuilder.

I know this is hacky. And this is why I want to know if there's any other better way?

I've got two ideas:

  • Is it possible to implement custom spannables with their own marshalling system? For example a custom spannable matching tags like:

    <div class="foo"></div>

Then I'd have a FooClickableSpannable extending ClickableSpan automatically detected with fromHtml()

  • I could use custom url schemes to open my activities but I'm not sure how to force it not to display an app chooser if there are other alternatives.
Was it helpful?

Solution

Is it possible to implement custom spannables with their own marshalling system?

Can you create custom subclasses of CharacterStyle, particularly ClickableSpan? Yes.

I have no idea what "their own marshalling system" means, though.

I could use custom url schemes to open my activities but I'm not sure how to force it not to display an app chooser if there are other alternatives.

Well, a true custom scheme (e.g., piel:// instead of http:// or content://) is unlikely to present a chooser, since it is unlikely that there will be another alternative.

Beyond that, what I have done in the past is:

  • Use Html.fromHtml() for a basic conversion
  • Use getSpans() on the results to find all the URLSpan objects
  • replaced those URLSpan objects with other sorts of custom spans where needed via removeSpan() and setSpan()

Another approach would be to create a workalike for Html.fromHtml(). This is something that's rattling around the lower levels of my own TODO list, to create something that is more extensible, handles more HTML constructs, etc.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top