'Android TextView make http and custom scheme urls clickable

I'm creating an android app and have defined an activity as follows:

<activity
    android:name="com.scheme.app.MainActivity"
    android:screenOrientation="portrait"
    android:theme="@style/MainActivityTheme">
    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <data
            android:host="invite"
            android:scheme="schemeapp" />
        <category android:name="android.intent.category.BROWSABLE" />
    </intent-filter>
</activity>

When hitting the url "schemeapp://invite" the user will be taken to MainActivity.

I have a textView with the following content:

textView.setText("Testing custom scheme - schemeapp://invite with http http://www.LMNGTFY.com");

I need to make the web url (http://www.LMNGTFY.com) as well as the custom url (schemeapp://invite) clickable.

What I've already tried:

String text = "Testing custom scheme - schemeapp://invite with http http://www.LMNGTFY.com";
textView.setText(text);
Linkify.addLinks(textView, Linkify.WEB_URLS | Linkify.PHONE_NUMBERS | Linkify.EMAIL_ADDRESSES | Linkify.ALL);
Pattern urlDetect = Pattern.compile("(schemeapp):\\/\\/([a-zA-Z0-9.]+)");
Matcher matcher = urlDetect.matcher(text);
String scheme = null;

while (matcher.find()) {
    String customSchemedUrl = matcher.group(0);
    Uri uri = Uri.parse(customSchemedUrl);
    scheme = uri.getScheme();
    break;
}

if (!TextUtils.isEmpty(scheme)) {
    Linkify.addLinks(textView, urlDetect, scheme);
}

If I remove the following line of code to detect web urls, the custom scheme url works:

Linkify.addLinks(textView, Linkify.WEB_URLS | Linkify.PHONE_NUMBERS | Linkify.EMAIL_ADDRESSES | Linkify.ALL);

If I try to add http as a custom scheme url, the http urls aren't clickable.

EDIT: Clarification

I cannot use HTML since, user input will also be displayed on the TextView and needs to be linked.

Can you please help. Thanks



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source