Code example for WebView: Displaying web pages

Methods: setOnCreateContextMenuListenerloadUrlsetMapTrackballToArrowKeyssetWebChromeClientsetWebViewClient, setDownloadListener

0
    /** 
     * Creates a new WebView and registers it with the global settings. 
     */ 
    private WebView createNewWebView() {
        // Create a new WebView 
        WebView w = new WebView(mActivity);
        w.setMapTrackballToArrowKeys(false); // use trackball directly
        // Enable the built-in zoom 
        //w.getSettings().setBuiltInZoomControls(false); 
 
        // Add this WebView to the settings observer list and update the 
        // settings 
        final BrowserSettings s = BrowserSettings.getInstance();
        s.addObserver(w.getSettings()).update(s, null);
        return w;
    } 
 
    /** 
     * Put the current tab in the background and set newTab as the current tab. 
     * @param newTab The new tab. If newTab is null, the current tab is not 
     *               set. 
     */ 
    boolean setCurrentTab(Tab newTab) {
        return setCurrentTab(newTab, false);
    } 
 
    /** 
     * If force is true, this method skips the check for newTab == current. 
     */ 
    private boolean setCurrentTab(Tab newTab, boolean force) {
        Tab current = getTab(mCurrentTab);
        if (current == newTab && !force) {
            return true; 
        } 
        if (current != null) {
            // Remove the current WebView and the container of the subwindow 
            putTabInBackground(current);
        } 
 
        if (newTab == null) {
            return false; 
        } 
 
        // Move the newTab to the end of the queue 
        int index = mTabQueue.indexOf(newTab);
        if (index != -1) {
            mTabQueue.remove(index);
        } 
        mTabQueue.add(newTab);
 
        WebView mainView;
        WebView subView;
 
        // Display the new current tab 
        mCurrentTab = mTabs.indexOf(newTab);
        mainView = newTab.mMainView;
        boolean needRestore = (mainView == null);
        if (needRestore) {
            // Same work as in createNewTab() except don't do new Tab() 
            newTab.mMainView = mainView = createNewWebView();
        } 
        mainView.setWebViewClient(mActivity.getWebViewClient());
        mainView.setWebChromeClient(mActivity.getWebChromeClient());
        mainView.setOnCreateContextMenuListener(mActivity);
        mainView.setDownloadListener(mActivity);
        // Add the subwindow if it exists 
        if (newTab.mSubViewContainer != null) {
            subView = newTab.mSubView;
            subView.setWebViewClient(newTab.mSubViewClient);
            subView.setWebChromeClient(newTab.mSubViewChromeClient);
            subView.setOnCreateContextMenuListener(mActivity);
            subView.setDownloadListener(mActivity);
        } 
        if (needRestore) {
            // Have to finish setCurrentTab work before calling restoreState 
            if (!restoreState(newTab.mSavedState, newTab)) {
                mainView.loadUrl(BrowserSettings.getInstance().getHomePage());
            } 
        } 
        return true; 
    }