android webview help needed

west555

Senior Member
Joined
Dec 4, 2011
Messages
1,004
Reaction score
455
I am making a simple webview app which should load certain webpage in app. And if URL contains specific Word it need to open chrome or any other external browser on my phone and load url with it. but its not working quite as expected ( still trying to load url inside app instead of opening external chrome )
here is my code bellow. I would appreciate if any android developer from here could help me solve this, I don't know what to do. ( i know am noob, but learning )

Code:
package us.bgame.app;

import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.content.Intent;
import android.net.Uri;

public class MainActivity extends Activity {
public WebView myview;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);

        myview = (WebView)findViewById(R.id.myview);
        WebSettings webSettings =myview.getSettings();
        webSettings.setJavaScriptEnabled(true);
        myview.loadUrl("http://mysite.com/");
        myview.setWebViewClient(new WebViewClient());

    }

    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if (url != null && url.startsWith("https://part of url")) {
            view.getContext().startActivity(
                    new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
            
            return true;
        } else {
            return false;
        }
    }

    @Override
    public void onBackPressed() {
       if(myview.canGoBack()){
           myview.goBack();
       }else {
           super.onBackPressed();
       }

    }
}
 
You're not calling 'shouldOverrideUrlLoading' anywhere, and the 'webSettings.setJavaScriptEnabled(true);' in your main funtion is forcing the in-app view.

Try this

Code:
public class MainActivity extends Activity {
public WebView myview;
   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       requestWindowFeature(Window.FEATURE_NO_TITLE);
       setContentView(R.layout.activity_main);
       url = "https://google.com"
       if shouldOverrideUrlLoading(url) == false {
     
           myview = (WebView)findViewById(R.id.myview);
           WebSettings webSettings =myview.getSettings();
           webSettings.setJavaScriptEnabled(true);
           myview.loadUrl(url);
           myview.setWebViewClient(new WebViewClient());

   }

You've also got a typo (?) here 'if (url != null && url.startsWith("https://part of url"))' - remove 'part of url'.

Hope this helps.
 
Back
Top