Search code examples
androidandroid-layoutandroid-fragmentsandroid-webviewwebviewclient

Android Studio: How to set a webview loading and error view?


I am working on an application for my study purpose , the app contain a navigation drawer and some webviews. my problem is when the page is loading, the screen still white and boring. and if the connection failed the ugly "no connexion" page appear showing my host adresse.. I want to set a loading page or image and an other one for alternative scenario(connexion fail, ...)i tried some solutions but no one of then suite my code.

the is the fragment JAVA and XML code..

layout1_acceuil.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent">

    <WebView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/webView1"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />

</RelativeLayout>​

menu1_acceuil.java

package xxxx.yyyyyy;

import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

/**
 * Created by Malek Boubakri on 13/03/2015.
 */
public class menu1_accueil extends Fragment {
    View rootview;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle setInitialSavedState) {
        rootview = inflater.inflate(R.layout.layout1_accueil, container, false);
        //set up webview
        WebView webView1 = (WebView) rootview.findViewById(R.id.webView1);
        // Enable Javascript
        WebSettings webSettings = webView1.getSettings();
        webSettings.setJavaScriptEnabled(true);
        //load an URL
        webView1.loadUrl("http://myhost.tn");
        // Force links and redirects to open in the WebView instead of in a browser
        webView1.setWebViewClient(new WebViewClient());
        return rootview;
    }
}

may my problem can seen easy for some genuis but am just a beginer and if this app dont run it will effect my mark badly :'( please post any information can help me and i will be thankful. (Again,I have to mention that i still beginner in android dev and my English may be somehow bad..)


Solution

  • You can perform any action before and after the webpage is loaded with the class WebViewClient. You can override method like onPageStarted(), onPageFinished() and onReceivedError().

    This is the android developer site for the WebViewClient, you can find all the information here: http://developer.android.com/reference/android/webkit/WebViewClient.html