Hacking by Walkingice: Android Clickable Span in TextView

2012/04/09

Android Clickable Span in TextView

Note: You could find chinese version in my chinese blog.


Spannable brings lots of possibility to TextView, includes displaying various appearance of a Text and onClick callbak.

The normal way to make a TextView clickable is to use View.setOnClickListener. It sets a callback listener to the whole view. What if we just want to add callback to a substring of a text?


This pictures demonstrate

  • One TextView
  • The sub-string has variant color
  • The sub-string is clickable


Of course we could use ViewGroup + many TextView to accomplish that, spannable make things eaiser.(maybe....)

We can understand Span in this way: Put extra function to a CharSequence. For example, If I have string 'TheFinalAnswerIs42', I can set Red color to "Final" and make 42 clickable. But the TextView does not know so much things, it only focus on showing the whole string.



We know that TextView only needs CharSequence as its content, and both of Spannable and Spanned are sub-interface of CharSequence. The question becomes 'How to build desired CharSequence'

Android API Documentations tells us that we can use SpannableStringBuilder to generate SpannableString.

Spannable span = (new SpannableStringBuilder()).newSpannable("TheFinalAnswerIs42");
span.setSpan(new ForegroundColorSpan(0xFFFF0000), 3, 5,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

So the string "Final" becomes red.And we are going to make '42' clickable.

span.setSpan(new MyClickableSpan(), 16, text.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
mTextView.setText(span);
mTextView.setMovementMethod(LinkMovementMethod.getInstance());


MyClickableSpan is to be a implementation of ClickableSpan. All we have to do is override method onClick.

Further question: why setSpan is using primitive class Object but not some interface such as Spanned?


API documentation also tells you that you can bind any your own object as Span to TextView. But how to use my customized span object? To solve this, we should understand how the TextView use ClickableSpan.

The source code of TextView tells us that it handle ClickableSpan in onTouchEvent.

In other words, to handle our customzied span object, we should extends TextView to use CustomizedTextView to accomplish it.

36 comments:

  1. the thing i want to ask is that,my app has a list view and i want to linkify some texts along with weblink too.For this particular purpose this idea is not working,since the holder does not recognizing the click able text. So please tell me another way out

    ReplyDelete
    Replies
    1. I don't understand your question exactly. 'Click a text and open a web link' is feasible. If you put the TextView into a ListView, you should consider that is the event pass to TextView correctly.

      Delete
  2. span click event,see below
    http://stackoverflow.com/a/16182500/596555

    ReplyDelete
  3. This is in fact a very nice sketch and even from the look at it, one is able to learn a lot concerning the details of the blog. I believe that the people who have read this content have been sufficiently enlightened. Through learning such hacking techniques we are to develop more secure applications. Statistics Homework Help

    ReplyDelete
  4. Interesting article to read.. I learnt something new information from this article.. thank you for sharing

    hadoop training | big data training

    ReplyDelete
  5. Can you recommend any other blogs/websites/forums that deal with the same subjects? Thanks.
    nebosh igc courses in chennai

    ReplyDelete
  6. This comment has been removed by the author.

    ReplyDelete
  7. Thanks for sharing this very informative blog for us. Who are interested to learn Digital marketing can join Litementors. Which is the No.1 Digital Marketing Training in Chennai.

    ReplyDelete
  8. Wow it is really wonderful and awesome thus it is veWow it is really wonderful and awesome thus it is very much useful for me to understand many concepts and helped me a lot. it is really explainable very well and i got more information from your site.ry much useful for me to understand many concepts and helped me a lot. it is really explainable very well and i got more information from your site.
    Hadoop Training in Bangalore

    ReplyDelete
  9. Thanks For sharing a nice post about Informatica Training Course.It is very helpful and Informatica useful for us.Informatica Training in Bangalore

    ReplyDelete
  10. Excellent information with unique content and it is very useful to know about the Oracle Apps HRMS.oracle apps hrms training in bangalore

    ReplyDelete
  11. Thank you for sharing such a nice post!

    Get Business Objects Training in Bangalore from Real Time Industry Experts with 100% Placement Assistance in MNC Companies. Book your Free Demo with Softgen Infotech.

    ReplyDelete
  12. Thanks for the information...
    Best SAP HANA Training in Bangalore - BTM Layout | SAP HANA Training Institutes | SAP HANA Course Content - Tecmax
    - Tecmax offers the Best SAP HANA Training in Bangalore - BTM Layout, We offer Real-Time Training with Live Projects, Our SAP HANA Trainers are Working Professionals with 8+ years of Expertise in SAP HANA, we also provide placement assistance.

    ReplyDelete
  13. This comment has been removed by the author.

    ReplyDelete
  14. Just found your post by searching on the Google, I am Impressed and Learned Lot of new thing from your post.
    Business Objects training in bangalore
    Business Objects institutes in bangalore

    ReplyDelete
  15. This site was... how would I say it? Applicable!! At long last I have discovered something that encouraged me. Good wishes! news updates

    ReplyDelete
  16. It s a very useful page. Thank you. 17f1b51b1b03d68b05efdc3405be47df
    sariyer
    tekman
    akcadag
    zara
    buyukcekmece
    omerli
    guzelyurt
    ardanuc
    siverek

    ReplyDelete
  17. Congratulations on your article, it was very helpful and successful. 96cf20e8cb5a1cf89bee1528bf307717
    website kurma
    website kurma
    sms onay

    ReplyDelete
  18. Thank you for your explanation, very good content. 2412172ee3910482daba55b26dd2b415
    altın dedektörü

    ReplyDelete
  19. Thanks for adding it to my knowledge. You are wonderful writer. Your articles are very helpful and easy to understand.

    TOEFL Coaching in Vadodara

    ReplyDelete
  20. Cool and I have a tremendous supply: Who Repair House Windows split level renovations

    ReplyDelete