+ 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) {
+ formattedUrlString = url;
+ urlTextBox.setText(formattedUrlString);
+ mainWebView.loadUrl(formattedUrlString);
+ return true;
+ }
+
+ @Override
+ public void onPageFinished(WebView view, String url) {
+ // Update the URL in urlTextBox. It is necessary to do this after the page finishes loading to get the final URL, which can change during load.
+ formattedUrlString = mainWebView.getUrl();
+ 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);