Created
March 11, 2014 20:05
-
-
Save csdear/271dc7f03329b9ae289b to your computer and use it in GitHub Desktop.
A WebView with Progress Bar / Loader
An activity that implements the webViewCLient in the onActivityCreated method A loader layout to contain the webview
Reference : http://developer.android.com/reference/android/webkit/WebView.html
See example in X: Code Repo >> UI >> Webviews Permission to access the internet. Components
ViewFragments for the …
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //1. The xml layout file | |
| <WebView | |
| android:layout_width="wrap_content" | |
| android:layout_height="wrap_content" | |
| xmlns:android="http://schemas.android.com/apk/res/android" | |
| android:id="@+id/<<webView>>"> | |
| </WebView> | |
| =================== | |
| //2. loader.xml for the progress bar. Points to a drawable "android android:indeterminateDrawable="@layout/loading_animation" | |
| <?xml version="1.0" encoding="utf-8"?> | |
| <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
| android:id="@+id/layout_root" | |
| android:layout_width="match_parent" | |
| android:layout_height="match_parent" | |
| android:orientation="vertical" | |
| android:layout_gravity="center_vertical|center_horizontal"> | |
| <ProgressBar | |
| android:layout_width="42dp" | |
| android:layout_height="42dp" | |
| //1a. Reference to loading animation layout file. | |
| android:indeterminateDrawable="@layout/<<loadingAnimationLayoutFile>>" | |
| android:layout_gravity="center_vertical|center_horizontal"/> | |
| </LinearLayout> | |
| =============================================== | |
| //2. Loading animation = loading_animation.xml | |
| <animation-list android:id="@+id/loadingAnimation" android:oneshot="false" xmlns:android="http://schemas.android.com/apk/res/android" | |
| android:layout_width="40dp" | |
| android:layout_height="40dp"> | |
| //2a. .png image files must be placed in drawable folder. | |
| <item android:drawable="@drawable/loader1" android:duration="280"/> | |
| <item android:drawable="@drawable/loader2" android:duration="280"/> | |
| <item android:drawable="@drawable/loader3" android:duration="280"/> | |
| <item android:drawable="@drawable/loader4" android:duration="280"/> | |
| </animation-list> | |
| =============================================== | |
| //3. <<nameViewFragment1>>.java. Placed in fragments package. | |
| package <<packageName>>.fragments; | |
| import android.content.Context; | |
| import android.os.Bundle; | |
| import android.support.v4.app.Fragment; | |
| import android.view.LayoutInflater; | |
| import android.view.View; | |
| import android.view.ViewGroup; | |
| import android.view.WindowManager; | |
| //3a. important : include WebView imports | |
| import android.webkit.WebView; | |
| import android.webkit.WebViewClient; | |
| import org.bcbsal.healthhandbook.R; | |
| //3b. important ProgressDialog import | |
| import android.app.ProgressDialog; | |
| /** | |
| * Created by stanw on 2/17/14. | |
| */ | |
| public class <<nameViewFragment1>> extends Fragment { | |
| //3c. Fields for Viewgroup, Context and ProgressDialog | |
| ViewGroup vg; | |
| private Context myContext; | |
| private ProgressDialog pd; | |
| //OPTION : you can set a field with a URL as well. | |
| //3d. Invoke newInstance() method of <<nameViewFragment1>> that returns a fragment object. | |
| public static <<nameViewFragment1>> newInstance() { | |
| <<nameViewFragment1>> fragment = new <<nameViewFragment1>>(); | |
| return fragment; | |
| } | |
| public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){ | |
| vg = container; | |
| //3e. Inflate view layout | |
| View v = inflater.inflate(R.layout.<<XML_LayoutToInflate>>, container, false); | |
| myContext = v.getContext(); | |
| return v; | |
| } | |
| @Override | |
| public void onActivityCreated(Bundle savedInstanceState) { | |
| super.onActivityCreated(savedInstanceState); | |
| pd = ProgressDialog.show(myContext,null,null); | |
| //3f. Set Content view with progress dialog loader XML loader layout | |
| pd.setContentView(R.layout.<<loaderXML_Layout>>); | |
| WindowManager.LayoutParams lp = pd.getWindow().getAttributes(); | |
| lp.dimAmount=0.0f; | |
| pd.getWindow().setAttributes(lp); | |
| pd.show(); | |
| //3g. Create a new instance of WebView, assign a XMLlayoutview from | |
| // the webview's id used in the inflated view above ( <<XML_LayoutToInflate>>) | |
| WebView wv = (WebView) getView().findViewById(R.id.<<webView_ID>>); | |
| wv.getSettings().setJavaScriptEnabled(true); | |
| wv.setWebViewClient(new WebViewClient() { | |
| @Override | |
| public void onPageFinished(WebView view, String url) { | |
| super.onPageFinished(view, url); | |
| pd.dismiss(); | |
| } | |
| }); | |
| //3h. URL to load the webpage content you want to display | |
| //E.G., wv.loadUrl("https://bcbsal.qualtrics.com/SE/?SID=SV_9ZWkc3BCMVyqrat"); | |
| wv.loadUrl("<<urlString>>"); | |
| } | |
| } | |
| ================================= | |
| // 4. Another varient fragment example, from BCBS fragment. ViewArticleFragment.java | |
| package <<packageName>>.fragments; | |
| import android.content.Context; | |
| import android.support.v4.app.Fragment; | |
| import android.os.Bundle; | |
| import android.view.LayoutInflater; | |
| import android.view.View; | |
| import android.view.ViewGroup; | |
| import android.view.WindowManager; | |
| //4a. Webkit and ProgressDialog imports. | |
| import android.webkit.WebView; | |
| import android.webkit.WebViewClient; | |
| import org.bcbsal.healthhandbook.R; | |
| import android.app.ProgressDialog; | |
| /** | |
| * Created by stanw on 2/17/14. | |
| */ | |
| public class <<rootFragmentClassName>> extends Fragment { | |
| ViewGroup vg; | |
| private Context myContext; | |
| private ProgressDialog pd; | |
| //4b. Extra field, string 'currentString' | |
| private String currentString; | |
| public static <<rootFragmentClassName>> newInstance() { | |
| <<rootFragmentClassName>> fragment = new <<rootFragmentClassName>>(); | |
| return fragment; | |
| } | |
| public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){ | |
| //4c. Inflate view of the container | |
| vg = container; | |
| View v = inflater.inflate(R.layout.<<inflatedViewContainer>>, container, false); | |
| myContext = v.getContext(); | |
| currentString = this.getArguments().getString("stringXML"); | |
| return v; | |
| } | |
| @Override | |
| public void onActivityCreated(Bundle savedInstanceState) { | |
| super.onActivityCreated(savedInstanceState); | |
| //4d. Set Content view to progress loader's xml layoutfile | |
| pd = ProgressDialog.show(myContext,null,null); | |
| pd.setContentView(R.layout.<<XML_LayoutLoader>>); | |
| WindowManager.LayoutParams lp = pd.getWindow().getAttributes(); | |
| lp.dimAmount=0.0f; | |
| pd.getWindow().setAttributes(lp); | |
| pd.show(); | |
| //4e. Set getView with id of webView element within the above inflated view. | |
| WebView wv = (WebView) getView().findViewById(R.id.<<InflatedViewContainerWebviewID>> | |
| ); | |
| wv.getSettings().setJavaScriptEnabled(true); | |
| //4f. Set onPageFinished parameters --"WebView view, String url" | |
| wv.setWebViewClient(new WebViewClient() { | |
| @Override | |
| public void onPageFinished(WebView view, String url) { | |
| super.onPageFinished(view, url); | |
| pd.dismiss(); | |
| } | |
| }); | |
| //4g. Variant : loadDataWithBaseURL method | |
| //EG. wv.loadDataWithBaseURL("file:///android_res/raw/.", currentString, "text/html", "UTF //-8", null); | |
| wv.loadDataWithBaseURL("<<urlParameter>>", currentString, "text/html", "UTF-8", null); | |
| } | |
| } | |
| ==================================================== | |
| //5. AndroidManifest.xml | |
| <uses-permission android:name="android.permission.INTERNET"/> | |
| =================================== | |
| //OPTIONS | |
| //1. Load from an HTML string: | |
| String summary = "<html><body>You scored <b>192</b> points.</body></html>"; | |
| webview.loadData(summary, "text/html", null); | |
| // ... although note that there are restrictions on what this HTML can do. | |
| // See the JavaDocs for loadData() and loadDataWithBaseURL() for more info | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment