Wednesday, 19 April 2017

Android WebView sample code

import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends AppCompatActivity {
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getSupportActionBar().hide();//to hide action bar
        WebView webView = (WebView) findViewById(R.id.webView);
        webView.loadUrl("http://mycreativecodes.blogspot.com");
        webView.getSettings().setJavaScriptEnabled(true);// enabling javascript
        webView.setHorizontalScrollBarEnabled(false);
        webView.setWebViewClient(new WebViewClient());
        webView.setWebViewClient(new WebViewClient() {// to show progressBar when loading page

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
                findViewById(R.id.progress1).setVisibility(View.VISIBLE);
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                findViewById(R.id.progress1).setVisibility(View.GONE);
            }

        });
    }
}

No comments:

Post a Comment