How to make deep link string clickable in android TextView

白昼怎懂夜的黑 提交于 2020-03-22 23:48:10

问题


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?


回答1:


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




回答2:


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



回答3:


Just use this

YourTextView.setMovementMethod(LinkMovementMethod.getInstance());



回答4:


Looking at https://stackoverflow.com/a/13509741/2914140, 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
    )
}


来源:https://stackoverflow.com/questions/36003131/how-to-make-deep-link-string-clickable-in-android-textview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!