If string starts with "www" automatically insert "https://"

Asked

Viewed 77 times

2

Here’s what I’m trying to do

So the user doesn’t need to type https:// I’m trying to make sure that when the text of EditText start with www. will automatically insert the https://

I tried to:

 if (!s_url.startsWith("www.")) {
    myWebView.loadUrl("http://"+s_url);
 }

But to no avail.

What am I doing wrong?

Code:

final String s_url = editText_url.getText().toString();
final WebView myWebView = (WebView) findViewById(R.id.webview);
final EditText editText_url = (EditText) findViewById(R.id.edittext_url);

//IME Action
    editText_url.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            boolean handled = true;
            if (actionId == EditorInfo.IME_ACTION_GO) {
                myWebView.loadUrl(editText_url.getText().toString());
                if (!s_url.startsWith("www.")) {
                    myWebView.loadUrl("http://"+s_url);
                }
                return true;
            }
            return true;
        }
    });

  • You can edit your server’s virtual host?

1 answer

3


Are you using the denial operator !, so it doesn’t work the way you expect it to.

 if (!s_url.startsWith("www.")) {
    myWebView.loadUrl("https://" + s_url);
 }

The second line only comes into action when the content of s_url does not begin with www..

  • 1

    Thanks ,so was to take the !. PS: I ended up using editText_url.getText().toString().startsWith("www.")) in place of var s_url but I believe the two work in the same way

Browser other questions tagged

You are not signed in. Login or sign up in order to post.