-5

I'm sorry it's a little bit long. but here's a point

if i run this app. webview shows up and then EditText and Button(GO button) is there at the top of screen

if i type http://www.google.co.kr and press 'go' button. it works proper but if i type www.google.co.kr and press 'go'. it doesn't work

as you know guys, if we type some URL without 'http://' it goes that URL anyway. so i want make this code to be work like that.

i didn't put all code of this app at this post. but if necessary. i can upload or add.

which option should i add? or how i can make that work?

It's a preview image of my app, don't care about korean words:

It's a preview image of my app, don't care about korean words

xml code below

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="1">

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">


<EditText
    android:layout_width="320dp"
    android:layout_height="40dp"
    android:id="@+id/txtURL"
    android:text="http://www.google.co.kr" />

<Button
    android:layout_width="match_parent"
    android:layout_height="40dp"
    android:text="Go"
    android:id="@+id/btnGo"
    android:onClick="goURL" />

<WebView
    android:id="@+id/WebView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="0.90" />



<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">



</LinearLayout>


java code below

@SuppressLint("SetJavaScriptEnabled") public class MainActivity extends ActionBarActivity {

    private WebView mWebView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mWebView = (WebView) findViewById(R.id.WebView1);
        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.loadUrl("http://google.co.kr");
        mWebView.setWebViewClient(new WishWebViewClient());

        mWebView.setWebViewClient(new WebViewClient(){ });

    }

    @Override

    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
            mWebView.goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    private class WishWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    }

    public void goURL(View view){
        TextView tvURL = (TextView)findViewById(R.id.txtURL);
        String url = tvURL.getText().toString();
        Log.i("URL","Opening URL :"+url);

        WebView webView = (WebView)findViewById(R.id.WebView1);
        webView.setWebViewClient(new WebViewClient()); 
        webView.loadUrl(url);

    }


}

2 Answers2

0

I think this can be achieved by checking whether the string is a valid url using a regex.

This regex is got from another SO post:

"\\b(https?|ftp|file)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]"

You first check whether url is a valid url using this regex. If the url is something like google.com, it does not match. If the url does not match, add "https://" to the start of it and see if it matches. If it matches this time, load the url, otherwise, tell the user that it's not a valid url or something of the sort.

Pattern p = Pattern.compile("\\b(https?|ftp|file)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]");
if (!p.matcher(url).matches() &&
        (p.matcher("https://" + url).matches()) {
    url = "https://" + url;
    // load the url
}
Community
  • 1
  • 1
Sweeper
  • 213,210
  • 22
  • 193
  • 313
  • It's my first time hearing 'regex'. but i'll give a shot. thank you for you comment. by the way. that line "\\b(https?|ftp|file)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]" can be used just like that? or am i need to adjust it? – Norris Zing May 10 '17 at 06:28
  • @NorrisZing just use it like that! Regex is a very powerful thing. I recommend you to learn more about it! – Sweeper May 10 '17 at 06:36
  • @NorrisZing If you think my answer answers your question, please consider accepting it by clicking on that checkmark! – Sweeper May 10 '17 at 06:45
  • Oh i totally forgot it. you got me. thank you for your comment. – Norris Zing May 10 '17 at 06:46
0

Try to prepend your text with http:// before sending the request.