2

I have a view display textview and enable autolink.

<TextView
   android:id="@+id/txt_contents"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:layout_margin="30dp"
   android:textColor="@color/txt_code_itemlist"
   android:autoLink="web"
   android:textSize="22sp" />

Now, when I click link from textview it's will open browser. But I want to find way to open new activity when I click same link.

Goffity
  • 142
  • 6

4 Answers4

0

that work perfect :

    tvoublie.setPaintFlags(tvoublie.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
            Linkify.addLinks(tvoublie, Linkify.ALL);
            tvoublie.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Log.i("LoginActivity", "Sign Up Activity activated.");
                    // this is where you should start the new Activity
                   Intent intent = new Intent(CurrentActivity.this,YourNewActivity.class);
                   startActivity(intent);  

                }
            });

this to open o browser with url :

register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setAction(Intent.ACTION_VIEW);
                intent.addCategory(Intent.CATEGORY_BROWSABLE);
                intent.setData(Uri.parse("https://www.google.com"));
                startActivity(intent);
            }
        });
Med Elgarnaoui
  • 1,612
  • 1
  • 18
  • 35
0

You can use Linkify in TextView,

    TextView sample = (TextView) findViewById(R.id.txt_contents);

    String webUrlOrText = "www.google.com a search Engine";
    Spannable spannableString = new SpannableString(Html.fromHtml(webUrlOrText));



    Linkify.addLinks(spannableString, Linkify.WEB_URLS); // It will make only Weblink clickable

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

    sample.setText(spannableString);
    sample.setMovementMethod(LinkMovementMethod.getInstance());

And LinkSpan class,

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

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

        if (url != null) {
           // Open the Activity here
           Log.d(TAG, "Url clicked");
        }
    }
}
Muthukrishnan Rajendran
  • 11,122
  • 3
  • 31
  • 41
-1

You can add this.

text_view.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
          Intent intent = new Intent(CurrentActivity.this,YourNewActivity.class);
          startActivity(intent);  
        }
    });

Hope this helps.

Sarthak Gandhi
  • 2,130
  • 1
  • 16
  • 23
  • Thanks. @sarthak, But this solution will open new activity when click any area of Textview. I want to able event only http link. – Goffity Jun 07 '17 at 04:17
-1

Use the below code to identify which part of text is clicked

tv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
    ClassroomLog.log(TAG, "Textview Click listener ");
    if (tv.getSelectionStart() == -1 && tv.getSelectionEnd() == -1) {
        //This condition will satisfy only when it is not an autolinked text
        //Fired only when you touch the part of the text that is not hyperlinked 
    }
}

});

and write code to do whatever you want with either case

Shubham Goel
  • 1,962
  • 17
  • 25