Circular Progressbar and Webview

1

I have a webview but it takes a few seconds to load, I want to include a progressbar while the web page loads and for some exception show a message when I can not load the webview with the content.

XML

<?xml version="1.0" encoding="utf-8"?>

<WebView

    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/websantotomas"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginLeft="516dp"
    android:layout_marginStart="516dp" />

<ProgressBar
    android:id="@+id/progressBar"
    style="?android:attr/progressBarStyleHorizontal"
    android:layout_width="150dp"
    android:layout_height="150dp"
    android:max="500"
    android:progress="0"
    android:progressDrawable="@drawable/circular"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="142dp" />

Java class

web = (WebView) findViewById(R.id.websantotomas);
    assert web != null;
web.loadUrl("http://pagina.com");
ProgressBar progressBar = (ProgressBar) findViewById(R.id.progressBar);
        ObjectAnimator animation = ObjectAnimator.ofInt (progressBar, "progress", 0, 500);
        animation.setDuration (3000);
        animation.setInterpolator (new DecelerateInterpolator());
        animation.start ();
        assert progressBar != null;
        progressBar.clearAnimation();
    
asked by Ashley G. 08.01.2017 в 19:13
source

1 answer

0

I leave you my example.

You have to define the pogressbar in my case a progressWheel in the toolar, like this:

    <LinearLayout
        android:layout_marginTop="10dp"
        android:layout_marginRight="10dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="end">
        <com.pnikosis.materialishprogress.ProgressWheel
            android:id="@+id/progress1"
            android:layout_width="35dp"
            android:layout_height="35dp"
            app:matProg_progressIndeterminate="true"
            app:matProg_barColor="@android:color/white"
            android:visibility="gone"/><!-- gone -->

    </LinearLayout>

And the activity will become visible or invisible (gone) if the site is loading

package cl.ejemplo.sitioweb.fragmentos;

import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.LinearLayout;

import com.pnikosis.materialishprogress.ProgressWheel;

import cl.ejemplo.sitioweb.R;
import cl.ejemplo.sitioweb.actividades.Root;     //activity que contiene al fragment
import cl.ejemplo.sitioweb.utilidades.Datos;

public class ejemploWeb extends Fragment {

    private final static String TAG = ">>> ejemploWeb";
    public Root fragmentActivity;
    private ProgressWheel progressPw;
    private WebView intranetWv;
    private MenuItem actualizar;
    //private Menu menu;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        fragmentActivity = (Root) super.getActivity();
        final LinearLayout myLinearLayout = (LinearLayout) inflater.inflate(R.layout.fragment_intranet, container, false);
        Log.d(">>>", TAG);

        Toolbar toolbar = (Toolbar) fragmentActivity.findViewById(R.id.toolbar);
        progressPw = (ProgressWheel) toolbar.findViewById(R.id.progress1);

        intranetWv = (WebView) myLinearLayout.findViewById(R.id.intranet_wv);
        intranetWv.getSettings().setJavaScriptEnabled(true); //Funciona con JS el sitio
        intranetWv.getSettings().setDomStorageEnabled(true);
        intranetWv.setWebViewClient(new WebViewClient(){
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                try{
                    //Log.d(TAG, "onPageStarted >>> " + url);
                    super.onPageStarted(view, url, favicon);
                    if (url != null && url.substring(url.length()-5).toLowerCase().contains(".pdf") && !url.toLowerCase().contains("docs.google.com")) {
                        intranetWv.loadUrl("https://docs.google.com/gview?embedded=true&url=" + url);
                        //https://docs.google.com/viewer?url=
                    }
                    else if (url != null && url.substring(url.length()-5).toLowerCase().contains(".xls") && !url.toLowerCase().contains("drive.google.com")) {
                        intranetWv.loadUrl("https://drive.google.com/viewerng/viewer?embedded=true&url=" + url);
                    }
                    progressPw.setVisibility(View.VISIBLE);
                    actualizar.setVisible(false);
                }
                catch(Exception e){
                    //e.printStackTrace();
                }
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                //Log.d(TAG, "onPageFinished >>> " + url);
                try {
                    progressPw.setVisibility(View.GONE);    //GONE
                    actualizar.setVisible(true);
                }catch (Exception e){
                    //e.printStackTrace();
                }
            }

            public boolean shouldOverrideUrlLoading(WebView view,String url) {
                if (url.startsWith("mailto:")) {
                    try{
                        Intent i = new Intent(Intent.ACTION_SENDTO, Uri.parse(url));
                        startActivity(i);
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                } else if (!url.contains("ejemplo.cl")) {    //Si no esta en la intranet, abre el navegador
                    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                    startActivity(intent);
                } else {
                    view.loadUrl(url);
                }
                return true;
            }

        });
        if (Build.VERSION.SDK_INT >= 21) {
            intranetWv.getSettings().setMixedContentMode( WebSettings.MIXED_CONTENT_ALWAYS_ALLOW );
        }
        //Permite Zoom en la WebView y ocultamos botones
        intranetWv.setInitialScale(150);
        intranetWv.getSettings().setBuiltInZoomControls(true);
        intranetWv.getSettings().setSupportZoom(true);
        intranetWv.getSettings().setDisplayZoomControls(false);
        intranetWv.loadUrl(getString(R.string.url_intranet));
        intranetWv.clearCache(true);

        //Poder usar el botón volver del celular en el fragment
        final Handler handler = new Handler(){
            @Override
            public void handleMessage(Message message) {
                switch (message.what) {
                    case 1:{
                        intranetWv.goBack();
                    }break;
                }
            }
        };
        intranetWv.setOnKeyListener(new View.OnKeyListener(){

            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == MotionEvent.ACTION_UP && intranetWv.canGoBack()) {
                    handler.sendEmptyMessage(1);
                    return true;
                }

                return false;
            }

        });

        return myLinearLayout;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState); // super para el menú
        setHasOptionsMenu(true);            // que tenga opciones el menú
    }

    @Override
    public void onStop() {
        super.onStop(); // super para el menú
        progressPw.setVisibility(View.GONE);    //GONE
    }

    //Definimos que iconos del menu accionan como se hace normalmente en onOptionsItemSelected y onCreateOptionsMenu //definimos el menuItem
    public void onPrepareOptionsMenu(Menu menu){
        menu.clear();
        Toolbar toolbar= (Toolbar) getActivity().findViewById(R.id.toolbar);
        toolbar.inflateMenu(R.menu.menu_intranet);
        actualizar = toolbar.getMenu().findItem(R.id.actualizar_intranet_item);
        toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem menuItem) {

                switch (menuItem.getItemId()){
                    case R.id.actualizar_intranet_item:
                        Log.d(TAG, "Reload");
                        intranetWv.reload();
                        return true;
                    default:
                        return false;
                }
            }
        });
    }
}
    
answered by 09.01.2017 / 21:11
source