load Url Adaptive Screen using WebView - Android User Interface

Android examples for User Interface:WebView URL

Description

load Url Adaptive Screen using WebView

Demo Code


//package com.java2s;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;

import android.util.DisplayMetrics;

import android.webkit.WebSettings;

import android.webkit.WebSettings.ZoomDensity;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class Main {
    @SuppressWarnings("deprecation")
    @SuppressLint("SetJavaScriptEnabled")
    public static void loadUrlAdaptiveScreen(Context mContext,
            WebView webview, String url, boolean javaScriptEnabled) {
        WebSettings webSettings = webview.getSettings();
        webSettings.setJavaScriptEnabled(javaScriptEnabled);
        DisplayMetrics metrics = new DisplayMetrics();
        ((Activity) mContext).getWindowManager().getDefaultDisplay()
                .getMetrics(metrics);//from w w  w  .j  av a 2  s  .com
        int mDensity = metrics.densityDpi;
        if (mDensity <= 120) {
            webSettings.setDefaultZoom(ZoomDensity.CLOSE);
        } else if (mDensity > 120 && mDensity < 240) {
            webSettings.setDefaultZoom(ZoomDensity.MEDIUM);
        } else if (mDensity >= 240) {
            webSettings.setDefaultZoom(ZoomDensity.FAR);
        }

        webview.setWebViewClient(new WebViewClient());
        webview.loadUrl(url);

    }

    @SuppressLint("SetJavaScriptEnabled")
    public static void loadUrl(Context mContext, WebView webview,
            String url, boolean javaScriptEnabled) {
        WebSettings webSettings = webview.getSettings();
        webSettings.setJavaScriptEnabled(javaScriptEnabled);
        webview.setWebViewClient(new WebViewClient());
        webview.loadUrl(url);
    }
}

Related Tutorials