简体   繁体   中英

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

This is one option:

final Spanned html = Html.fromHtml("Testing custom scheme - <a href='schemeapp://invite'>schemeapp://invite</a> with http http://www.LMNGTFY.com";);

helpText.setText(html);
helpText.setMovementMethod(new LinkMovementMethod());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM