dev_swat
dev_swat

Reputation: 1344

How to get url from linkify textview

I need to highlight weburl inside textview. To achieve this I have added android:autoLink="web" attribute inside my textview xml.

If url is related to youtube video I need to play it inside youtube player activity in my app and for other types of urls i want it to open on web browser. So How I detect which url is get click and find is it youtube link or not and perform redirection according to it.

Following are sample text which my text view holding

this is sample text
you can find nice article over this link
www.example.com
and there is nice video which explain here
www.youtube.com/xyzpqr
furthere reading please download pdf from here
www.example.com/pdf/xyz

there are multiple links is present so need to detect that clicked link and preform action on selected link.

Upvotes: 0

Views: 1099

Answers (3)

Sagar Vekariya
Sagar Vekariya

Reputation: 210

Please try below attribute in your textview in xml file:

        android:autoLink="all"
        android:linksClickable="false"
        android:textColorLink="#3393FF"

You can check URL using contains function.

textView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String URL = textView.getText().toString();
            if (URL.contains("youtube") || URL.contains("youtu.be")) {
                // then redirect to youtube
            } else if (URL.contains("pdf")) {
                // then redirect to pdf app
            } else {
                // then redirect to web
            }
        }
    });

OR

you can use below Library and link : https://github.com/saket/Better-Link-Movement-Method

implementation 'me.saket:better-link-movement-method:2.2.0'

Try below code :

BetterLinkMovementMethod
.linkify(Linkify.ALL, textView)
.setOnLinkClickListener((textView, url) -> {
     // Handle clicks.

    if (url.contains("youtube") || url.contains("youtu.be")) {
           // then redirect to youtube
    } else if (url.contains("pdf")) {
           // then redirect to pdf app
    } else {
           // then redirect to web
    }

     return true;
})
.setOnLinkLongClickListener((textView, url) -> {
    // Handle long-clicks.
    return true;
});

Upvotes: 0

Félix Maroy
Félix Maroy

Reputation: 1487

Why don't you use setMouvementMethod() instead ?

Try this in your TextView:

<TextView
    android:id="@+id/myTextView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Text"/>

Then add this line in you Java class:

TextView myLink = (TextView) findViewById(R.id.myTextView);
myLink.setMovementMethod(LinkMovementMethod.getInstance());

Upvotes: 0

Suleman Khan
Suleman Khan

Reputation: 1

You Can Apply Listener on TextView and When Click is Detected use StartsWith Method in Android To Detect if url starts with you.(url of youtube ) if it matches do your youtube player stuff else perform redirection

Upvotes: 0

Related Questions