WebView Example

In our Example, Webview and Static HTML two pages added in app. It display webpage in one side and other one as Html page.

actvity_main.xml

This xml displays two buttons and a Webview in our layout.

 



    

        

MainActivity.java

This activity shows webview and static html in one by one page.

package com.example.icowboysradio.myapplication;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{


        WebView webView;
        Button webPage, staticHtml;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            staticHtml = (Button) findViewById(R.id.staticHTML);
            staticHtml.setOnClickListener(this);
            webPage = (Button) findViewById(R.id.webPage);
            webPage.setOnClickListener(this);
            webView = (WebView) findViewById(R.id.webView);

        }

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.staticHTML:

                    String customHtml = "

Hi, CodebridgePlus

" + "

Android

PHP Manual

Angular JS

" + "

The Full Study Material for alll Beginners

" + ""; webView.loadData(customHtml, "text/html", "UTF-8"); break; case R.id.webPage: webView.setWebViewClient(new MyWebViewClient()); String url = "https://codebridgeplus.com/"; webView.getSettings().setJavaScriptEnabled(true); webView.loadUrl(url); break; } } private class MyWebViewClient extends WebViewClient { @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { view.loadUrl(url); return true; } } }