Java tutorial
/******************************************************************************* * Copyright 2012 Niklas Ekman * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. *******************************************************************************/ package yet.another.hackernews.reader; import org.json.JSONException; import org.json.JSONObject; import yet.another.hackernews.reader.data.News; import yet.another.hackernews.reader.functions.ProgressFragment.OnProgressListener; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.webkit.WebChromeClient; import android.webkit.WebSettings; import android.webkit.WebView; import android.webkit.WebViewClient; import android.widget.ProgressBar; import android.widget.Toast; import com.actionbarsherlock.app.SherlockFragment; public class ViewFragment extends SherlockFragment implements OnProgressListener { private WebView menuBrowser; private static News news; public static void setNews(News news) { ViewFragment.news = news; } public static News getNews() { return ViewFragment.news; } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.layout_view, container, false); } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setRetainInstance(true); } @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); menuBrowser = (WebView) getSherlockActivity().findViewById(R.id.view_webview); final ProgressBar progressBar = (ProgressBar) getSherlockActivity().findViewById(R.id.view_progress); webviewSettings(); menuBrowser.setWebChromeClient(new WebChromeClient() { @Override public void onProgressChanged(WebView view, int progress) { // Hide dialog on finished if (progress >= 100) { progressBar.setVisibility(View.GONE); } else { progressBar.setProgress(progress); } } }); menuBrowser.setWebViewClient(new WebViewClient() { @Override public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { progressBar.setVisibility(View.GONE); Toast.makeText(getActivity().getApplicationContext(), R.string.error, Toast.LENGTH_SHORT).show(); } }); final Bundle recieveArgs = this.getSherlockActivity().getIntent().getExtras(); if (recieveArgs != null) { try { news = new News(new JSONObject(recieveArgs.getString("NewsObject"))); } catch (JSONException e) { } update(progressBar); } } public void update(ProgressBar progressBar) { if (news == null) { return; } if (progressBar == null) { progressBar = (ProgressBar) getSherlockActivity().findViewById(R.id.view_progress); } if (progressBar.getVisibility() != View.VISIBLE) { progressBar.setVisibility(View.VISIBLE); } if (menuBrowser.getProgress() < 100) { menuBrowser.stopLoading(); } menuBrowser.loadUrl(news.getURL()); } @Override public void onPause() { super.onPause(); OnProgressCancel(); } @Override public void OnProgressCancel() { if (menuBrowser.getProgress() < 100) { menuBrowser.stopLoading(); } } private void webviewSettings() { final WebSettings settings = menuBrowser.getSettings(); settings.setJavaScriptEnabled(true); } }