4

How to control textView onclicklistener with autolink web setting or in other words,intercept autolink web OnClick event?

For example,String text="Lucy is very nice.Here is her link.https://www.google.com";textview.setText(text); when clicking "https://www.google.com",I can catch it and jump to my app activity not to web browser. Textview has a property “autolink”.I set autolink as web.android:autoLink="web" So,android system can automatically detect the url.When clicking the url,it will jump to the browser.Now when clicking, I do not want jump to the brower,I just want to jump to my app activity and stay in app.

flynn
  • 159
  • 1
  • 1
  • 12

4 Answers4

5

Thanks you for all of your answers.Now I find my question's answer.There are two steps.

1.you need set Textview property. android:autoLink="web"

  <TextView  
    android:id="@+id/text_view"  
    android:layout_width="fill_parent"  
    android:layout_height="wrap_content"  
    android:autoLink="web"  
    android:text="Lucy is very nice.Here is her link.https://www.google.com" />

2.override URL onclick.There is an example.

public class MainActivity extends Activity {

TextView tv;  

@Override  
protected void onCreate(Bundle savedInstanceState) {  
    super.onCreate(savedInstanceState);  
    setContentView(R.layout.activity_main);  
    tv = (TextView) findViewById(R.id.tv);  
    CharSequence text = tv.getText();  
    if (text instanceof Spannable) {  
        int end = text.length();  
        Spannable sp = (Spannable) text;  
        URLSpan urls[] = sp.getSpans(0, end, URLSpan.class);  
        SpannableStringBuilder style = new SpannableStringBuilder(text);  
        style.clearSpans();  
        for (URLSpan urlSpan : urls) {  
            MyURLSpan myURLSpan = new MyURLSpan(urlSpan.getURL());  
            style.setSpan(myURLSpan, sp.getSpanStart(urlSpan),  
                    sp.getSpanEnd(urlSpan),  
                    Spannable.SPAN_EXCLUSIVE_INCLUSIVE);  

        }  
        tv.setText(style);  
    }  
}  

private class MyURLSpan extends ClickableSpan {  

    private String url;  

    public MyURLSpan(String url) {  
        this.url = url;  
    }  

    @Override  
    public void onClick(View arg0) {  
        Toast.makeText(MainActivity.this, url, Toast.LENGTH_LONG).show(); 
    }  

}  

}

3.The above code perfectly solved my problem.So when I click www.google.com in the Textview,the url will show out and jump to a specific activity.

flynn
  • 159
  • 1
  • 1
  • 12
0

As your question is not clear I am giving you answer that might help. Create another activity i which you want to show the link :

   WebView wv;
@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_web_search);
    Bundle bundle = getIntent().getExtras();
    String url = bundle.getString("message");
    wv=(WebView)findViewById(R.id.left_webview);

    getActionBar().setHomeButtonEnabled(true);

    //wv.getSettings().setJavaScriptEnabled(true);
    wv.getSettings().setSupportMultipleWindows(true);
    wv.loadUrl(url);

    wv.setWebViewClient(new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            System.out.println("URL :: " + url);
            view.loadUrl(url);
            return true;
        }
    });

}

in the textView activity :

Intent i = new Intent(TextViewActivity.this,NextActivity.class);
            i.putExtra("message","https://google.com");
            startActivity(i);
Sushrita
  • 725
  • 10
  • 29
  • Thank you for your answer.I mean:Textview has a property “autolink”.I set autolink as web.android:autoLink="web" So,android system can automatically detect the url.When clicking the url,it will jump to the browser.Now when clicking, I do not want jump to the brower,I just want to jump to my app activity and stay in app.Do you understand? – flynn Jul 25 '16 at 04:21
  • try it I think it will help you what you want – Sushrita Jul 25 '16 at 04:27
0

If I understand you question correctly this is what you are looking for. In order to control the click event on the text inside the TextView you have to use HTML to create the link and use a SpannableString.

        // textView.setText("Lucy is very nice. Here is her link. https://www.google.com");
        final String source = "Lucy is very nice. Here is her link. <a href=\"scheme://some.link\">Click</a>";
        final Spanned html = Html.fromHtml(source);
        final SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(html);
        final URLSpan[] spans = spannableStringBuilder.getSpans(0, html.length(), URLSpan.class);
        final URLSpan span = spans[0];
        final int start = spannableStringBuilder.getSpanStart(span);
        final int end = spannableStringBuilder.getSpanEnd(span);
        final int flags = spannableStringBuilder.getSpanFlags(span);
        final ClickableSpan clickableSpan = new ClickableSpan() {
            public void onClick(View view) {
                Log.d(TAG, "Clicked: " + span.getURL());
            }
        };
        spannableStringBuilder.setSpan(clickableSpan, start, end, flags);
        spannableStringBuilder.removeSpan(span);
        textView.setText(spannableStringBuilder);
        textView.setLinksClickable(true);
        textView.setMovementMethod(LinkMovementMethod.getInstance());

EDIT

So, according to your comment you can't use HTML so here is another example taking the text from the TextView with autoLink already set:

    final TextView textView = (TextView) findViewById(R.id.text);
    final CharSequence text = textView.getText();
    final SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(text);
    final URLSpan[] spans = spannableStringBuilder.getSpans(0, text.length(), URLSpan.class);
    final URLSpan span = spans[0];
    final int start = spannableStringBuilder.getSpanStart(span);
    final int end = spannableStringBuilder.getSpanEnd(span);
    final int flags = spannableStringBuilder.getSpanFlags(span);
    final ClickableSpan clickableSpan = new ClickableSpan() {
        public void onClick(View view) {
            Log.d(TAG, "Clicked: " + span.getURL());
        }
    };
    spannableStringBuilder.setSpan(clickableSpan, start, end, flags);
    spannableStringBuilder.removeSpan(span);
    textView.setText(spannableStringBuilder);
    textView.setLinksClickable(true);
    textView.setMovementMethod(LinkMovementMethod.getInstance());
Diego Torres Milano
  • 65,697
  • 9
  • 111
  • 134
  • Thank you for your answer.I know your mean.However,I haven't used HTML.This is a chat app,you send text message,and I receive.The url detect is completed by android system with textview setting android:autoLink="web".I can not create the html link. – flynn Jul 25 '16 at 06:04
-1

I don't get what you are trying to say, but this will open a URL, or you can copy it to the cliopboard like copy and paste.

button.setOnClickListener((View.OnClickListener) v -> {
            Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
            startActivity(browserIntent);
        }
Community
  • 1
  • 1
Jared
  • 11
  • 4
  • Textview has a property “autolink”.I set autolink as web.android:autoLink="web" So,android system can automatically detect the url.When clicking the url,it will jump to the browser.Now when clicking, I do not want jump to the brower,I just want to jump to my app activity and stay in app.Do you understand? – flynn Jul 25 '16 at 03:58
  • button.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { setContentView(R.layout."activity you want to use"); Maybe don't use a textView. It sounds as if there is a better way around this. Or is it you have a web browsing app? } } } } – Jared Jul 25 '16 at 04:09
  • I'm doing a chat app. Chat list control must be textview. – flynn Jul 25 '16 at 04:20
  • Okay I think and I have your answer myWebView.setWebViewClient(new WebViewClient(){ @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { if(url.startsWith("www"){ return true; } return false; } }); – Jared Jul 25 '16 at 04:39