How to make deep link string clickable in android TextView
Asked Answered
R

4

5

How do I make a deep link string for example "myapp://product/123" clickable in android TextView. I know there are autoLink options like email, web and phone but there isn't any deeplink option. How do I make it clickable and launch the intent on click of that link?

Rootstock answered 15/3, 2016 at 5:14 Comment(6)
what about using android:autoLink="web" attribute ?Gibber
Didn't work... Since it's not a weblinkRootstock
That means you are trying to make some specific word clickable in TextView ?Gibber
No. I was trying to make a deeplink string clickableRootstock
upon the click I want to make an explicit intent and launch it so that I can go to specific activity that is registered with same intent filter and categoryRootstock
Are you wanting this link to be clickable everywhere, or only within the context of a TextView inside your own app?Schroder
I
5

you can do that by using ClickableSpan

eg.

ClickableSpan clickableSpan = new ClickableSpan() {
    @Override
    public void onClick(View textView) {
        startActivity(new Intent(MyActivity.this, NextActivity.class));
    }
    @Override
    public void updateDrawState(TextPaint ds) {
            super.updateDrawState(ds);
            ds.setUnderlineText(false);
        }
};

See this link How to set the part of the text view is clickable

Iterative answered 15/3, 2016 at 5:18 Comment(0)
S
0

Just you have to make code as like below in java file.That can be you can click to any link from textview.

TextView t2 = (TextView) findViewById(R.id.text2);
t2.setMovementMethod(LinkMovementMethod.getInstance());
Shabbir answered 15/3, 2016 at 5:18 Comment(2)
Appreciate your help. But that didn't helpRootstock
Works only for http/https scheme.Tauto
H
0

Just use this

YourTextView.setMovementMethod(LinkMovementMethod.getInstance());
Haff answered 15/3, 2016 at 5:19 Comment(2)
Appreciate your answer.. But that didn't helpRootstock
if you added android:autoLink="web" in xml layout, then remove that property and try.Haff
T
0

Looking at https://mcmap.net/q/45588/-how-to-make-links-in-a-textview-clickable, I wrote similar:

val url = "myapp://example.com/some_string"
textView.text = url
textView.setOnClickListener {
    startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(url)))
}

You don't even need <uses-permission android:name="android.permission.INTERNET" /> in AndroidManifest.

If you have an application, responding to myapp scheme and example.com host, it will be opened.

To format the textView like a link also write:

textView.hyperlinkStyle()


private fun TextView.hyperlinkStyle() {
    setText(
        SpannableString(text).apply {
            setSpan(
                URLSpan(""),
                0,
                length,
                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
            )
        },
        TextView.BufferType.SPANNABLE
    )
}
Tauto answered 13/3, 2020 at 8:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.