Krishnakant Dalal
Krishnakant Dalal

Reputation: 3578

Add http://www. in the text if not Exist

How can I know that some text contain "http://www." I want to show domain in Web View. Domain name is written in TextView but there is no restriction to add prefix. If user didn't enter it I have to add and display URL in webview.

Upvotes: 11

Views: 8922

Answers (7)

Noor Hossain
Noor Hossain

Reputation: 1831

Last of 2024 : Validate url for modern Times (actually it works for me ) :

public static @NonNull String validateUrl(String url ) {

        if (url.startsWith("http://")) {
            url= url.replace("http://","https://" );
        }
        if(url.startsWith("https://")&& !url.startsWith("https://www.")){
            url = url.replace("https://","https://www.");
        }

        if(!url.startsWith("www.")&& !url.startsWith("https://")&&
        !url.startsWith("http://")&& !url.startsWith("http://www.")
                &&!url.startsWith("https://www.")
        ){
            url = "https://www."+url;
        }

        if(url.startsWith("www.")&& !url.startsWith("https://")&&
                !url.startsWith("http://")&& !url.startsWith("http://www.")
                &&!url.startsWith("https://www.")
        ){
            url = "https://www."+url.replace("www.", "");
        }
        return url;
    }

Upvotes: 0

Maor Hadad
Maor Hadad

Reputation: 1872

just modified @silwar answer and add https :

 if(!url.startsWith("www.")&& !url.startsWith("http://") && !url.startsWith("https://")){
        url = "www."+url;
    }
    if(!url.startsWith("http://") && !url.startsWith("https://")){
        url = "http://"+url;
    }

But remember it that sometimes http:// create security exception in android so we should use https://.So for risk-free code, we have to do that like the last checking -

 if(!url.startsWith("http://") && !url.startsWith("https://")){
            url = "https://"+url;}

Upvotes: 5

abhijeet kokane
abhijeet kokane

Reputation: 7

Try this

String a = "http://";
webview.loadUrl(a + url.getText().toString());

Upvotes: -2

silwar
silwar

Reputation: 6518

You can do like this

String url = textView.getText().toString();
if(!url.startsWith("www.")&& !url.startsWith("http://")){
  url = "www."+url;
}
if(!url.startsWith("http://")){
  url = "http://"+url;
}

You can use this url to display content in WebView

Hope this will solve your problem

Upvotes: 28

Joey Roosing
Joey Roosing

Reputation: 2155

As Sebastien requested, regex is a good option. You can also get the text from the view, create an URI object

Uri uri = Uri.create(view.getText().toString());

then with uri.somemethod You should be able to get everything about an url you want to know. If the uri fails to create, you generate error messages cause something has gone wrong.

Upvotes: 2

user1014917
user1014917

Reputation: 681

I would just get the text from the TextView and parse it via startsWith(). If this is false, just add it to the text and use setText() to reasign it.

You might also want to check for other expressions like only "www.". So take a closer look at contains().

Upvotes: 1

Sébastien
Sébastien

Reputation: 14841

The most efficient way of checking that the domain name is well formed and contains (or not) a prefix, is using a regular expression.

Check out Java Pattern to match regex in Android. It is worth it.

Upvotes: 3

Related Questions