1

I have to display some text in textview. I just want the a part of the text to be clickable and the rest of the text to be normal.

Here is my code:

TextView mTextView = (TextView) findViewById(R.id.txt);
String mText = "some text.Link to click.Some more text";
SpannableString sb = new SpannableString(mText);
sb.setSpan(new ClickableSpan() {
    @Override
    public void onClick(View widget) {
        Intent in=new Intent(Intent.ACTION_VIEW,Uri.parse("http://www.twitter.com/"));
        startActivity(in);
    }
},  mText.indexOf("Link"),mText.indexOf("Link") + 13,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

mTextView.setText(sb);
mTextView.setMovementMethod(LinkMovementMethod.getInstance());
O__O
  • 950
  • 1
  • 9
  • 17
  • I would just do an href and parse the whole string for the TextView as html. Text looks normal besides your link, that will appear blue. – A--C Dec 02 '12 at 02:22

2 Answers2

3

If you are putting the full url into the TextView you can use android:autoLink="web" to greatly simplify this process.

See the TextView docs for more

Another option is the Html object. Specifically the fromHtml() method I think will allow you to achieve what you want. You should be able to do something like this:

mTextView.setText(Html.fromHtml("blah blah <a href=\"http://some.site.com\">some text to be linkified</a> blah blah"));
FoamyGuy
  • 46,603
  • 18
  • 125
  • 156
  • 1
    It might not be a url only. might be some text that I want to click and launch a third party app. – O__O Dec 02 '12 at 02:43
  • FoamyGuy has the right answer (but is not an answer,so I can't mark as correct). I think the fromHtml() will be the most seamless. – Booger Dec 02 '12 at 02:57
  • @Booger you can't mark it as correct anyhow, it's not your question =p. I'll edit my answer though to add it. – FoamyGuy Dec 02 '12 at 03:00
  • @Booger/FoamGuy What if its not an url only..please check my earlier comment. It might be a normal text also. – O__O Dec 02 '12 at 03:09
  • @androiddeveloper see the second part of my answer. I edited to add a method that should work for that case too. But also if you aren't using a url I suggest you edit your question since that is how you stated it. – FoamyGuy Dec 02 '12 at 03:11
  • @FoamGuy Edited my question. Your solution Works fine..But would be great if I would not have to create a separate textview because i get the text as a response from server and would have to display the entire thing on textview with only a part clickable – O__O Dec 02 '12 at 03:19
0

You could also use the linkify option: http://android-developers.blogspot.com/2008/03/linkify-your-text.html - In this case, you can just build your string dynamically, and make only the part of it you want clickable.

I had never heard of the autoLink attribute - cool.

Another option, which might be closer to your original question - you could create two different TextViews next to each other (if the spacing is odd, use layout_marginRight="-8" - using a negative offset, which may require a bit of tweaking to get the spacing perfect). Then you could apply the autoLink attribute to only the TextView you want to be clickable.

Booger
  • 18,579
  • 7
  • 55
  • 72