Display loading icon whenever they click on a link

1

I'm new to Android development and I'm working on an application that loads a webpage through a WebView . However, whenever I open a link within this app, there is a certain delay.

I would like a loading icon to be displayed whenever someone clicked on a link and it disappears after full page loading.

My codes are these:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="ga.traveltreasure.traveltreasureparty.MainActivity"
    android:background="#000000">

<WebView
    android:id="@+id/ttpWv"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentBottom="true"
    android:layout_alignParentEnd="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentTop="true"
    tools:layout_editor_absoluteX="8dp"
    android:background="#fc466b"
    app:layout_constraintBottom_toBottomOf="parent">
</WebView>

</android.support.constraint.ConstraintLayout>

-

package ga.traveltreasure.traveltreasureparty;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Handler;
import android.os.StrictMode;
import android.support.annotation.StringDef;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.webkit.SslErrorHandler;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.Toast;
import java.net.InetAddress;
import java.net.UnknownHostException;

public class MainActivity extends AppCompatActivity {

    WebView webView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        getSupportActionBar().hide();

        final WebView wv = (WebView) findViewById(R.id.ttpWv);

        WebSettings ws = wv.getSettings();
        ws.setJavaScriptEnabled(true);
        ws.setSupportZoom(false);




        wv.loadUrl("LINK-PARA-A-PÁGINA");



        wv.setWebViewClient(new WebViewClient()
        {
            public void onReceivedError(WebView webview, int i, String s, String s1)
            {
                wv.loadUrl("file:///android_asset/error.html");
            }
        });

    }
}
    
asked by anonymous 08.10.2017 / 23:35

1 answer

0

In your WebViewClient, implement the onPageStarted and onPageFinished methods, like this:

@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
    super.onPageStarted(view, url, favicon);
    // Aqui você EXIBE o ícone carregando, por exemplo um ProgressBar
}

@Override
public void onPageFinished(WebView view, String url) {
    super.onPageFinished(view, url);
    // Aqui você ESCONDE o ícone carregando, por exemplo um ProgressBar
}

I hope I have helped!

    
19.09.2018 / 23:52