Example usage for android.webkit WebView WebView

List of usage examples for android.webkit WebView WebView

Introduction

In this page you can find the example usage for android.webkit WebView WebView.

Prototype

public WebView(Context context) 

Source Link

Document

Constructs a new WebView with an Activity Context object.

Usage

From source file:com.lcl.thumbweather.MainActivity.java

private void aboutDialog() {
    AlertDialog.Builder alert = new AlertDialog.Builder(this);
    alert.setTitle("ThumbWeather");
    final WebView webView = new WebView(this);
    String about = "<p>A light,open source App.</p>"
            + "<p>Data provided by <a href='http://openweathermap.org/'>OpenWeatherMap</a>, under the <a href='http://creativecommons.org/licenses/by-sa/2.0/'>Creative Commons license</a>"
            + "<p>Icons are <a href='https://erikflowers.github.io/weather-icons/'>Weather Icons</a>, by <a href='http://www.twitter.com/artill'>Lukas Bischoff</a> and <a href='http://www.twitter.com/Erik_UX'>Erik Flowers</a>, under the <a href='http://scripts.sil.org/OFL'>SIL OFL 1.1</a> licence.";
    if (darkTheme) {
        // Style text color for dark theme
        about = "<style media=\"screen\" type=\"text/css\">" + "body {\n" + "    color:white;\n" + "}\n"
                + "a:link {color:cyan}\n" + "</style>" + about;
    }/*from  w w  w.  jav a2 s.c o m*/
    webView.setBackgroundColor(Color.TRANSPARENT);
    webView.loadData(about, "text/html", "UTF-8");
    alert.setView(webView, 32, 0, 32, 0);
    alert.setPositiveButton(R.string.dialog_ok, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {

        }
    });
    alert.show();
}

From source file:com.forecast.weather.activities.MainActivity.java

private void aboutDialog() {
    AlertDialog.Builder alert = new AlertDialog.Builder(this);
    alert.setTitle("Forecast Weather");
    final WebView webView = new WebView(this);
    String about = "<p>Forecast weather app.</p>"
            + "<p>Data provided by <a href='http://openweathermap.org/'>OpenWeatherMap</a>, under the <a href='http://creativecommons.org/licenses/by-sa/2.0/'>Creative Commons license</a>"
            + "<p>Icons are <a href='https://erikflowers.github.io/weather-icons/'>Weather Icons</a>, by <a href='http://www.twitter.com/artill'>Lukas Bischoff</a> under the <a href='http://scripts.sil.org/OFL'>SIL OFL 1.1</a> licence.";
    TypedArray ta = obtainStyledAttributes(new int[] { android.R.attr.textColorPrimary, R.attr.colorAccent });
    String textColor = String.format("#%06X", (0xFFFFFF & ta.getColor(0, Color.BLACK)));
    String accentColor = String.format("#%06X", (0xFFFFFF & ta.getColor(1, Color.BLUE)));
    ta.recycle();/* w  w w  .  j  ava 2s.c  om*/
    about = "<style media=\"screen\" type=\"text/css\">" + "body {\n" + "    color:" + textColor + ";\n" + "}\n"
            + "a:link {color:" + accentColor + "}\n" + "</style>" + about;
    webView.setBackgroundColor(Color.TRANSPARENT);
    webView.loadData(about, "text/html", "UTF-8");
    alert.setView(webView, 32, 0, 32, 0);
    alert.setPositiveButton(R.string.dialog_ok, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {

        }
    });
    alert.show();
}

From source file:com.poepoemyintswe.popularmovies.ui.MainFragment.java

private void showLicense() {
    WebView webView = new WebView(getActivity());
    webView.loadUrl("file:///android_asset/licenses.html");

    new android.app.AlertDialog.Builder(getActivity(), R.style.MyAlertDialogStyle)
            .setTitle(R.string.action_license).setView(webView)
            .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    dialog.dismiss();/* ww w.  j ava  2s .c o m*/
                }
            }).create().show();
}

From source file:org.sigimera.app.android.MainActivity.java

/**
 * Shows the about dialog.//ww  w. ja v a 2  s . c  o m
 */
public final void showAboutDialog() {
    AlertDialog dialog = new AlertDialog.Builder(MainActivity.this).create();
    dialog.setTitle("About");
    dialog.setButton(AlertDialog.BUTTON_POSITIVE, "OK", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(final DialogInterface dialog, final int which) {
            dialog.cancel();
        }
    });

    WebView wv = new WebView(this);
    wv.setBackgroundColor(Color.BLACK);

    StringBuffer strbuffer = new StringBuffer();
    strbuffer.append("<small><font color='white'>");
    strbuffer.append("<h3 style='text-align: center'>" + this.getString(R.string.app_name) + "</h3>");

    strbuffer.append("<p>This is the official App of the Crises "
            + "Information Platform Sigimera. It provides " + "the following functionality:</p>");
    strbuffer.append("<ul>");
    strbuffer.append("<li>Get crises (natural disaster) information."
            + "Currently floods, earthquakes, cyclones " + "and volcanic erruptions.</li>");
    strbuffer.append("<li>Get crises alerts via push notifications.</li>");
    strbuffer.append("<li>Get new crises via push notifications.</li>");
    strbuffer.append("<li>Manage your App via " + "<a href='http://www.sigimera.org/mobile_devices'>"
            + "<span style='color: #00FFFF'>mobile device management" + "website </span></a>.");
    strbuffer.append("</ul>");
    strbuffer.append("<p>&copy; 2013 <a href='http://www.sigimera.com'>"
            + "<span style='color: #00FFFF'>Sigimera Ltd.</span></a>. " + "All rights reserved.</p>");

    wv.loadData(strbuffer.toString(), "text/html", "utf-8");

    dialog.setView(wv);
    dialog.show();
}

From source file:com.phonegap.childBrowser.ChildBrowser.java

/**
 * Display a new browser with the specified URL.
 *
 * @param url           The url to load.
 * @param jsonObject /*from www  .j  a  va 2s.c o m*/
 */
public String showWebPage(final String url, JSONObject options) {
    // Determine if we should hide the location bar.
    if (options != null) {
        showLocationBar = options.optBoolean("showLocationBar", true);
    }

    // Create dialog in new thread 
    Runnable runnable = new Runnable() {
        public void run() {
            dialog = new Dialog(ctx);

            dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
            dialog.setCancelable(true);
            dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
                public void onDismiss(DialogInterface dialog) {
                    try {
                        JSONObject obj = new JSONObject();
                        obj.put("type", CLOSE_EVENT);

                        sendUpdate(obj, false);
                    } catch (JSONException e) {
                        Log.d(LOG_TAG, "Should never happen");
                    }
                }
            });

            LinearLayout.LayoutParams backParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
                    LayoutParams.WRAP_CONTENT);
            LinearLayout.LayoutParams forwardParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
                    LayoutParams.WRAP_CONTENT);
            LinearLayout.LayoutParams editParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
                    LayoutParams.WRAP_CONTENT, 1.0f);
            LinearLayout.LayoutParams closeParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
                    LayoutParams.WRAP_CONTENT);
            LinearLayout.LayoutParams wvParams = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
                    LayoutParams.FILL_PARENT);

            LinearLayout main = new LinearLayout(ctx);
            main.setOrientation(LinearLayout.VERTICAL);

            LinearLayout toolbar = new LinearLayout(ctx);
            toolbar.setOrientation(LinearLayout.HORIZONTAL);

            ImageButton back = new ImageButton(ctx);
            back.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    goBack();
                }
            });
            back.setId(1);
            try {
                back.setImageBitmap(loadDrawable("www/childbrowser/icon_arrow_left.png"));
            } catch (IOException e) {
                Log.e(LOG_TAG, e.getMessage(), e);
            }
            back.setLayoutParams(backParams);

            ImageButton forward = new ImageButton(ctx);
            forward.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    goForward();
                }
            });
            forward.setId(2);
            try {
                forward.setImageBitmap(loadDrawable("www/childbrowser/icon_arrow_right.png"));
            } catch (IOException e) {
                Log.e(LOG_TAG, e.getMessage(), e);
            }
            forward.setLayoutParams(forwardParams);

            edittext = new EditText(ctx);
            edittext.setOnKeyListener(new View.OnKeyListener() {
                public boolean onKey(View v, int keyCode, KeyEvent event) {
                    // If the event is a key-down event on the "enter" button
                    if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) {
                        navigate(edittext.getText().toString());
                        return true;
                    }
                    return false;
                }
            });
            edittext.setId(3);
            edittext.setSingleLine(true);
            edittext.setText(url);
            edittext.setLayoutParams(editParams);

            ImageButton close = new ImageButton(ctx);
            close.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    closeDialog();
                }
            });
            close.setId(4);
            try {
                close.setImageBitmap(loadDrawable("www/childbrowser/icon_close.png"));
            } catch (IOException e) {
                Log.e(LOG_TAG, e.getMessage(), e);
            }
            close.setLayoutParams(closeParams);

            webview = new WebView(ctx);
            webview.getSettings().setJavaScriptEnabled(true);
            webview.getSettings().setBuiltInZoomControls(true);
            WebViewClient client = new ChildBrowserClient(ctx, edittext);
            webview.setWebViewClient(client);
            webview.loadUrl(url);
            webview.setId(5);
            webview.setInitialScale(0);
            webview.setLayoutParams(wvParams);
            webview.requestFocus();
            webview.requestFocusFromTouch();

            toolbar.addView(back);
            toolbar.addView(forward);
            toolbar.addView(edittext);
            toolbar.addView(close);

            if (getShowLocationBar()) {
                main.addView(toolbar);
            }
            main.addView(webview);

            WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
            lp.copyFrom(dialog.getWindow().getAttributes());
            lp.width = WindowManager.LayoutParams.FILL_PARENT;
            lp.height = WindowManager.LayoutParams.FILL_PARENT;

            dialog.setContentView(main);
            dialog.show();
            dialog.getWindow().setAttributes(lp);
        }

        private Bitmap loadDrawable(String filename) throws java.io.IOException {
            InputStream input = ctx.getAssets().open(filename);
            return BitmapFactory.decodeStream(input);
        }
    };
    this.ctx.runOnUiThread(runnable);
    return "";
}

From source file:com.appnexus.opensdk.ANNativeAdResponse.java

boolean handleClick(String clickUrl, Context context) {
    if (clickUrl == null || clickUrl.isEmpty()) {
        return false;
    }// ww  w  .  j ava  2s.  c  o  m
    // if install, open store
    if (clickUrl.contains("://play.google.com") || clickUrl.contains("market://")) {
        Clog.d(Clog.nativeLogTag, Clog.getString(R.string.opening_app_store));
        return openNativeIntent(clickUrl, context);
    }
    // open browser
    if (openNativeBrowser) {
        // if set to use native browser, open intent
        if (openNativeIntent(clickUrl, context)) {
            if (listener != null) {
                listener.onAdWillLeaveApplication();
            }
            return true;
        }
        return false;
    } else {
        // launch Browser Activity
        Class<?> activity_clz = AdActivity.getActivityClass();
        try {
            WebView out = new WebView(new MutableContextWrapper(context));
            WebviewUtil.setWebViewSettings(out);
            out.loadUrl(clickUrl);
            BrowserAdActivity.BROWSER_QUEUE.add(out);

            Intent intent = new Intent(context, activity_clz);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.putExtra(AdActivity.INTENT_KEY_ACTIVITY_TYPE, AdActivity.ACTIVITY_TYPE_BROWSER);
            context.startActivity(intent);
            return true;
        } catch (ActivityNotFoundException e) {
            Clog.w(Clog.baseLogTag, Clog.getString(R.string.adactivity_missing, activity_clz.getName()));
            BrowserAdActivity.BROWSER_QUEUE.remove();
        } catch (Exception e) {
            // Catches PackageManager$NameNotFoundException for webview
            Clog.e(Clog.baseLogTag, "Exception initializing the redirect webview: " + e.getMessage());
            return false;
        }

        return false;
    }
}

From source file:com.phonegap.plugins.childBrowser.ChildBrowser.java

/**
 * Display a new browser with the specified URL.
 *
 * @param url       The url to load.//from   ww w .j a va 2 s  .co  m
 * @param jsonObject 
 */
public String showWebPage(final String url, JSONObject options) {
    // Determine if we should hide the location bar.
    if (options != null) {
        showLocationBar = options.optBoolean("showLocationBar", true);
    }

    // Create dialog in new thread 
    Runnable runnable = new Runnable() {
        public void run() {
            dialog = new Dialog(ctx.getContext());

            dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
            dialog.setCancelable(true);
            dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
                public void onDismiss(DialogInterface dialog) {
                    try {
                        JSONObject obj = new JSONObject();
                        obj.put("type", CLOSE_EVENT);

                        sendUpdate(obj, false);
                    } catch (JSONException e) {
                        Log.d(LOG_TAG, "Should never happen");
                    }
                }
            });

            LinearLayout.LayoutParams backParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
                    LayoutParams.WRAP_CONTENT);
            LinearLayout.LayoutParams forwardParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
                    LayoutParams.WRAP_CONTENT);
            LinearLayout.LayoutParams editParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
                    LayoutParams.WRAP_CONTENT, 1.0f);
            LinearLayout.LayoutParams closeParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
                    LayoutParams.WRAP_CONTENT);
            LinearLayout.LayoutParams wvParams = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
                    LayoutParams.FILL_PARENT);

            LinearLayout main = new LinearLayout(ctx.getContext());
            main.setOrientation(LinearLayout.VERTICAL);

            LinearLayout toolbar = new LinearLayout(ctx.getContext());
            toolbar.setOrientation(LinearLayout.HORIZONTAL);

            ImageButton back = new ImageButton(ctx.getContext());
            back.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    goBack();
                }
            });
            back.setId(1);
            try {
                back.setImageBitmap(loadDrawable("www/childbrowser/icon_arrow_left.png"));
            } catch (IOException e) {
                Log.e(LOG_TAG, e.getMessage(), e);
            }
            back.setLayoutParams(backParams);

            ImageButton forward = new ImageButton(ctx.getContext());
            forward.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    goForward();
                }
            });
            forward.setId(2);
            try {
                forward.setImageBitmap(loadDrawable("www/childbrowser/icon_arrow_right.png"));
            } catch (IOException e) {
                Log.e(LOG_TAG, e.getMessage(), e);
            }
            forward.setLayoutParams(forwardParams);

            edittext = new EditText(ctx.getContext());
            edittext.setOnKeyListener(new View.OnKeyListener() {
                public boolean onKey(View v, int keyCode, KeyEvent event) {
                    // If the event is a key-down event on the "enter" button
                    if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) {
                        navigate(edittext.getText().toString());
                        return true;
                    }
                    return false;
                }
            });
            edittext.setId(3);
            edittext.setSingleLine(true);
            edittext.setText(url);
            edittext.setLayoutParams(editParams);

            ImageButton close = new ImageButton((Context) ctx);
            close.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    closeDialog();
                }
            });
            close.setId(4);
            try {
                close.setImageBitmap(loadDrawable("www/childbrowser/icon_close.png"));
            } catch (IOException e) {
                Log.e(LOG_TAG, e.getMessage(), e);
            }
            close.setLayoutParams(closeParams);

            webview = new WebView(ctx.getContext());
            webview.getSettings().setJavaScriptEnabled(true);
            webview.getSettings().setBuiltInZoomControls(true);

            // dda: intercept calls to console.log
            webview.setWebChromeClient(new WebChromeClient() {
                public boolean onConsoleMessage(ConsoleMessage cmsg) {
                    // check secret prefix
                    if (cmsg.message().startsWith("MAGIC")) {
                        String msg = cmsg.message().substring(5); // strip off prefix
                        /* process HTML */
                        try {
                            JSONObject obj = new JSONObject();
                            obj.put("type", PAGE_LOADED);
                            obj.put("html", msg);
                            sendUpdate(obj, true);
                        } catch (JSONException e) {
                            Log.d("ChildBrowser", "This should never happen");
                        }
                        return true;
                    }
                    return false;
                }
            });
            // dda: inject the JavaScript on page load
            webview.setWebViewClient(new ChildBrowserClient(edittext) {
                public void onPageFinished(WebView view, String address) {
                    // have the page spill its guts, with a secret prefix
                    Log.d("ChildBrowser", "\n\nInjecting javascript\n\n");
                    view.loadUrl(
                            "javascript:console.log('MAGIC'+document.getElementsByTagName('html')[0].innerHTML);");
                }
            });

            //        webview.setWebViewClient(client);
            webview.loadUrl(url);
            webview.setId(5);
            webview.setInitialScale(0);
            webview.setLayoutParams(wvParams);
            webview.requestFocus();
            webview.requestFocusFromTouch();

            toolbar.addView(back);
            toolbar.addView(forward);
            toolbar.addView(edittext);
            toolbar.addView(close);

            if (getShowLocationBar()) {
                main.addView(toolbar);
            }
            main.addView(webview);

            WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
            lp.copyFrom(dialog.getWindow().getAttributes());
            lp.width = WindowManager.LayoutParams.FILL_PARENT;
            lp.height = WindowManager.LayoutParams.FILL_PARENT;

            dialog.setContentView(main);
            dialog.show();
            dialog.getWindow().setAttributes(lp);
        }

        private Bitmap loadDrawable(String filename) throws java.io.IOException {
            InputStream input = ctx.getAssets().open(filename);
            return BitmapFactory.decodeStream(input);
        }
    };
    this.ctx.runOnUiThread(runnable);
    return "";
}

From source file:com.elpstudio.player.externals.Common.java

@Override
public void onCreate() {
    super.onCreate();

    //Application context.
    mContext = getApplicationContext();/*w w  w  . j ava 2 s.co  m*/

    sInstance = this;
    light = Typeface.createFromAsset(getApplicationContext().getAssets(), "segoeuil.ttf");
    reg = Typeface.createFromAsset(getApplicationContext().getAssets(), "segoeui.ttf");

    //SharedPreferences.
    mSharedPreferences = this.getSharedPreferences("com.jams.music.player", Context.MODE_PRIVATE);

    //Init the database.
    mDBAccessHelper = new DBAccessHelper(mContext);

    //Playback kickstarter.
    //mPlaybackKickstarter = new PlaybackKickstarter(this.getApplicationContext());

    //Picasso.
    // mPicasso = new Picasso.Builder(mContext).build();

    //ImageLoader.
    mImageLoader = ImageLoader.getInstance();
    mImageLoaderConfiguration = new ImageLoaderConfiguration.Builder(getApplicationContext())
            .memoryCache(new WeakMemoryCache()).memoryCacheSizePercentage(13)
            .imageDownloader(new ByteArrayUniversalImageLoader(mContext)).build();
    mImageLoader.init(mImageLoaderConfiguration);

    //Init DisplayImageOptions.
    initDisplayImageOptions();

    //Log the user into Google Play Music only if the account is currently set up and active.
    if (mSharedPreferences.getBoolean("GOOGLE_PLAY_MUSIC_ENABLED", false) == true) {

        //Create a temp WebView to retrieve the user agent string.
        String userAgentString = "";
        if (mSharedPreferences.getBoolean("GOT_USER_AGENT", false) == false) {
            WebView webView = new WebView(getApplicationContext());
            webView.setVisibility(View.GONE);
            webView.loadUrl("http://www.google.com");
            userAgentString = webView.getSettings().getUserAgentString();
            mSharedPreferences.edit().putBoolean("GOT_USER_AGENT", true).commit();
            mSharedPreferences.edit().putString("USER_AGENT", userAgentString).commit();
            webView = null;
        }

        /*setGMusicClientCalls(GMusicClientCalls.getInstance(getApplicationContext()));
        GMusicClientCalls.setWebClientUserAgent(userAgentString);
        String accountName = mSharedPreferences.getString("GOOGLE_PLAY_MUSIC_ACCOUNT", "");
                
        //Authenticate with Google.
        AsyncGoogleMusicAuthenticationTask task = new AsyncGoogleMusicAuthenticationTask(mContext, false, accountName);
        task.execute();
        */
    }

}

From source file:com.phonegap.plugin.ChildBrowser.java

/**
 * Display a new browser with the specified URL.
 *
 * @param url           The url to load.
 * @param jsonObject // w w  w  . j av  a  2 s. c o m
 */
public String showWebPage(final String url, JSONObject options) {
    // Determine if we should hide the location bar.
    if (options != null) {
        showLocationBar = options.optBoolean("showLocationBar", true);
    }

    // Create dialog in new thread 
    Runnable runnable = new Runnable() {
        public void run() {
            dialog = new Dialog(ctx.getContext(), android.R.style.Theme_Translucent_NoTitleBar);

            dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
            dialog.setCancelable(true);
            dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
                public void onDismiss(DialogInterface dialog) {
                    try {
                        JSONObject obj = new JSONObject();
                        obj.put("type", CLOSE_EVENT);

                        sendUpdate(obj, false);
                    } catch (JSONException e) {
                        Log.d(LOG_TAG, "Should never happen");
                    }
                }
            });

            LinearLayout.LayoutParams backParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
                    LayoutParams.WRAP_CONTENT);
            LinearLayout.LayoutParams forwardParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
                    LayoutParams.WRAP_CONTENT);
            LinearLayout.LayoutParams editParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
                    LayoutParams.WRAP_CONTENT, 1.0f);
            LinearLayout.LayoutParams closeParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
                    LayoutParams.WRAP_CONTENT);
            LinearLayout.LayoutParams wvParams = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
                    LayoutParams.FILL_PARENT);

            LinearLayout main = new LinearLayout(ctx.getContext());
            main.setOrientation(LinearLayout.VERTICAL);

            LinearLayout toolbar = new LinearLayout(ctx.getContext());
            toolbar.setLayoutParams(
                    new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
            toolbar.setOrientation(LinearLayout.HORIZONTAL);

            ImageButton back = new ImageButton(ctx.getContext());
            back.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    goBack();
                }
            });
            back.setId(1);
            try {
                back.setImageBitmap(loadDrawable("www/childbrowser/icon_arrow_left.png"));
            } catch (IOException e) {
                Log.e(LOG_TAG, e.getMessage(), e);
            }
            back.setLayoutParams(backParams);

            ImageButton forward = new ImageButton(ctx.getContext());
            forward.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    goForward();
                }
            });
            forward.setId(2);
            try {
                forward.setImageBitmap(loadDrawable("www/childbrowser/icon_arrow_right.png"));
            } catch (IOException e) {
                Log.e(LOG_TAG, e.getMessage(), e);
            }
            forward.setLayoutParams(forwardParams);

            edittext = new EditText(ctx.getContext());
            edittext.setOnKeyListener(new View.OnKeyListener() {
                public boolean onKey(View v, int keyCode, KeyEvent event) {
                    // If the event is a key-down event on the "enter" button
                    if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) {
                        navigate(edittext.getText().toString());
                        return true;
                    }
                    return false;
                }
            });
            edittext.setId(3);
            edittext.setSingleLine(true);
            edittext.setText(url);
            edittext.setLayoutParams(editParams);

            ImageButton close = new ImageButton(ctx.getContext());
            close.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    closeDialog();
                }
            });
            close.setId(4);
            try {
                close.setImageBitmap(loadDrawable("www/childbrowser/icon_close.png"));
            } catch (IOException e) {
                Log.e(LOG_TAG, e.getMessage(), e);
            }
            close.setLayoutParams(closeParams);

            webview = new WebView(ctx.getContext());
            webview.setWebChromeClient(new WebChromeClient());
            WebViewClient client = new ChildBrowserClient(edittext);
            webview.setWebViewClient(client);
            WebSettings settings = webview.getSettings();
            settings.setJavaScriptEnabled(true);
            settings.setJavaScriptCanOpenWindowsAutomatically(true);
            settings.setBuiltInZoomControls(true);
            settings.setPluginsEnabled(true);
            settings.setDomStorageEnabled(true);
            webview.loadUrl(url);
            webview.setId(5);
            webview.setInitialScale(0);
            webview.setLayoutParams(wvParams);
            webview.requestFocus();
            webview.requestFocusFromTouch();

            toolbar.addView(back);
            toolbar.addView(forward);
            toolbar.addView(edittext);
            toolbar.addView(close);

            if (getShowLocationBar()) {
                main.addView(toolbar);
            }
            main.addView(webview);

            WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
            lp.copyFrom(dialog.getWindow().getAttributes());
            lp.width = WindowManager.LayoutParams.FILL_PARENT;
            lp.height = WindowManager.LayoutParams.FILL_PARENT;

            dialog.setContentView(main);
            dialog.show();
            dialog.getWindow().setAttributes(lp);
        }

        private Bitmap loadDrawable(String filename) throws java.io.IOException {
            InputStream input = ctx.getAssets().open(filename);
            return BitmapFactory.decodeStream(input);
        }
    };
    this.ctx.runOnUiThread(runnable);
    return "";
}

From source file:com.taobao.wuzhong.plugins.ChildBrowser.java

/**
 * Display a new browser with the specified URL.
 *
 * @param url           The url to load.
 * @param jsonObject//from  w w  w  .jav a  2s  .c  o  m
 */
public String showWebPage(final String url, JSONObject options) {
    // Determine if we should hide the location bar.
    if (options != null) {
        showLocationBar = options.optBoolean("showLocationBar", true);
    }

    // Create dialog in new thread
    Runnable runnable = new Runnable() {
        public void run() {
            dialog = new Dialog(ctx.getContext(), android.R.style.Theme_Translucent_NoTitleBar);

            dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
            dialog.setCancelable(true);
            dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
                public void onDismiss(DialogInterface dialog) {
                    try {
                        JSONObject obj = new JSONObject();
                        obj.put("type", CLOSE_EVENT);

                        sendUpdate(obj, false);
                    } catch (JSONException e) {
                        Log.d(LOG_TAG, "Should never happen");
                    }
                }
            });

            LinearLayout.LayoutParams backParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
                    LayoutParams.WRAP_CONTENT);
            LinearLayout.LayoutParams forwardParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
                    LayoutParams.WRAP_CONTENT);
            LinearLayout.LayoutParams editParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
                    LayoutParams.WRAP_CONTENT, 1.0f);
            LinearLayout.LayoutParams closeParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
                    LayoutParams.WRAP_CONTENT);
            LinearLayout.LayoutParams wvParams = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
                    LayoutParams.FILL_PARENT);

            LinearLayout main = new LinearLayout(ctx.getContext());
            main.setOrientation(LinearLayout.VERTICAL);

            LinearLayout toolbar = new LinearLayout(ctx.getContext());
            toolbar.setLayoutParams(
                    new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
            toolbar.setOrientation(LinearLayout.HORIZONTAL);

            ImageButton back = new ImageButton(ctx.getContext());
            back.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    goBack();
                }
            });
            back.setId(1);
            try {
                back.setImageBitmap(loadDrawable("www/childbrowser/icon_arrow_left.png"));
            } catch (IOException e) {
                Log.e(LOG_TAG, e.getMessage(), e);
            }
            back.setLayoutParams(backParams);

            ImageButton forward = new ImageButton(ctx.getContext());
            forward.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    goForward();
                }
            });
            forward.setId(2);
            try {
                forward.setImageBitmap(loadDrawable("www/childbrowser/icon_arrow_right.png"));
            } catch (IOException e) {
                Log.e(LOG_TAG, e.getMessage(), e);
            }
            forward.setLayoutParams(forwardParams);

            edittext = new EditText(ctx.getContext());
            edittext.setOnKeyListener(new View.OnKeyListener() {
                public boolean onKey(View v, int keyCode, KeyEvent event) {
                    // If the event is a key-down event on the "enter" button
                    if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) {
                        navigate(edittext.getText().toString());
                        return true;
                    }
                    return false;
                }
            });
            edittext.setId(3);
            edittext.setSingleLine(true);
            edittext.setText(url);
            edittext.setLayoutParams(editParams);

            ImageButton close = new ImageButton(ctx.getContext());
            close.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    closeDialog();
                }
            });
            close.setId(4);
            try {
                close.setImageBitmap(loadDrawable("www/childbrowser/icon_close.png"));
            } catch (IOException e) {
                Log.e(LOG_TAG, e.getMessage(), e);
            }
            close.setLayoutParams(closeParams);

            webview = new WebView(ctx.getContext());
            webview.setWebChromeClient(new WebChromeClient());
            WebViewClient client = new ChildBrowserClient(edittext);
            webview.setWebViewClient(client);
            WebSettings settings = webview.getSettings();
            settings.setJavaScriptEnabled(true);
            settings.setJavaScriptCanOpenWindowsAutomatically(true);
            settings.setBuiltInZoomControls(true);
            settings.setPluginsEnabled(true);
            settings.setDomStorageEnabled(true);
            webview.loadUrl(url);
            webview.setId(5);
            webview.setInitialScale(0);
            webview.setLayoutParams(wvParams);
            webview.requestFocus();
            webview.requestFocusFromTouch();

            toolbar.addView(back);
            toolbar.addView(forward);
            toolbar.addView(edittext);
            toolbar.addView(close);

            if (getShowLocationBar()) {
                main.addView(toolbar);
            }
            main.addView(webview);

            WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
            lp.copyFrom(dialog.getWindow().getAttributes());
            lp.width = WindowManager.LayoutParams.FILL_PARENT;
            lp.height = WindowManager.LayoutParams.FILL_PARENT;

            dialog.setContentView(main);
            dialog.show();
            dialog.getWindow().setAttributes(lp);
        }

        private Bitmap loadDrawable(String filename) throws java.io.IOException {
            InputStream input = ctx.getAssets().open(filename);
            return BitmapFactory.decodeStream(input);
        }
    };
    this.ctx.runOnUiThread(runnable);
    return "";
}