0

I want to open link which is in text view in the form of text:type ="autolink" and it contains some URL but when a user tap on it, opens browser instead of webview activity

For Example

{

  <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
         android:autoLink="web"
        android:text="www.facebook.com"
        android:id="@+id/iimage" />

}

so please tell if a user clicks on this link it opens in webView in WebView activity. How to do this

fool-dev
  • 7,671
  • 9
  • 40
  • 54
Manik
  • 153
  • 4
  • 15

3 Answers3

1

You will need to set a Spannable to your textview and set the click listener on that Spannable.

Here you have an example i took from some answer: Android: ClickableSpan in clickable TextView

TextView tv = (TextView)findViewById(R.id.textview01);      
Spannable span = Spannable.Factory.getInstance().newSpannable("test link span");   
span.setSpan(new ClickableSpan() {  
    @Override
    public void onClick(View v) {  
        Log.d("main", "link clicked");
        Toast.makeText(Main.this, "link clicked", Toast.LENGTH_SHORT).show(); 
    } }, 5, 9, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

// All the rest will have the same spannable.
ClickableSpan cs = new ClickableSpan() {  
    @Override
    public void onClick(View v) {  
        Log.d("main", "textview clicked");
        Toast.makeText(Main.this, "textview clicked", Toast.LENGTH_SHORT).show(); 
    } };

// set the "test " spannable.
span.setSpan(cs, 0, 5, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

// set the " span" spannable
span.setSpan(cs, 6, span.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

tv.setText(span);

tv.setMovementMethod(LinkMovementMethod.getInstance());
Clapa Lucian
  • 590
  • 2
  • 7
  • 14
0

Look How can we open TextView's links into Webview

But actually all you need is webView.loadUrl("www.facebook.com")

600
  • 84
  • 3
  • no bro I am trying to open text view links in other activity with webview not just by loading loadUrl(); – Manik Feb 14 '18 at 21:07
  • Well, ok. And what a problem to pass String to other activity in intent extras, in activity extract it and put to webView? – 600 Feb 14 '18 at 21:10
0

You can do this simply by:

    iimage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
               Intent intent = new Intent(currentactivity, youractivity.class);
                intent.putExtra("url", your_url );
                startActivity(intent);
                finish();
            }
        });

Inside onCreate of your youractivity

String url = getIntent().getStringExtra("url");



webView.loadUrl(url);

Hope this helps.

Mamedov
  • 69
  • 8
nimi0112
  • 2,065
  • 1
  • 18
  • 32