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

2

I'm trying to do the following

For the user do not need to type https:// I'm trying to make, when the text of EditText starts with www. it will automatically insert https://

I've tried:

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

But without success.

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;
        }
    });
asked by anonymous 15.02.2015 / 20:08

1 answer

4

You're using the negation with%, so it does not work the way that you expect.

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

The second line only takes action when the content of ! does not start with s_url .

    
15.02.2015 / 20:34