user2386226
user2386226

Reputation:

How do I add a rotating spinner to my webview?

I have made a webview in which I require to add a loading /rotating spinner. I have the image for the same, but I am not sure how do I go about it such that when the webview loads it starts rotating and when the entire view is loaded it stops. Here is my code so far:

 @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_search_answers, container, false);
      mWebview =  (WebView)view.findViewById(R.id.webview);


        mWebview.setVisibility(View.VISIBLE);


            mWebview.setWebViewClient(new MyWebViewClient(this, mWebview));
            mWebview.getSettings().setPluginState(PluginState.ON);
            mWebview.getSettings().setUseWideViewPort(true);
            mWebview.getSettings().setDefaultZoom(ZoomDensity.FAR);
            mWebview.getSettings().setBuiltInZoomControls(true);
            mWebview.getSettings().setSupportZoom(true);
            mWebview.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
            mWebview.getSettings().setAllowFileAccess(true);
            mWebview.getSettings().setDomStorageEnabled(true);
            mWebview.getSettings().setJavaScriptEnabled(true);
            mWebview.setHttpAuthUsernamePassword(HOST, REALM, USERNAME, PASSWORD);
            mWebview.getSettings().setAppCacheEnabled(true);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
                mWebview.getSettings().setDisplayZoomControls(false);

            mWebview.loadUrl(URL);


        return view;
    }

P.S:I would like to add constant animated rotation. Here is the code for rotate.xml I added under animations:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <rotate
        android:fromDegrees="180.0"
        android:toDegrees="0.0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:duration="200"
    />
</set>

Any idea how I can connect my webview to the imae and ultimately to rotate.xml so it meets the expected criteria?

Thanks! Justin

Upvotes: 1

Views: 8027

Answers (1)

user2608661
user2608661

Reputation:

The solution is easy: Below this line:

View view = inflater.inflate(R.layout.fragment_search_answers, container, false);
      mWebview =  (WebView)view.findViewById(R.id.webview);

add:

mPbar = (ProgressBar) view.findViewById(R.id.web_view_progress);

after setting it in the title as:

private ProgressBar mPbar = null;

And then go to your web view client section and add :

@Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
mPbar.setVisibility(View.VISIBLE);
}

and :

public void onPageFinished(WebView view, String url) {
mPbar.setVisibility(View.GONE);
        }

ad do not forget to set <progressbar with style and required width height below <webview> in xml and it will do wonders.

Upvotes: 8

Related Questions