Bobby
Bobby

Reputation: 1454

Android TextView URL

In android, a TextView with a URL can be clicked to open the URL in the web by using:

android:autoLink="web"

What I would like to do is instead capture this click and if this TextView contains a URL, then I would like to display a Dialog. How can I go about this? Thanks!

Upvotes: 1

Views: 170

Answers (2)

Lan Nguyen
Lan Nguyen

Reputation: 795

Simply, by implement setOnCclikListener, check your string of TextView if it's a URL display the dialog.

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class URLValidator {

    private Pattern pattern;
    private Matcher matcher;

    private static final String URL_PATTERN = 
        "/^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/";

    public URLValidator() {
        pattern = Pattern.compile(URL_PATTERN);
    }

    //Validate your input string 
    public boolean validate(final String url) {
        matcher = pattern.matcher(url);
        return matcher.matches();
    }
}

Check your text get from TextView

URLValidator validator = new URLValidator();
if(validator.valiadte("yourString")) {
    //Show your dialog
}

Upvotes: 0

Lazy Ninja
Lazy Ninja

Reputation: 22527

Check if your text is url or not.

try {
    new URI(YourText);
    // show your dialog here
} catch (URISyntaxException e) {
    // Certainly not an URL
}

Upvotes: 1

Related Questions