Android Linkify text - Spannable Text in Single Text View - As like Twitter tweet

22,380

Solution 1

I have used the same thing which I answered here in This Question

For that I have used this LinkEnabledTextView and that become easy for me to do the task.

Solution 2

What you can do is create a Custom ClickableSpan as below,

class MyCustomSpannable extends ClickableSpan
{
    String Url;
    public MyCustomSpannable(String Url) {
        this.Url = Url;
    }
    @Override
    public void updateDrawState(TextPaint ds) {
            // Customize your Text Look if required
        ds.setColor(Color.YELLOW);
        ds.setFakeBoldText(true);
        ds.setStrikeThruText(true);
        ds.setTypeface(Typeface.SERIF);
        ds.setUnderlineText(true);
        ds.setShadowLayer(10, 1, 1, Color.WHITE);
        ds.setTextSize(15);
    }
    @Override
    public void onClick(View widget) {
    }
    public String getUrl() {
        return Url;
    }
}

And the use its onClick() for opening an Activity or URL. I am adding a demo for loading a URL in WebView.

String text = "http://www.google.co.in/";
SpannableStringBuilder stringBuilder = new SpannableStringBuilder(text);
MyCustomSpannable customSpannable = new MyCustomSpannable(
                                               "http://www.google.co.in/"){

            @Override
            public void onClick(View widget) {
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setData(Uri.parse(customSpannable.getUrl()));
                startActivity(intent);
            }
        };
        stringBuilder.setSpan(customSpannable, 0, text.length(),
                                         Spannable.SPAN_INCLUSIVE_INCLUSIVE);

        textView.setText( stringBuilder, BufferType.SPANNABLE );
        textView.setMovementMethod(LinkMovementMethod.getInstance());

Solution 3

Generate your TextView in the following manner:

TextView t3 = (TextView) findViewById(R.id.text3);
t3.setText(Html.fromHtml("This is the Simple Text with 
    <a href=\'http://www.google.com\'>Keyword</a> and the 
    <a href='startActivityFromLink://some_info'>Link</a> to browse"));
t3.setMovementMethod(LinkMovementMethod.getInstance());

Specify your activity the following way:

<activity android:name=".TestActivity"
      android:label="@string/app_name">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
    <intent-filter>
        <data android:scheme="startActivityFromLink" />
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.BROWSABLE" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</activity>

I think it should work. Might require some tweaking. I did not get the time to test it. Let me know if it works.

Share:
22,380
MKJParekh
Author by

MKJParekh

I am a professional Android Developer, Currently working for a company in India. Having experience of working in the field since January 2011. In StackOverflow, I am active in android. I have completed my Masters as well as Bachelors in Computer Applications. Recently started blogging here : http://mayurkosh.blogspot.in/ [Not Technical !!] Twitter : #MayurKJParekh [Still Confuse, What to Tweet !!] I am from "રંગીલું રાજકોટ" - ભાઈ ભાઈ Personally I love the concept of Question/Answer. More details will be available soon. "Be Nice or Stay Away"

Updated on July 09, 2022

Comments

  • MKJParekh
    MKJParekh almost 2 years

    I have a textView and text like

    "This is the Simple Text with KeyWord and the Link to browse"

    in the above text i want to make..

    clicking on the Link goes to open that URL AND clicking on that KeyWord open a new Activity in my application

    also, There is an click event for the Whole TextView even.

    Please help me finding a solution. Thank You, MKJParekh

  • MKJParekh
    MKJParekh over 12 years
    yes i know i can do that way...but need to take only one text view
  • MKJParekh
    MKJParekh about 12 years
    Thanks for the answer,It helped me a lot.