+ progressBar = (ProgressBar) findViewById(R.id.progressBar);
+ favoriteIcon = (ImageView) findViewById(R.id.favoriteIcon);
+
+ // Remove the title from the action bar.
+ final ActionBar actionBar = getSupportActionBar();
+ if (actionBar != null) {
+ actionBar.setDisplayShowTitleEnabled(false);
+ // actionBar.setHideOnContentScrollEnabled(true);
+ }
+
+ // Implement swipe down to refresh.
+ swipeToRefresh.setColorSchemeColors(0xFF0097FF);
+ swipeToRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
+ @Override
+ public void onRefresh() {
+ mainWebView.loadUrl(formattedUrlString);
+ }
+ });
+
+ // Only enable swipeToRefresh if is mainWebView is scrolled to the top.
+ mainWebView.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
+ @Override
+ public void onScrollChanged() {
+ if (mainWebView.getScrollY() == 0) {
+ swipeToRefresh.setEnabled(true);
+ } else {
+ swipeToRefresh.setEnabled(false);
+ }
+ }
+ });
+
+ mainWebView.setWebViewClient(new WebViewClient() {
+
+ // setWebViewClient makes this WebView the default handler for URLs inside the app, so that links are not kicked out to other apps.
+ // Save the URL to formattedUrlString and update urlTextBox before loading mainWebView.
+ @Override
+ public boolean shouldOverrideUrlLoading(WebView view, String url) {
+ mainWebView.loadUrl(url);
+ return true;
+ }
+
+ // Update the URL in urlTextBox when the page starts to load.
+ @Override
+ public void onPageStarted(WebView view, String url, Bitmap favicon) {
+ urlTextBox.setText(url);
+ }
+
+ // Update formattedUrlString and urlTextBox. It is necessary to do this after the page finishes loading because the final URL can change during load.
+ @Override
+ public void onPageFinished(WebView view, String url) {
+ formattedUrlString = url;
+ urlTextBox.setText(formattedUrlString);
+ }
+ });
+
+ mainWebView.setWebChromeClient(new WebChromeClient() {
+
+ // Update the progress bar when a page is loading.
+ @Override
+ public void onProgressChanged(WebView view, int progress) {
+ progressBar.setProgress(progress);
+ if (progress < 100) {
+ progressBar.setVisibility(View.VISIBLE);
+ } else {
+ progressBar.setVisibility(View.GONE);
+
+ // Stop the refreshing indicator if it is running.
+ swipeToRefresh.setRefreshing(false);
+ }
+ }
+
+ // Set the favorite icon when it changes.
+ @Override
+ public void onReceivedIcon(WebView view, Bitmap icon) {
+ favoriteIcon.setImageBitmap(Bitmap.createScaledBitmap(icon, 64, 64, true));
+ }
+ });
+
+ // Set the "go" button on the keyboard to load the URL.
+ urlTextBox.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, load the URL.
+ if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
+ (keyCode == KeyEvent.KEYCODE_ENTER)) {
+ // Load the URL into the mainWebView and consume the event.
+ try {
+ loadUrlFromTextBox(mainWebView);
+ } catch (UnsupportedEncodingException e) {
+ e.printStackTrace();
+ }
+ // If the enter key was pressed, consume the event.
+ return true;
+ }
+ // If any other key was pressed, do not consume the event.
+ return false;
+ }
+ });