Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to open a url inside inapp browser efficiently ?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 253
    Comment on it

    Below is a simple android activity which shows you clearly how to open a particular url inside our native application. In here we have taken a single xml i.e. "show_web_view" which is used as a layout of this activity in which only a WebView is defined.

    public class OpenUrlWebView extends Activity {
    
    //private Button button;
    private WebView webView;
    public void onCreate(Bundle savedInstanceState) {
    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.show_web_view);
    
        //Get webview
        webView = (WebView) findViewById(R.id.webView1);
    
        startWebView("http://www.google.com");
    
    }
    
    private void startWebView(String url) {
    
     //Create new webview Client to show progress dialog
        //When opening a url or click on link
    
        webView.setWebViewClient(new WebViewClient() {     
            ProgressDialog progressDialog;
    
            //If you will not use this method url links are opeen in new brower not in webview
            public boolean shouldOverrideUrlLoading(WebView view, String url) {             
                view.loadUrl(url);
                return true;
            }
    
            //Show loader on url load
            public void onLoadResource (WebView view, String url) {
                if (progressDialog == null) {
                    // in standard case YourActivity.this
                    progressDialog = new ProgressDialog(ShowWebView.this);
                    progressDialog.setMessage("Loading...");
                    progressDialog.show();
                }
            }
            public void onPageFinished(WebView view, String url) {
                try{
                if (progressDialog.isShowing()) {
                    progressDialog.dismiss();
                    progressDialog = null;
                }
                }catch(Exception exception){
                    exception.printStackTrace();
                }
            }
    
        });
    
         // Javascript enabled on webview 
        webView.getSettings().setJavaScriptEnabled(true);
    
        // Other webview options
        webView.getSettings().setLoadWithOverviewMode(true);
        webView.getSettings().setUseWideViewPort(true);
        webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
        webView.setScrollbarFadingEnabled(false);
        webView.getSettings().setBuiltInZoomControls(true);
    
        //Load url in webview
        webView.loadUrl(url);
    
    }
    

    // Open previous opened link from history on webview when back button pressed

    @Override
    // Detect when the back button is pressed
    public void onBackPressed() {
        if(webView.canGoBack()) {
            webView.goBack();
        } else {
            // Let the system handle the back button
            super.onBackPressed();
        }
    }
    

    Hope this will help you. If you have any queries regarding please feel free to ask. :)

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: