flynn
flynn

Reputation: 177

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

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.

Upvotes: 4

Views: 1961

Answers (4)

Jared
Jared

Reputation: 11

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);
        }

Upvotes: -1

flynn
flynn

Reputation: 177

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.

Upvotes: 5

Diego Torres Milano
Diego Torres Milano

Reputation: 69368

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());

Upvotes: 0

Sushrita
Sushrita

Reputation: 725

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);

Upvotes: 0

Related Questions