Example usage for android.webkit WebView setWebViewClient

List of usage examples for android.webkit WebView setWebViewClient

Introduction

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

Prototype

public void setWebViewClient(WebViewClient client) 

Source Link

Document

Sets the WebViewClient that will receive various notifications and requests.

Usage

From source file:com.userhook.view.UHMessageView.java

protected void loadWebViewContent(String html) {

    WebView webView = new WebView(getContext());
    webView.setWebViewClient(new MessageWebViewClient());
    webView.getSettings().setJavaScriptEnabled(true);

    // find dialog width in pixels
    final float scale = getResources().getDisplayMetrics().density;
    int width = (int) (dialogWidth * scale);

    LayoutParams layoutParams = new LayoutParams(width, ViewGroup.LayoutParams.WRAP_CONTENT);
    layoutParams.addRule(CENTER_IN_PARENT);
    addView(webView, layoutParams);/* w ww .j  a  v a  2 s.  c  o  m*/

    webView.loadData(html, "text/html", "utf-8");
    contentView = webView;

    contentLoaded = true;
}

From source file:io.vit.vitio.Fragments.CampusMapFragment.java

@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
        @Nullable Bundle savedInstanceState) {
    ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.campusmap_fragment, container, false);
    setTransitions();/*from ww w .ja v  a  2 s .c om*/
    WebView view = (WebView) rootView.findViewById(R.id.img_map);
    view.setVisibility(View.GONE);

    view.getSettings().setBuiltInZoomControls(true);

    view.getSettings().setDisplayZoomControls(false);

    view.getSettings().setLoadWithOverviewMode(true);

    view.getSettings().setUseWideViewPort(true);

    view.setWebViewClient(new WebViewClient() {

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            try {
                view.setVisibility(View.VISIBLE);
            } catch (Exception ignore) {
            }

        }

        @Override
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
            try {
            } catch (Exception ignore) {
            }

        }
    });

    view.loadUrl("file:///android_asset/vit_map.jpg");

    return rootView;
}

From source file:org.digitalcampus.oppia.widgets.UrlWidget.java

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    // show description if any
    String desc = activity//w w w  . j ava 2 s  .co m
            .getDescription(prefs.getString(PrefsActivity.PREF_LANGUAGE, Locale.getDefault().getLanguage()));

    TextView descTV = (TextView) getView().findViewById(R.id.widget_url_description);
    if (desc.length() > 0) {
        descTV.setText(desc);
    } else {
        descTV.setVisibility(View.GONE);
    }

    WebView wv = (WebView) getView().findViewById(R.id.widget_url_webview);
    int defaultFontSize = Integer.parseInt(prefs.getString(PrefsActivity.PREF_TEXT_SIZE, "16"));
    wv.getSettings().setDefaultFontSize(defaultFontSize);
    wv.getSettings().setJavaScriptEnabled(true);
    wv.setWebViewClient(new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            return false;
        }
    });
    wv.loadUrl(activity
            .getLocation(prefs.getString(PrefsActivity.PREF_LANGUAGE, Locale.getDefault().getLanguage())));

}

From source file:org.schabi.newpipe.ReCaptchaActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_recaptcha);

    // Set return to Cancel by default
    setResult(RESULT_CANCELED);/*from  w  w  w.j  a  v a  2  s . com*/

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    ActionBar actionBar = getSupportActionBar();
    if (actionBar != null) {
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setTitle(R.string.reCaptcha_title);
        actionBar.setDisplayShowTitleEnabled(true);
    }

    WebView myWebView = (WebView) findViewById(R.id.reCaptchaWebView);

    // Enable Javascript
    WebSettings webSettings = myWebView.getSettings();
    webSettings.setJavaScriptEnabled(true);

    ReCaptchaWebViewClient webClient = new ReCaptchaWebViewClient(this);
    myWebView.setWebViewClient(webClient);

    // Cleaning cache, history and cookies from webView
    myWebView.clearCache(true);
    myWebView.clearHistory();
    android.webkit.CookieManager cookieManager = CookieManager.getInstance();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        cookieManager.removeAllCookies(new ValueCallback<Boolean>() {
            @Override
            public void onReceiveValue(Boolean aBoolean) {
            }
        });
    } else {
        cookieManager.removeAllCookie();
    }

    myWebView.loadUrl(YT_URL);
}

From source file:com.devspark.sidenavigation.meiriyiwen.MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);
    icon = (ImageView) findViewById(android.R.id.icon);
    sideNavigationView = (SideNavigationView) findViewById(R.id.side_navigation_view);
    sideNavigationView.setMenuItems(R.menu.side_navigation_menu);
    sideNavigationView.setMenuClickCallback(this);

    if (getIntent().hasExtra(EXTRA_TITLE)) {
        String title = getIntent().getStringExtra(EXTRA_TITLE);
        int resId = getIntent().getIntExtra(EXTRA_RESOURCE_ID, 0);
        setTitle(title);/*from   w  ww  . jav  a  2 s . c  o  m*/
        icon.setImageResource(resId);
        sideNavigationView.setMode(getIntent().getIntExtra(EXTRA_MODE, 0) == 0 ? Mode.LEFT : Mode.RIGHT);
    }

    StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().detectDiskReads().detectDiskWrites()
            .detectNetwork().penaltyLog().build());
    StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectLeakedSqlLiteObjects()
            .detectLeakedClosableObjects().penaltyLog().penaltyDeath().build());
    {
        String title = "http://meiriyiwen.com/index/mobile";
        //setTitle(title);
        WebView uiwebview = (WebView) findViewById(R.id.meiriyiwenWeb);
        uiwebview.setBackgroundColor(Color.BLACK);
        uiwebview.setWebViewClient(new Callback());
        uiwebview.getSettings().setBuiltInZoomControls(true);
        uiwebview.getSettings().setJavaScriptEnabled(true);
        uiwebview.loadUrl(title);

    }

    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

}

From source file:com.just.agentweb.AgentWebUtils.java

static final void clearWebView(WebView m) {

    if (m == null) {
        return;/*from  ww  w. j av a 2  s .c o  m*/
    }
    if (Looper.myLooper() != Looper.getMainLooper()) {
        return;
    }
    m.loadUrl("about:blank");
    m.stopLoading();
    if (m.getHandler() != null) {
        m.getHandler().removeCallbacksAndMessages(null);
    }
    m.removeAllViews();
    ViewGroup mViewGroup = null;
    if ((mViewGroup = ((ViewGroup) m.getParent())) != null) {
        mViewGroup.removeView(m);
    }
    m.setWebChromeClient(null);
    m.setWebViewClient(null);
    m.setTag(null);
    m.clearHistory();
    m.destroy();
    m = null;

}

From source file:com.concentricsky.android.khanacademy.app.SignInActivity.java

/**
 * Set up a url filter to catch our callback, then start a request token operation.
 *//*w  w w .  j av  a 2  s.  c om*/
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    getActionBar().setDisplayHomeAsUpEnabled(true);
    setTitle("Log in");

    // Show no activity transition animation; we want to appear as if we're the same activity as the underlying ShowProfileActivity.
    overridePendingTransition(0, 0);
    setContentView(R.layout.popover_web);

    webView = (WebView) findViewById(R.id.web_view);
    enableJavascript(webView);

    webView.setWebViewClient(new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(WebView webView, String url) {
            showSpinner();
            Log.d(LOG_TAG, ">>>>>>>>>>>>> url: " + url);
            if (url.contains(OAUTH_CALLBACK_URL)) {
                currentTasks.add(new AccessTokenFetcher().execute(url));
                return true;
            }

            return false;
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            hideSpinner();
        }
    });
    showSpinner();

    requestDataService(new ObjectCallback<KADataService>() {
        @Override
        public void call(KADataService service) {
            consumer = service.getAPIAdapter().getConsumer(null);
            currentTasks.add(new RequestTokenFetcher().execute());
        }
    });
}

From source file:org.geometerplus.android.fbreader.network.auth.WebAuthorisationScreen.java

@Override
protected void onCreate(Bundle icicle) {
    super.onCreate(icicle);

    requestWindowFeature(Window.FEATURE_PROGRESS);
    SQLiteCookieDatabase.init(this);
    CookieSyncManager.createInstance(getApplicationContext());
    CookieManager.getInstance().removeAllCookie();
    final Intent intent = getIntent();
    final Uri data = intent.getData();
    if (data == null || data.getHost() == null) {
        finish();/*from  w  w  w. j av  a  2 s. c o m*/
        return;
    }
    final String completeUrl = intent.getStringExtra(COMPLETE_URL_KEY);

    OrientationUtil.setOrientation(this, intent);
    final WebView view = new WebView(this);
    view.getSettings().setJavaScriptEnabled(true);

    view.setWebChromeClient(new WebChromeClient() {
        @Override
        public void onProgressChanged(WebView view, int progress) {
            setProgress(progress * 100);
        }
    });
    view.setWebViewClient(new WebViewClient() {
        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            setTitle(url);
            if (url != null && url.startsWith(completeUrl)) {
                final HashMap<String, String> cookies = new HashMap<String, String>();
                final String cookieString = CookieManager.getInstance().getCookie(url);
                if (cookieString != null) {
                    // cookieString is a string like NAME=VALUE [; NAME=VALUE]
                    for (String pair : cookieString.split(";")) {
                        final String[] parts = pair.split("=", 2);
                        if (parts.length != 2) {
                            continue;
                        }
                        cookies.put(parts[0].trim(), parts[1].trim());
                    }
                }
                storeCookies(data.getHost(), cookies);
                WebAuthorisationScreen.this.setResult(RESULT_OK);
                finish();
            }
        }

        public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
            if (android.os.Build.VERSION.SDK_INT == android.os.Build.VERSION_CODES.ECLAIR_MR1) {
                // hack for auth problem in android 2.1
                handler.proceed();
            } else {
                super.onReceivedSslError(view, handler, error);
            }
        }
    });
    setContentView(view);
    view.loadUrl(intent.getDataString());
}

From source file:net.idlesoft.android.apps.github.activities.SingleActivityItem.java

@Override
public void onCreate(final Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.single_activity_item);

    mPrefs = getSharedPreferences(Hubroid.PREFS_NAME, 0);
    mEditor = mPrefs.edit();/* w w  w .  j a  v  a 2 s  .c  om*/

    ((ImageButton) findViewById(R.id.btn_search)).setOnClickListener(new OnClickListener() {
        public void onClick(final View v) {
            startActivity(new Intent(SingleActivityItem.this, Search.class));
        }
    });

    final Bundle extras = getIntent().getExtras();
    if (extras != null) {
        try {
            mJson = new JSONObject(extras.getString("item_json"));
            loadActivityItemBox();
            final WebView content = (WebView) findViewById(R.id.wv_single_activity_item_content);
            content.setWebViewClient(new WebViewClient() {
                public boolean shouldOverrideUrlLoading(final WebView view, final String url) {
                    if (url.startsWith("hubroid://")) {
                        final String parts[] = url.substring(10).split("/");
                        if (parts[0].equals("showCommit")) {
                            final Intent intent = new Intent(SingleActivityItem.this, Commit.class);
                            intent.putExtra("repo_owner", parts[1]);
                            intent.putExtra("repo_name", parts[2]);
                            intent.putExtra("commit_sha", parts[3]);
                            startActivity(intent);
                        } else if (parts[0].equals("showRepo")) {
                            final Intent intent = new Intent(SingleActivityItem.this, Repository.class);
                            intent.putExtra("repo_owner", parts[1]);
                            intent.putExtra("repo_name", parts[2]);
                            startActivity(intent);
                        } else if (parts[0].equals("showUser")) {
                            final Intent intent = new Intent(SingleActivityItem.this, Profile.class);
                            intent.putExtra("username", parts[1]);
                            startActivity(intent);
                        } else if (parts[0].equals("showIssues")) {
                            final Intent intent = new Intent(SingleActivityItem.this, Issues.class);
                            intent.putExtra("repo_owner", parts[1]);
                            intent.putExtra("repo_name", parts[2]);
                            startActivity(intent);
                        }
                        return true;
                    }
                    return false;
                }
            });
            String html = "";
            final String eventType = mJson.getString("type");
            if (eventType.equals("PushEvent")) {
                html = NewsFeedHelpers.linkifyPushItem(mJson);
            } else if (eventType.equals("CreateEvent")) {
                final String object = mJson.getJSONObject("payload").getString("object");
                if (object.equals("branch")) {
                    html = NewsFeedHelpers.linkifyCreateBranchItem(mJson);
                } else if (object.equals("repository")) {
                    html = NewsFeedHelpers.linkifyCreateRepoItem(mJson);
                }
            } else if (eventType.equals("CommitCommentEvent")) {
                html = NewsFeedHelpers.linkifyCommitCommentItem(mJson);
            } else if (eventType.equals("FollowEvent")) {
                html = NewsFeedHelpers.linkifyFollowItem(mJson);
            } else if (eventType.equals("ForkEvent")) {
                html = NewsFeedHelpers.linkifyForkItem(mJson);
            } else if (eventType.equals("IssuesEvent")) {
                html = NewsFeedHelpers.linkifyIssueItem(mJson);
            } else if (eventType.equals("WatchEvent")) {
                html = NewsFeedHelpers.linkifyWatchItem(mJson);
            }
            final String out = CSS + html;
            content.loadData(out, "text/html", "UTF-8");
        } catch (final JSONException e) {
            e.printStackTrace();
        }
    }
}

From source file:com.emobc.android.activities.generators.PdfActivityGenerator.java

@Override
protected void loadAppLevelData(final Activity activity, final AppLevelData data) {
    final PdfLevelDataItem item = (PdfLevelDataItem) data.findByNextLevel(nextLevel);
    if (item == null)
        return;//from  www  . j a v  a  2s  .  c  om

    //rotateScreen(activity);
    initializeHeader(activity, item);

    //Create Banner
    CreateMenus c = (CreateMenus) activity;
    c.createBanner();
    if (Utils.hasLength(item.getPdfUrl())) {
        WebView webview = (WebView) activity.findViewById(R.id.pdfviewer);
        webview.getSettings().setJavaScriptEnabled(true);
        webview.getSettings().setPluginsEnabled(true);
        webview.setWebViewClient(new InsideWebViewClient(activity));

        webview.loadUrl("http://docs.google.com/gview?embedded=true&url=" + item.getPdfUrl());

        //activity.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(item.getPdfUrl())));

        /*
        File outputDir = activity.getCacheDir(); 
        try {
           File outputFile = File.createTempFile("eMobc", ".tmp", outputDir);
           if(downloadPdfToFile(outputFile, item.getPdfUrl())){
              File file = new File(outputFile.getAbsolutePath());
                
              if (file.exists()) {                      
               Uri path = Uri.fromFile(outputFile);
               Intent intent = new Intent(Intent.ACTION_VIEW);
               intent.setDataAndType(path, "application/pdf");
               intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                
               try {
                  activity.startActivity(intent);
               }catch (ActivityNotFoundException e) {
                   Toast.makeText(activity, 
                       "No Application Available to View PDF", 
                       Toast.LENGTH_SHORT).show();
               }
              }
           }            
        } catch (IOException e1) {
           Log.e("PdfActivityGenerator", "Error: " + e1.getMessage());
        }*/
        /*
                
        String pdfFileUrl = String.format(PDF_WEBVIEW_TPL, item.getPdfUrl());
                
        try {
           URL url = new URL(pdfFileUrl);
           URI uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(), url.getQuery(), url.getRef());
           url = uri.toURL();
                
                   
           WebView webView = (WebView)activity.findViewById(R.id.pdfviewer);
           webView.getSettings().setJavaScriptEnabled(true); 
           webView.loadUrl(url.toString());
                   
           Uri uriPath = Uri.parse(item.getPdfUrl());
                   
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(uriPath, "application/pdf");
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                
        try {
           activity.startActivity(intent);
        } catch (ActivityNotFoundException e) {
            Toast.makeText(activity, 
                "No Application Available to View PDF", 
                Toast.LENGTH_SHORT).show();
        }
        } catch (MalformedURLException e) {
           Log.e("PdfActivityGenerator", "MalformedURLException: " + e.getMessage());
        } catch (URISyntaxException e) {
           Log.e("PdfActivityGenerator", "URISyntaxException: " + e.getMessage());
        }
        */
    }
}