Breaking News

How to create a Full Webview





In Android Programming, To create a Web Browser, we need a WebView to implement in our XML file.

For that, let's create a project first called MyBrowser.

in activitymain.xml add WebView Code. We create it match_parent so that it can fit on full screen.
 <WebView  
     android:id="@+id/webview"  
     android:layout_width="match_parent"  
     android:layout_height="match_parent"/>  

Then in your main activity,

create a variable of Webview and a Url to open in Webview
 public class MainActivity extends AppCompatActivity {  
   WebView mywebview;  
   String url = "https://www.google.com";  

then in your onCreate, find WebView Id and use WebSettings to handle a WebView.
 mywebview = findViewById(R.id.webview);  
     mywebview.loadUrl(url);  
   
     WebSettings settings = mywebview.getSettings();  
   
     settings.setJavaScriptEnabled(true);  
     settings.setDisplayZoomControls(false);  
     settings.supportZoom();  
     settings.setSupportZoom(true);  
     settings.setBuiltInZoomControls(true);  
     settings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);  
     settings.setLoadWithOverviewMode(true);  
     settings.setUseWideViewPort(true);  
   
     mywebview.clearHistory();  
     mywebview.clearCache(true);  

Then add Internet Permission in AndroidManifest.xml
 <uses-permission android:name="android.permission.INTERNET" />  

If we run this code now, it opens the Url but didn't handle clicking on a link.
Let's Fix it.

Add Webchromeclient and WebViewClient in your MainActivity.onCreate

Here onPageStarted When your WebPage Starts, onPageFinished When your Page Finished Loading.
and ShouldOverrideLoading when the user clicks on a link
 mywebview.setWebChromeClient(new WebChromeClient());  
   
     mywebview.setWebViewClient(new WebViewClient()  
     {  
       @Override  
       public void onPageStarted(WebView view, String url, Bitmap favicon) {  
           }  
         super.onPageStarted(view, url, favicon);  
       }  
   
       @Override  
       public void onPageFinished(WebView view, String url) {  
          super.onPageFinished(view, url);  
       }  
   
       @Override  
       public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {  
         return super.shouldOverrideUrlLoading(view, request);  
       }  
     });  

Done now you can open any link in your web browser.
Thanks.

Rounded Corner Edit text in Android

1 comment:

  1. while using view and favicon it show two options add parameter and add method body what to do

    ReplyDelete