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:org.dmfs.oauth2.android.fragment.InteractiveGrantFragment.java

@SuppressLint("SetJavaScriptEnabled")
@Nullable//from ww  w . j  a v  a 2s  .  c  o m
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
        @Nullable Bundle savedInstanceState) {
    if (mWebView == null) {
        // create and configure the WebView
        // Note using just getActivity would will leak the activity.
        mWebView = new WebView(getActivity().getApplicationContext());
        mWebView.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT));
        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.setOnKeyListener(this);
        mWebView.setWebViewClient(mGrantWebViewClient);
        mWebView.loadUrl(mGrant.authorizationUrl().toASCIIString());

        // wipe cookies to enforce a new login
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
            CookieManager.getInstance().removeAllCookies(null);
            CookieManager.getInstance().flush();
        } else {
            CookieSyncManager cookieSyncMngr = CookieSyncManager.createInstance(getActivity());
            cookieSyncMngr.startSync();
            CookieManager cookieManager = CookieManager.getInstance();
            cookieManager.removeAllCookie();
            cookieManager.removeSessionCookie();
            cookieSyncMngr.stopSync();
            cookieSyncMngr.sync();
        }
    }
    return mWebView;
}

From source file:org.projecthdata.hhub.ui.HDataWebOauthActivity.java

@Override
public void onCreate(Bundle savedInstanceState) {
    getWindow().requestFeature(Window.FEATURE_PROGRESS);
    getWindow().setFeatureInt(Window.FEATURE_PROGRESS, Window.PROGRESS_VISIBILITY_ON);
    super.onCreate(savedInstanceState);

    webView = new WebView(this);
    setContentView(webView);/*from www  .  j ava2 s . c  o m*/
    this.ehrUrl = getIntent().getStringExtra(EXTRA_EHR_URL);

    activity = this;

    this.connectionRepository = getApplicationContext().getConnectionRepository();
    this.hDataConnectionFactory = getApplicationContext().getHDataConnectionFactory(ehrUrl);

    webView.setWebChromeClient(new WebChromeClient() {

        public void onProgressChanged(WebView view, int progress) {
            activity.setTitle("Loading...");
            activity.setProgress(progress * 100);
            if (progress == 100) {
                activity.setTitle(R.string.app_name);
            }
        }
    });
    webView.setWebViewClient(new MyWebViewClient());

    // clear out any previously used credentials
    webView.clearCache(true);
    webView.clearFormData();
    webView.clearHistory();
    webView.getSettings().setSavePassword(false);
    webView.getSettings().setSaveFormData(false);
    CookieSyncManager.createInstance(this);
    CookieManager.getInstance().removeAllCookie();
}

From source file:com.thefinestartist.simplewebviewfragment.SimpleWebViewFragment.java

@SuppressLint({ "SetJavaScriptEnabled", "NewApi" })
@Override//from  w w w.j  a v a2s .  c o m
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.web_view, container, false);

    mPbar = (ProgressBar) view.findViewById(R.id.web_view_progress);

    mView = (FrameLayout) view.findViewById(R.id.web_view);
    mView.setLayoutParams(
            new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));

    mImageView = new ImageView(getActivity());
    mImageView.setBackgroundColor(getResources().getColor(android.R.color.white));
    mImageView.setImageResource(R.drawable.big_image_loading);
    mImageView.setScaleType(ScaleType.CENTER_INSIDE);
    mImageView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
    mImageView.post(new Runnable() {
        @Override
        public void run() {
            ((AnimationDrawable) mImageView.getDrawable()).start();
        }
    });
    mView.addView(mImageView);

    mWebview = new WebView(getActivity());
    mWebview.setVisibility(View.GONE);
    mWebview.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
    if (mUrl != null) {
        mWebview.setWebViewClient(new MyWebViewClient());
        mWebview.setWebChromeClient(new MyWebChromeClient());
        mWebview.getSettings().setPluginState(PluginState.ON);
        mWebview.getSettings().setUseWideViewPort(true);
        mWebview.getSettings().setDefaultZoom(ZoomDensity.FAR);
        mWebview.getSettings().setBuiltInZoomControls(true);
        mWebview.getSettings().setSupportZoom(true);
        mWebview.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
        mWebview.getSettings().setAllowFileAccess(true);
        mWebview.getSettings().setDomStorageEnabled(true);
        mWebview.getSettings().setJavaScriptEnabled(true);
        mWebview.getSettings().setAppCacheEnabled(true);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
            mWebview.getSettings().setDisplayZoomControls(false);

        mWebview.loadUrl(mUrl);
    }
    mView.addView(mWebview);

    mBackBtn = (ImageButton) view.findViewById(R.id.web_view_btn_back);
    mFowardBtn = (ImageButton) view.findViewById(R.id.web_view_btn_forward);
    mRefreshBtn = (ImageButton) view.findViewById(R.id.web_view_btn_refresh);
    mRefreshPbar = (ProgressBar) view.findViewById(R.id.loading);
    mShareBtn = (ImageButton) view.findViewById(R.id.web_view_btn_share);

    mBackBtn.setOnClickListener(this);
    mFowardBtn.setOnClickListener(this);
    mRefreshBtn.setOnClickListener(this);
    mShareBtn.setOnClickListener(this);

    updateActionView();

    return view;
}

From source file:de.qabel.qabelbox.fragments.WelcomeDisclaimerFragment.java

private void setClickListener(View fragmentView, int view, final Type mode) {

    fragmentView.findViewById(view).setOnClickListener(new View.OnClickListener() {
        @Override//from  w  w w  . java 2s .co  m
        public void onClick(View v) {

            WebView webView = new WebView(getActivity());
            String file = "";
            if (mode == Type.QAPL) {
                file = getResources().getString(R.string.FILE_QAPL_LICENSE);
            }
            if (mode == Type.PRIVACY) {
                file = getResources().getString(R.string.FILE_PRIVACY);
            }
            if (mode == Type.LEGAL) {
                file = getResources().getString(R.string.FILE_LEGAL);
            }

            webView.loadDataWithBaseURL("file:///android_asset/",
                    FileHelper.loadFileFromAssets(getActivity(), "html/help/" + file), "text/html", "utf-8",
                    null);

            UIHelper.showCustomDialog(getActivity(), webView, R.string.ok, null);
        }
    });
}

From source file:piuk.blockchain.android.ui.SendCoinsActivity.java

@Override
protected Dialog onCreateDialog(final int id) {
    final WebView webView = new WebView(this);
    webView.loadUrl("file:///android_asset/help_send_coins" + languagePrefix() + ".html");

    final Dialog dialog = new Dialog(this);
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    dialog.setContentView(webView);// w  w  w . j  av  a2 s  . co m
    dialog.setCanceledOnTouchOutside(true);

    return dialog;
}

From source file:io.github.hidroh.materialistic.SubmitActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    AppUtils.setStatusBarColor(getWindow(), ContextCompat.getColor(this, R.color.blackT12));
    setContentView(R.layout.activity_submit);
    setSupportActionBar((Toolbar) findViewById(R.id.toolbar));
    //noinspection ConstantConditions
    getSupportActionBar().setDisplayOptions(
            ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_SHOW_TITLE | ActionBar.DISPLAY_HOME_AS_UP);
    mTitleLayout = (TextInputLayout) findViewById(R.id.textinput_title);
    mContentLayout = (TextInputLayout) findViewById(R.id.textinput_content);
    mTitleEditText = (TextView) findViewById(R.id.edittext_title);
    mContentEditText = (TextView) findViewById(R.id.edittext_content);
    String text, subject;//from  w ww .j a v a2  s  .  c om
    if (savedInstanceState == null) {
        subject = getIntent().getStringExtra(Intent.EXTRA_SUBJECT);
        text = getIntent().getStringExtra(Intent.EXTRA_TEXT);
    } else {
        subject = savedInstanceState.getString(STATE_SUBJECT);
        text = savedInstanceState.getString(STATE_TEXT);
    }
    mTitleEditText.setText(subject);
    mContentEditText.setText(text);
    if (TextUtils.isEmpty(subject)) {
        if (isUrl(text)) {
            WebView webView = new WebView(this);
            webView.setWebChromeClient(new WebChromeClient() {
                @Override
                public void onReceivedTitle(WebView view, String title) {
                    if (mTitleEditText.length() == 0) {
                        mTitleEditText.setText(title);
                    }
                }
            });
            webView.loadUrl(text);
        } else if (!TextUtils.isEmpty(text)) {
            extractUrl(text);
        }
    }
}

From source file:fi.aalto.legroup.achso.browsing.DetailActivity.java

@Override
public boolean onMenuItemClick(MenuItem menuItem) {
    String licenceInfo = GooglePlayServicesUtil.getOpenSourceSoftwareLicenseInfo(DetailActivity.this);

    // Using a WebView is a lot faster than showing the text as the dialog's message
    WebView webView = new WebView(DetailActivity.this);
    webView.loadDataWithBaseURL(null, licenceInfo, "text/plain", null, null);

    new AlertDialog.Builder(DetailActivity.this).setTitle(R.string.about_maps).setView(webView)
            .setNeutralButton(R.string.ok, new DialogInterface.OnClickListener() {
                @Override/*  w w w  . ja  va 2 s. com*/
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            }).show();

    return true;
}

From source file:com.ohso.omgubuntu.CommentsActivity.java

@SuppressLint("SetJavaScriptEnabled")
@Override//from   w w  w . ja  v a2  s  .  c om
public void onCreate(Bundle instance) {
    super.onCreate(instance);
    setContentView(R.layout.fragment_comments);
    setTitle("Comments");
    ActionBar actionBar = getSupportActionBar();
    actionBar.setDisplayHomeAsUpEnabled(true);

    mArticlePath = getIntent().getStringExtra(COMMENTS_URL);
    mIdentifier = getIntent().getStringExtra(COMMENTS_IDENTIFIER);
    if (mArticlePath == null)
        finish();
    mFragmentManager = getSupportFragmentManager();

    // Setting up the default Disqus WebView

    WebViewFragment commentsFragment;

    if (instance != null) {
        commentsFragment = (WebViewFragment) getSupportFragmentManager().findFragmentByTag("comments");
        commentView = commentsFragment.getWebView();
    } else {
        commentsFragment = new WebViewFragment();
        mFragmentManager.beginTransaction()
                .replace(R.id.comments_fragment_container, commentsFragment, "comments").commit();
        commentView = new WebView(this);
        commentsFragment.setWebView(commentView);
    }
    if (commentView == null)
        commentView = new WebView(this);
    commentView.getSettings().setJavaScriptEnabled(true);
    commentView.getSettings().setSupportMultipleWindows(true);
    commentView.setWebChromeClient(new WebFragmentClient(this));
    commentView.setWebViewClient(new WebClient(this));
    if (instance == null)
        setContents(mArticlePath);
}

From source file:es.glasspixel.wlanaudit.activities.AboutActivity.java

/**
 * @see android.app.Activity#onStart/* ww  w .  j a va  2 s .c  o  m*/
 */
@Override
protected void onStart() {
    super.onStart();
    mVersionValueLabel.setText(getVersion());
    mReleaseValueLabel.setText(String.valueOf(getRelease()));
    mOssLicensesButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            AlertDialog.Builder alert = new AlertDialog.Builder(AboutActivity.this);

            alert.setTitle(getText(R.string.oss_licenses_dialog_title));
            WebView wv = new WebView(AboutActivity.this);

            wv.loadUrl("file:///android_asset/licenses.html");
            alert.setView(wv);
            alert.show();
        }
    });
    mFacebookButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent browserIntent = new Intent(Intent.ACTION_VIEW,
                    Uri.parse("https://www.facebook.com/wlanaudit"));
            startActivity(browserIntent);
        }
    });
    mTwitterButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent browserIntent = new Intent(Intent.ACTION_VIEW,
                    Uri.parse("https://twitter.com/#!/TitoXamps"));
            startActivity(browserIntent);
        }
    });
    mGplusButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent browserIntent = new Intent(Intent.ACTION_VIEW,
                    Uri.parse("https://plus.google.com/b/113060827862322417267/113060827862322417267"));
            startActivity(browserIntent);
        }
    });
}

From source file:au.com.wallaceit.reddinator.TabCommentsFragment.java

@SuppressLint({ "SetJavaScriptEnabled", "AddJavascriptInterface" })
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mContext = this.getActivity();
    SharedPreferences mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(mContext);
    global = (GlobalObjects) mContext.getApplicationContext();
    final boolean load = getArguments().getBoolean("load");

    // get needed activity values
    articleId = getActivity().getIntent().getStringExtra(WidgetProvider.ITEM_ID);
    permalink = getActivity().getIntent().getStringExtra(WidgetProvider.ITEM_PERMALINK);

    ll = new LinearLayout(mContext);
    ll.setLayoutParams(new WebView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.MATCH_PARENT, 0, 0));
    // fixes for webview not taking keyboard input on some devices
    mWebView = new WebView(mContext);
    mWebView.setLayoutParams(new WebView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.MATCH_PARENT, 0, 0));
    ll.addView(mWebView);//from  w ww .ja  va  2  s . co m
    WebSettings webSettings = mWebView.getSettings();
    webSettings.setJavaScriptEnabled(true); // enable ecmascript
    webSettings.setDomStorageEnabled(true); // some video sites require dom storage
    webSettings.setSupportZoom(false);
    webSettings.setBuiltInZoomControls(false);
    webSettings.setDisplayZoomControls(false);
    int fontSize = Integer.parseInt(mSharedPreferences.getString("commentfontpref", "18"));
    webSettings.setDefaultFontSize(fontSize);
    webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);

    mSharedPreferences.getString("titlefontpref", "16");

    final String themeStr = global.mThemeManager.getActiveTheme("appthemepref").getValuesString();
    mWebView.setWebViewClient(new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            boolean redditLink = false;
            System.out.println(url);
            if (url.indexOf("file://") == 0) { // fix for short sub and user links
                url = url.replace("file://", "https://www.reddit.com") + "/.compact";
                redditLink = true;
            }
            if (redditLink || url.indexOf("https://www.reddit.com/") == 0) {
                Intent i = new Intent(mContext, WebViewActivity.class);
                i.putExtra("url", url);
                startActivity(i);
            } else {
                Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                startActivity(i);
            }
            return true; // always override url
        }

        public void onPageFinished(WebView view, String url) {
            mWebView.loadUrl("javascript:init(\"" + StringEscapeUtils.escapeJavaScript(themeStr) + "\", \""
                    + global.mRedditData.getUsername() + "\")");
            if (load)
                load();
        }
    });
    mWebView.setWebChromeClient(new WebChromeClient());

    mWebView.requestFocus(View.FOCUS_DOWN);
    WebInterface webInterface = new WebInterface(mContext);
    mWebView.addJavascriptInterface(webInterface, "Reddinator");

    mWebView.loadUrl("file:///android_asset/comments.html#" + articleId);
}