0

I have some text with a link in a TextView. Now I want that the browser open the link if the user click on it. My TextView looks like this:

<string name="Info">Go to <a href="www.google.com">Google</a></string>

<TextView
        android:id="@+id/Info"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="@dimen/OptionMarginBottom"
        android:autoLink="web"
        android:linksClickable="true"
        android:text="@string/Info" />

The Link is displayed correct in blue but I can not click on it. Why is that?

Cilenco
  • 6,951
  • 17
  • 72
  • 152

4 Answers4

2

use this method

public static void addLink(TextView textView, String patternToMatch,
        final String link) {
    Linkify.TransformFilter filter = new Linkify.TransformFilter() {
        @Override public String transformUrl(Matcher match, String url) {
            return link;
        }
    };
    Linkify.addLinks(textView, Pattern.compile(patternToMatch), null, null,
            filter);
}

and use as

addLink(text, "^Android", "http://abhiandroidinfo.blogspot.in");
abhi
  • 759
  • 6
  • 15
1

Use Linkify on your TextView

Linkify.addLinks(yourTextviewObject, Linkify.WEB_URLS);

Linkify take a piece of text and a regular expression and turns all of the regex matches in the text into clickable links.

Blackbelt
  • 156,034
  • 29
  • 297
  • 305
  • the textview contains a website ? Something like http://www.google.com? . If you want the html approach you have so set text inside your textview at runtime with yourTextviewObject.setText(Html.fromHtml(getString(R.string.info))); – Blackbelt Jun 18 '13 at 12:56
  • The TextView contains the string from above with text and a link `Go to Google` – Cilenco Jun 18 '13 at 13:16
  • then at runtime yourTextviewObject.setText(Html.fromHtml(getString(R.string.info))); and it should works – Blackbelt Jun 18 '13 at 13:16
  • No sorry does not work... In the onCreateView of my Fragment I call `TextView tv = (TextView) rootView.findViewById(R.id.Info); tv.setText(Html.fromHtml(getString(R.string.Icon))); Linkify.addLinks(tv, Linkify.WEB_URLS);` Do I have to set special values on my TextView? – Cilenco Jun 18 '13 at 13:29
  • why R.string.Icon ? the String is R.string.Info. You can rid of Linkify in this case – Blackbelt Jun 18 '13 at 13:30
0

Helper Method:

public static void addLinks(TextView textView, String linkThis, String toThis) {
        Pattern pattern = Pattern.compile(linkThis);
        String scheme = toThis;
        android.text.util.Linkify.addLinks(textView, pattern, scheme, new MatchFilter() {
            @Override
            public boolean acceptMatch(CharSequence s, int start, int end) {
                return true;
            }
        }, new TransformFilter() {

            @Override
            public String transformUrl(Matcher match, String url) {
                return "";
            }
        });
    }

Now use like Below:

String weblink = "WebsiteName";
String url = course.getString(TAG_Info);

TextView txtInfo= (TextView) findViewById(R.id.Info);
txtInfo.setText(userCanSeeThis);

addLinks(txtInfo, weblink, url);
Sagar Maiyad
  • 12,655
  • 9
  • 63
  • 99
0

Easier method is to make a string and add Link Text and in the xml file, make the textView:

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:autoLink="web"
    android:text="@string/string_with_link" />
Dtroll MC
  • 303
  • 5
  • 14