Display load icon whenever you click a link

Asked

Viewed 68 times

1

I’m new in Android development and am working on an app that loads a web page through a WebView. However, whenever I open a link inside this app, there is a certain delay [the time of the other page to load].

I would like a loading icon to appear whenever someone clicked on a link and it disappeared after the full page upload.

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");
            }
        });

    }
}

1 answer

0

In your Webviewclient, implement the onPageStarted and onPageFinished methods in this way:

@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’ve helped!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.