6

I have a text view. In that I have text like this "You may visit this: http://www.google.com". After clicking this link it should open it in a webview instead of default browser. My code is:

//MainActivity.java

link = (TextView) findViewById(R.id.textView);
link.setText("You may visit here : http://www.google.com");
link.setMovementMethod(LinkMovementMethod.getInstance());

//Manifest

<activity
            android:name="com.xpointers.xpmediaapp.mediaapp.WebViewActivity"
            android:label="@string/app_name">
            <intent-filter>

                <category android:name="android.intent.category.DEFAULT" />
                <action android:name="android.intent.action.VIEW" />

            </intent-filter>
        </activity>

I tried using this link: handle textview link click in my android app but failed to understand and what should I write in WebviewActivity?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Ak10147
  • 103
  • 1
  • 7

3 Answers3

9

The following problems need to be solved:

Linkify the TextView Find a way to listen to a click on a link in the TextView Get the url of the clicked link and load it in the WebView Optional: make the TextView clickable without losing the ability to select text Optional: handle formatted text in the TextView (different text sizes and styles)

1 Linkify the TextView

String text = "These are some sample links:\nwww.google.com\nwww.facebook.com\nwww.yahoo.com";
Spannable spannable = new SpannableString( Html.fromHtml(text) );
Linkify.addLinks(spannable, Linkify.WEB_URLS);

2 + #3 Listen to clicks on links and open them in the WebView

URLSpan[] spans = spannable.getSpans(0, spannable.length(), URLSpan.class);
for (URLSpan urlSpan : spans) {
    LinkSpan linkSpan = new LinkSpan(urlSpan.getURL());
    int spanStart = spannable.getSpanStart(urlSpan);
    int spanEnd = spannable.getSpanEnd(urlSpan);
    spannable.setSpan(linkSpan, spanStart, spanEnd, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    spannable.removeSpan(urlSpan);
}

For opening in new activity:

 private class LinkSpan extends URLSpan {
        private LinkSpan(String url) {
            super(url);
        }

        @Override
        public void onClick(View view) {
            String url = getURL();
            if (url != null) {


                startActivity(new Intent(LinkTestActivity.this,WebViewActivity.class).putExtra("url",url));

            }
        }
    }

And loding the url in webview.

for more see below link :-

Open URL in WebView instead of default Browser

Community
  • 1
  • 1
duggu
  • 37,851
  • 12
  • 116
  • 113
  • 1
    When I try to settext with the above resulting string like textview.setText(text); it shows me the plain text, without even provision to click it. How else do I need to setText? – Narendra Singh Nov 29 '15 at 11:47
  • 1
    @King, use `setText(Html.fromHtml(s))`, where s should containg a text like "see here: <a href="http://example.com/">example</a>" – CoolMind Oct 28 '16 at 11:42
  • 7
    @Duggu onClick is never called.. When i click on link, it opens in default browser. – Vishal Puri Nov 22 '16 at 17:16
  • For this solution to work, we need to disable autolinking (android:autoLink view tag shouldn't be set to "web") and also set movement method on the textview programatically: textView.setMovementMethod(LinkMovementMethod.getInstance()); – Aurintas Nov 20 '19 at 11:33
1

As of the link you posted, you should declare in Manifest file that your certain activity can open links. When such link is clicked, your activity is opened, therefore you should set launch mode to singleTop. Then you can receive new intents and display them in your webview. onNewIntent: http://developer.android.com/reference/android/app/Activity.html#onNewIntent(android.content.Intent)

This example is about implementing search, so you can skip to the "singleTop" part: http://developer.android.com/guide/topics/search/search-dialog.html

Marius
  • 810
  • 7
  • 20
0

Place this code after link.setText(--url--);

((TextView) findViewById(R.id.textView)).setOnClickListener(
    new OnClickListener() {
        @Override
        public void onClick(View v) {
            String url = ((TextView) findViewById(R.id.account_title))
                .getText().toString();
            WebView webView = (WebView) findViewById(R.id.wv);
            webView.getSettings().setJavaScriptEnabled(true);
            webView.loadUrl(url);
        }
    }
);

Create a webview in your relevant xml file, like this:

<WebView android:id="@+id/wv"
         android:layout_width="match_parent"
         android:layout_height="match_parent" />

That's it... Let me know if you are still getting some issue.

William Price
  • 4,033
  • 1
  • 35
  • 54
Ahsanwarsi
  • 1,013
  • 9
  • 18
  • bro i want to open it in new activity. – Ak10147 Jun 16 '14 at 06:12
  • @Ak10147 No issue, you can use same script for new activity, Just create a new JAVA class and put java code in onCreate() method then create a new .XML file and place above xml code in your parent layout. – Ahsanwarsi Jul 01 '14 at 11:45