Example usage for android.webkit WebSettings setJavaScriptEnabled

List of usage examples for android.webkit WebSettings setJavaScriptEnabled

Introduction

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

Prototype

public abstract void setJavaScriptEnabled(boolean flag);

Source Link

Document

Tells the WebView to enable JavaScript execution.

Usage

From source file:com.hyperkode.friendshare.fragment.TwitterWebViewFragment.java

public void onResume() {
    super.onResume();
    String url = null;//from  w w  w .  j ava  2 s  .  c  o m
    FragmentManager fragmentManager = TwitterWebViewFragment.this.getActivity().getSupportFragmentManager();
    Bundle args = this.getArguments();
    if (args != null) {
        url = args.getString("URL");
        loginFragment = (LoginFragment) fragmentManager.getFragment(args, "LoginFragment");
    }

    WebView webView = (WebView) mThisActivity.findViewById(R.id.twitter_webview);
    WebSettings webSettings = webView.getSettings();
    webSettings.setSavePassword(false);
    webSettings.setSaveFormData(false);
    webSettings.setJavaScriptEnabled(true);
    webSettings.setSupportZoom(false);

    webView.setWebViewClient(new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if (url.contains(getString(R.string.TWITTER_CALLBACK_URL))) {
                Uri uri = Uri.parse(url);
                String oauthVerifier = uri.getQueryParameter("oauth_verifier");
                if (loginFragment != null) {
                    loginFragment.setOAuthVerifierResult(oauthVerifier);
                }
                return true;
            }
            return false;
        }
    });
    webView.loadUrl(url);
}

From source file:com.usabusi.newsreader.ArticleFragment.java

/**
 * Sets up the UI. It consists if a single WebView.
 *///from   w ww . j a  va 2s.  c  o  m
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    mWebView = new WebView(getActivity());
    //http://stackoverflow.com/questions/17259537/load-webview-from-cache
    WebSettings webSettings = mWebView.getSettings();
    // Enable JavaScript
    webSettings.setJavaScriptEnabled(true); // enable javascript
    mWebView.setHorizontalScrollBarEnabled(false);
    webSettings.setAppCacheMaxSize(5 * 1024 * 1024); // 5MB
    webSettings.setAppCachePath(getActivity().getApplicationContext().getCacheDir().getAbsolutePath());
    webSettings.setAllowFileAccess(true);
    webSettings.setAppCacheEnabled(true);
    webSettings.setCacheMode(WebSettings.LOAD_DEFAULT); // load online by default
    //http://stackoverflow.com/questions/25161720/url-opened-in-browser-instead-of-web-view
    mWebView.setVisibility(View.VISIBLE);
    webSettings.setPluginState(WebSettings.PluginState.ON);
    webSettings.setBuiltInZoomControls(true);

    //final Activity activity = this;
    // Make WebClient
    mWebView.setWebViewClient(new WebViewClient() {
        // Trace Errors
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
            Toast.makeText(getActivity().getApplicationContext(), description, Toast.LENGTH_SHORT).show();
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            //http://developer.android.com/guide/webapps/webview.html#HandlingNavigation
            //                view.loadUrl(url);
            //                return true;
            return false;
        }
    });

    loadWebView();
    return mWebView;
}

From source file:net.olejon.mdapp.MedicationFelleskatalogenFragment.java

@SuppressLint("SetJavaScriptEnabled")
@Override//from   ww w.jav  a 2  s  . com
public View onCreateView(LayoutInflater inflater, ViewGroup container, final Bundle savedInstanceState) {
    ViewGroup viewGroup = (ViewGroup) inflater.inflate(R.layout.fragment_medication_felleskatalogen, container,
            false);

    // Activity
    final Activity activity = getActivity();

    // Context
    final Context context = activity.getApplicationContext();

    // Tools
    final MyTools mTools = new MyTools(context);

    // Arguments
    Bundle bundle = getArguments();

    final String pageUri = bundle.getString("uri");

    // Progress bar
    final ProgressBar progressBar = (ProgressBar) activity
            .findViewById(R.id.medication_toolbar_progressbar_horizontal);

    // Toolbar
    final LinearLayout toolbarSearchLayout = (LinearLayout) activity
            .findViewById(R.id.medication_toolbar_search_layout);
    final EditText toolbarSearchEditText = (EditText) activity.findViewById(R.id.medication_toolbar_search);

    // Web view
    WEBVIEW = (WebView) viewGroup.findViewById(R.id.medication_felleskatalogen_content);

    WebSettings webSettings = WEBVIEW.getSettings();
    webSettings.setJavaScriptEnabled(true);

    WEBVIEW.setWebViewClient(new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if (!mTools.isDeviceConnected()) {
                mTools.showToast(getString(R.string.device_not_connected), 0);
                return true;
            } else if (url.matches("^https?://.*?\\.pdf$")) {
                mTools.downloadFile(view.getTitle(), url);
                return true;
            }

            return false;
        }
    });

    WEBVIEW.setWebChromeClient(new WebChromeClient() {
        @Override
        public void onProgressChanged(WebView view, int newProgress) {
            if (newProgress == 100) {
                progressBar.setVisibility(View.INVISIBLE);
            } else {
                progressBar.setVisibility(View.VISIBLE);
                progressBar.setProgress(newProgress);

                toolbarSearchLayout.setVisibility(View.GONE);
                toolbarSearchEditText.setText("");
            }
        }
    });

    if (savedInstanceState == null) {
        WEBVIEW.loadUrl(pageUri);
    } else {
        WEBVIEW.restoreState(savedInstanceState);
    }

    return viewGroup;
}

From source file:org.noise_planet.noisecapture.MapActivity.java

public void loadWebView() {
    WebView leaflet = (WebView) findViewById(R.id.webmapview);
    WebSettings webSettings = leaflet.getSettings();
    webSettings.setJavaScriptEnabled(true);
    leaflet.clearCache(true);/*w ww.j  a  va2s . co m*/
    leaflet.setInitialScale(200);
    String location = "";
    if (builder != null && validBoundingBox) {
        LatLng latLng = builder.build().getCenter();
        location = "/#18/" + latLng.latitude + "/" + latLng.longitude;
    }
    leaflet.loadUrl("http://onomap.noise-planet.org" + location);
}

From source file:com.prey.activities.PanelWebActivity.java

@Override
public void onResume() {
    super.onResume();
    WebSettings settings = myWebView.getSettings();
    settings.setUseWideViewPort(true);/*from  ww  w.  j a  v  a2 s. c om*/
    settings.setLoadWithOverviewMode(true);
    settings.setJavaScriptEnabled(true);
    myWebView.setVerticalScrollBarEnabled(false);
    myWebView.setHorizontalScrollBarEnabled(false);
    myWebView.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);
        }

    });
    myWebView.setWebViewClient(new WebViewClient() {

        @Override
        public void onPageFinished(WebView view, String url) {
            PreyLogger.d("Finished:" + url);
            super.onPageFinished(view, url);
        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            PreyLogger.d("Started:" + url);
            super.onPageStarted(view, url, favicon);
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            PreyLogger.d("OverrideUrl:" + url);
            return super.shouldOverrideUrlLoading(view, url);
        }
    });

    String url = PreyConfig.getPreyConfig(getApplicationContext()).getPreyPanelJwt();

    String postData = "token=" + PreyConfig.getPreyConfig(getApplicationContext()).getTokenJwt();
    ;

    byte[] postByte = EncodingUtils.getBytes(postData, "BASE64");
    myWebView.postUrl(url, postByte);
}

From source file:com.github.akinaru.hcidebugger.activity.DescriptionActivity.java

protected void onCreate(Bundle savedInstanceState) {

    setLayout(R.layout.description_activity);
    super.onCreate(savedInstanceState);

    //setup navigation items
    setupDrawerContent(nvDrawer);//from w  w  w  .  jav  a 2s  .com

    //hide max packet count for this activity
    nvDrawer.getMenu().findItem(R.id.set_max_packet_num).setVisible(false);
    nvDrawer.getMenu().findItem(R.id.browse_file).setVisible(false);
    nvDrawer.getMenu().findItem(R.id.change_settings).setVisible(false);

    //get information sent via intent to be displayed
    String hciPacket = getIntent().getExtras().getString(Constants.INTENT_HCI_PACKET);
    String snoopPacket = getIntent().getExtras().getString(Constants.INTENT_SNOOP_PACKET);
    int packetNumber = getIntent().getExtras().getInt(Constants.INTENT_PACKET_NUMBER);
    String ts = getIntent().getExtras().getString(Constants.INTENT_PACKET_TS);
    String packet_type = getIntent().getExtras().getString(Constants.INTENT_PACKET_TYPE);
    String destination = getIntent().getExtras().getString(Constants.INTENT_PACKET_DEST);

    //setup description item table
    tablelayout = (TableLayout) findViewById(R.id.tablelayout);
    altTableRow(2);

    //setup json highlishter web page
    WebView lWebView = (WebView) findViewById(R.id.webView);

    TextView number_value = (TextView) findViewById(R.id.number_value);
    TextView ts_value = (TextView) findViewById(R.id.ts_value);
    TextView packet_type_value = (TextView) findViewById(R.id.packet_type_value);
    TextView destination_value = (TextView) findViewById(R.id.dest_value);
    number_value.setText("" + packetNumber);
    ts_value.setText(ts);
    packet_type_value.setText(packet_type);
    destination_value.setText(destination);

    WebSettings webSettings = lWebView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    int spacesToIndentEachLevel = 2;
    String beautify = "{}";
    try {
        beautify = new JSONObject(hciPacket).toString(spacesToIndentEachLevel);
    } catch (JSONException e) {
        e.printStackTrace();
    }

    String html = "<HTML><HEAD><link rel=\"stylesheet\" href=\"styles.css\">"
            + "<script src=\"highlight.js\"></script>" + "<script>hljs.initHighlightingOnLoad();</script>"
            + "</HEAD><body>" + "<pre><code class=\"json\">" + beautify + "</code></pre>" + "</body></HTML>";

    lWebView.loadDataWithBaseURL("file:///android_asset/", html, "text/html", "utf-8", null);
}

From source file:net.olejon.mdapp.MedicationNlhFragment.java

@SuppressLint("SetJavaScriptEnabled")
@Override/*from   www  .  java  2s.  c o m*/
public View onCreateView(LayoutInflater inflater, ViewGroup container, final Bundle savedInstanceState) {
    ViewGroup viewGroup = (ViewGroup) inflater.inflate(R.layout.fragment_medication_nlh, container, false);

    // Activity
    final Activity activity = getActivity();

    // Context
    final Context context = activity.getApplicationContext();

    // Tools
    final MyTools mTools = new MyTools(context);

    // Arguments
    Bundle bundle = getArguments();

    final String pageUri = bundle.getString("uri");

    // Progress bar
    final ProgressBar progressBar = (ProgressBar) activity
            .findViewById(R.id.medication_toolbar_progressbar_horizontal);

    // Toolbar
    final LinearLayout toolbarSearchLayout = (LinearLayout) activity
            .findViewById(R.id.medication_toolbar_search_layout);
    final EditText toolbarSearchEditText = (EditText) activity.findViewById(R.id.medication_toolbar_search);

    // Web view
    WEBVIEW = (WebView) viewGroup.findViewById(R.id.medication_nlh_content);

    WebSettings webSettings = WEBVIEW.getSettings();
    webSettings.setJavaScriptEnabled(true);
    webSettings.setAppCacheEnabled(true);
    webSettings.setDomStorageEnabled(true);
    webSettings.setAppCachePath(context.getCacheDir().getAbsolutePath());
    webSettings.setCacheMode(WebSettings.LOAD_DEFAULT);

    WEBVIEW.setWebViewClient(new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if (!mTools.isDeviceConnected()) {
                mTools.showToast(getString(R.string.device_not_connected), 0);
                return true;
            } else if (url.matches(".*/[^#]+#[^/]+$")) {
                WEBVIEW.loadUrl(url.replaceAll("#[^/]+$", ""));
                return true;
            } else if (url.matches("^https?://.*?\\.pdf$")) {
                mTools.downloadFile(view.getTitle(), url);
                return true;
            }

            return false;
        }
    });

    WEBVIEW.setWebChromeClient(new WebChromeClient() {
        @Override
        public void onProgressChanged(WebView view, int newProgress) {
            if (newProgress == 100) {
                progressBar.setVisibility(View.INVISIBLE);
            } else {
                progressBar.setVisibility(View.VISIBLE);
                progressBar.setProgress(newProgress);

                toolbarSearchLayout.setVisibility(View.GONE);
                toolbarSearchEditText.setText("");
            }
        }
    });

    if (savedInstanceState == null) {
        WEBVIEW.loadUrl(pageUri);
    } else {
        WEBVIEW.restoreState(savedInstanceState);
    }

    return viewGroup;
}

From source file:org.quizreader.android.PageReadActivity.java

/** Called when the activity is first created. */
@SuppressLint("SetJavaScriptEnabled")
@Override//from w  ww .j  a  v  a 2 s.  c om
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.page_read);
    // immediately pop up loading dialog
    dialog = new ProgressDialog(this);
    dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
    dialog.setMessage("loading page");
    dialog.show();
    // set up webview
    WebView webview = (WebView) findViewById(R.id.webView);
    webview.setWebViewClient(new QRWebViewClient());
    webview.setWebChromeClient(new WebChromeClient() {
        public boolean onConsoleMessage(ConsoleMessage cm) {
            Log.d("QuizReader", cm.message() + ", line " + cm.lineNumber() + " " + cm.sourceId());
            return true;
        }
    });
    WebSettings webSettings = webview.getSettings();
    webSettings.setJavaScriptEnabled(true);
    webview.addJavascriptInterface(new QuizReaderInterface(), "qr");
    try {
        random = new Random();
        wordDao = new WordDao(this);
        qzzFile = new QzzFile(title.getFilepath(), this);
        URL htmlURL = qzzFile.getHTML(title.getId(), title.getSection());
        if (htmlURL == null) { // beyond the last section
            setResult(RESULT_END_TITLE);
            finish();
        }
        String externalForm = htmlURL.toExternalForm();
        webview.loadUrl(externalForm + "?paragraph=" + title.getParagraph());
    } catch (Exception ex) {
        webview.loadData(ex.toString(), "text/plain", null);
    }
}

From source file:com.yang.bruce.mumuxi.util.WebDialog.java

private void setWebView(WebView webView, Context context) {
    WebSettings settings = webView.getSettings();
    settings.setDefaultTextEncodingName(KEY_UTF_8);
    settings.setJavaScriptEnabled(true);
    webView.addJavascriptInterface(new WebAppInterface(context), "Android");
}

From source file:com.johnny.gank.ui.activity.WebviewActivity.java

private void setUpWebView() {
    WebSettings settings = vWebView.getSettings();
    settings.setJavaScriptEnabled(true);
    settings.setLoadWithOverviewMode(true);
    vWebView.setWebViewClient(new WebViewClient() {
        @Override//from   w  w  w .  j  a v  a2  s  .c  o  m
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
            vRefreshLayout.setRefreshing(true);
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            vRefreshLayout.setRefreshing(false);
        }

        @Override
        public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
            super.onReceivedError(view, request, error);
            vRefreshLayout.setRefreshing(false);
        }
    });
    vWebView.setWebChromeClient(new WebChromeClient() {
        @Override
        public void onProgressChanged(WebView view, int newProgress) {
            super.onProgressChanged(view, newProgress);
            if (newProgress >= 80) {
                vRefreshLayout.setRefreshing(false);
            }
        }
    });
}