1. This website uses cookies to improve service and provide a tailored user experience. By using this site, you agree to this use. See our Cookie Policy.
    Dismiss Notice

android webview help needed

Discussion in 'Programming' started by west555, Jan 21, 2019.

  1. west555

    west555 Regular Member

    Joined:
    Dec 4, 2011
    Messages:
    415
    Likes Received:
    156
    Location:
    /etc/passwd
    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();
           }
    
        }
    }
    
     
  2. Lifelong

    Lifelong Newbie

    Joined:
    May 28, 2014
    Messages:
    36
    Likes Received:
    6
    Occupation:
    System Administrator
    Location:
    UK
    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.