0

I have 2 links inside HTML code that is shown via a TextView. The first needs to open the URL in a webbrowser. The second needs to open another Activity in the App, so it must trigger some code in the App. Is this possible?

    val textView = findViewById<TextView>(R.id.someTextView)

    val html = "<ul>\n" +
    "<li><a href=\\https://www.google.nl>Click here for google</a></li>\n" +
    "<li><a href=\\scheme:open-something>Open App screen</a></li>\n" +
    "</ul>\n"

    textView.text = Html.fromHtml(html, Html.TO_HTML_PARAGRAPH_LINES_CONSECUTIVE)

TextView:

<TextView
    android:id="@+id/someTextView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />
Jim Clermonts
  • 1,694
  • 8
  • 39
  • 94

1 Answers1

0

I've used this post as a starting point.

    val textView = findViewById<TextView>(R.id.someTextView)

    val html = "<ul>" +
            "<li><a href='myscheme://www.google.com'>link</a></li>" +
            "<li><a href='myscheme://someuniquevalue.google.com'>link jim</a></li>" +
            "</ul>"

    textView.setText(Html.fromHtml(html))
    textView.setMovementMethod(LinkMovementMethod.getInstance())

WebViewActivity:

class WebViewActivity : Activity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        if (savedInstanceState == null) {
            val url = intent.dataString!!.replace("myscheme://", "https://")

            val i = Intent(Intent.ACTION_VIEW)
            i.data = Uri.parse(url)
            startActivity(i)
        }
    }
}

Androidmanifest:

    <activity android:name=".WebViewActivity" android:exported="false">
        <intent-filter>
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.DEFAULT" />
            <data android:scheme="myscheme" />
        </intent-filter>
    </activity>
Jim Clermonts
  • 1,694
  • 8
  • 39
  • 94