Skip to content

📱 Este é um projeto modelo para o Android Studio que permite criar um aplicativo de Webview do Android em questão de minutos.

Notifications You must be signed in to change notification settings

ronysfreitas/webview

Repository files navigation

WebView APP Example

This is a template project for Android Studio that allows you to create an android webview application in minutes. You can use it to create a simple app for your website or as a starting point for your HTML5 based android app.

Getting started

  1. Install Android Studio, make sure that the Android SDK Tools are properly installed and install the appropriate packages for the platforms you want to target.

  2. Download or clone this repository and import it into Android Studio.

Explaining the code

These are the basic settings of a good WEB APP with WebView:

AndroidManifest

  • You must add the INTERNET permissions to your Android Manifest File

    This must be a child of the element.

     <uses-permission android:name="android.permission.INTERNET" />

MainActivity

  • Getting WebView Settings (line 49)

     WebSettings webSettings = webView.getSettings();
  • Enabling JavaScript (line 52)

     webSettings.setJavaScriptEnabled(true);
  • Disable Support Zoom (recommendation) (line 55)

     webSettings.setSupportZoom(false);
  • Settings to enable/disable cache in WebView (line 58)

     webView.setDrawingCacheEnabled(true);
  • Settings to disable text selection in WebView (start in line 61)

     webView.setOnLongClickListener(new View.OnLongClickListener() {
         @Override
         public boolean onLongClick(View v) {
             return true;
         }
     });
  • Settings to enable Cookie in WebView (start in line 69)

     CookieManager cookieManager = CookieManager.getInstance();
         if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
             CookieSyncManager.createInstance(this);
         }
     cookieManager.setAcceptCookie(true);
  • Set Up WebViewClient (start in line 75)

     webView.setWebViewClient(new WebViewClient() {
         @Override
         public boolean shouldOverrideUrlLoading(WebView view, String url) {
             if (Uri.parse(url).getHost().equals("www.example.com")) {
                 // This is my web site, so do not override; let my WebView load the page
                 return false;
             }
             // Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
             Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
             startActivity(intent);
             return true;
         }
    
         @Override
         public void onPageStarted(WebView view, String url, Bitmap favicon) {
             // Page Started
             webView.setVisibility(View.INVISIBLE);
             progressBar.setVisibility(View.VISIBLE);
         }
    
         @Override
         public void onPageFinished(WebView view, String url) {
             // Page Finished
             progressBar.setVisibility(View.INVISIBLE);
             webView.setVisibility(View.VISIBLE);
         }
    
         @Override
         public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
             // In case of error
         }
     });
  • Load resource (start in line 109)

     // Use remote resource
     webView.loadUrl("http://www.example.com");
    
     // Use local resource
     // webview.loadUrl("file:///android_asset/www/index.html");
  • Function for reload WebView (start in line 116)

     private void webViewReload() {
         webView.reload();
     }
  • Reload WebView on click item menu

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
    
        if (id == R.id.reload) {
            webViewReload();
            return true;
        }
    
        return super.onOptionsItemSelected(item);
    }
  • Navigating web page history

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

About

📱 Este é um projeto modelo para o Android Studio que permite criar um aplicativo de Webview do Android em questão de minutos.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages