show WebView and set URL - Android User Interface

Android examples for User Interface:WebView URL

Description

show WebView and set URL

Demo Code


//package com.java2s;
import android.content.Context;
import android.util.Log;
import android.view.ViewGroup;
import android.webkit.ConsoleMessage;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class Main {
    protected static final String TAG = "WebviewUtil";

    public static WebView showWebView(Context context, ViewGroup container) {
        WebView view = new WebView(context);
        view.setWebChromeClient(new WebChromeClient() {
            @Override//  ww w  . j ava 2s. c  om
            public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
                Log.i(TAG, "msg:" + consoleMessage.message());
                return super.onConsoleMessage(consoleMessage);
            }
        });
        view.getSettings().setJavaScriptEnabled(true);

        view.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                Log.v(TAG, "url:" + url);
                return super.shouldOverrideUrlLoading(view, url);
            }

            @Override
            public void onLoadResource(WebView view, String url) {
                super.onLoadResource(view, url);
                Log.d(TAG, "loadResource: url:" + url);
            }
        });

        container.removeAllViews();
        container.addView(view);
        view.loadUrl("file:///android_asset/geo.html");
        return view;
    }
}

Related Tutorials