24

Is there any way I can launch an activity from a portion of a string.

eg I have this in my strings.xml file:

<string name="clickable_string">This is a <u>clickable string</u></string>

I would like the text between the u tags to be underlined and launch an activity when clicked when inserted in to a TextView

Martyn
  • 16,432
  • 24
  • 71
  • 104

3 Answers3

45

Try this,

final Context context = ... // whereever your context is
CharSequence sequence = Html.fromSource(context.getString(R.string.clickable_string));
SpannableStringBuilder strBuilder = new SpannableStringBuilder(sequence);
UnderlineSpan[] underlines = strBuilder.getSpans(UnderlineSpan.class);
for(UnderlineSpan span : underlines) {
   int start = strBuilder.getSpanStart(span);
   int end = strBuilder.getSpanEnd(span);
   int flags = strBuilder.getSpanFlags(span);
   ClickableSpan myActivityLauncher = new ClickableSpan() {
     public void onClick(View view) {
       context.startActivity(getIntentForActivityToStart());
     }
   };

   strBuilder.setSpan(myActivityLauncher, start, end, flags);
}

TextView textView = ...
textView.setText(strBuilder);
textView.setMovementMethod(LinkMovementMethod.getInstance());

Basically you have to attach a Span object to the range of characters you want to be clickable. Since you are using HTML anyways, you can use the underline spans placed by the Html.fromSource() as markers for your own spans.

Alternatively you could also define a Tag within the string that only you know of. i.e. <activity> And supply your own tag handler to the Html.fromSource() method. This way your TagHandler instance could do something like, surround the tagged text with a specific color, underline, bold and make it clickable. However I would only recommend the TagHandler approach if you find yourself writing this type of code a lot.

Greg Giacovelli
  • 10,164
  • 2
  • 47
  • 64
  • 1
    This is *awesome*. One thing though, if i fetched the string using getString it didn't work :(. I just hardcoded the string in the code for now.. yay. Also if you want to set the link color to white: textView.setLinkTextColor(android.R.color.white); – tomwilson Aug 17 '11 at 07:36
  • Hi Kabal, it definitely does work regardless of using getString() or getText(). By using Html.fromSource(String) it will create a spannable with the html mark up turned into spans for android's text renderer to handle. Pop up a follow up question if there is a specific issue you think you may be dealing with. – Greg Giacovelli Aug 18 '11 at 04:32
  • hello it awesome! how can i get the string clicked ? i have more than string clickable (underlined) by textView? any help? – Angie Apr 03 '12 at 17:03
  • You can create a ClickableSpan and associate it the characters within the string you want clickable. Similar to the UnderlineSpan solution above. – Greg Giacovelli Apr 03 '12 at 20:41
2

Answered here Make parts of textview clickable (not url) I just made a modification if you want to use it with a HTML Message do the following In your Display function

public void displayText(String message) {

            chapterTextView.setText(Html.fromHtml(message),TextView.BufferType.SPANNABLE);
            chapterTextView.setMovementMethod(LinkMovementMethod.getInstance());
            Spannable clickableMessage = (Spannable) chapterTextView.getText();
            chapterTextView.setText(addClickablePart(clickableMessage), BufferType.SPANNABLE);
}

The Modified function of addClickablePart

private SpannableStringBuilder  addClickablePart(Spannable charSequence) {
        SpannableStringBuilder  ssb = new SpannableStringBuilder(charSequence);

        int idx1 = charSequence.toString().indexOf("(");
        int idx2 = 0;
        while (idx1 != -1) {
            idx2 = charSequence.toString().indexOf(")", idx1) + 1;

            final String clickString = charSequence.toString().substring(idx1, idx2);
            ssb.setSpan(new ClickableSpan() {

                @Override
                public void onClick(View widget) {
                    Toast.makeText(getActivity(), clickString,
                            Toast.LENGTH_SHORT).show();
                }
            }, idx1, idx2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            idx1 = charSequence.toString().indexOf("(", idx2);
        }

        return ssb;
    }

Hope this help someone.

Community
  • 1
  • 1
Bahaa Hany
  • 744
  • 13
  • 22
2

assign this string to one of your xml layout and then in your code get the id of TextView and then implement OnClickListener for this Textview,inside of it you can start your new activity you want.

Senthil Mg
  • 3,301
  • 4
  • 32
  • 49
  • the problem is that I only want the text inside the tags to be clickable. I've simplified the issue a bit but this is a requirement. Basically I want it to act like a HTML link tag but instead of a webpage, it launches an activity. – Martyn Oct 26 '10 at 17:17