6

I developed a website for my entreprisee and I work almost exclusively with PHP

So the Java language (and android studio) is a really new for me

Despite this I have to create an APK to use the website (in order to block the android home on this site)

For the moment I managed to set up refresh pages, and now I am trying to add a progress/loading bar to my application that uses WebView but it doesn't work ..

MainActivity.java

import android.support.v4.widget.SwipeRefreshLayout;
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends Activity {

    WebView webView;

    SwipeRefreshLayout swipe;

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

        swipe = (SwipeRefreshLayout) findViewById(R.id.swipe);
        swipe.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                webView.reload();
            }
        });

        LoadWeb();

    }


    public void LoadWeb(){

        webView = (WebView) findViewById(R.id.webView);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setAppCacheEnabled(true);
        webView.loadUrl("https://www.google.com/");
        swipe.setRefreshing(true);
        webView.setWebViewClient(new WebViewClient(){

            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {

                webView.loadUrl("file:///android_asset/error.html");
            }

            public  void onLoadResource(WebView view, String url) { //Doesn't work
                swipe.setRefreshing(true);
            }

            public  void  onPageFinished(WebView view, String url){

                //Hide the SwipeReefreshLayout

                swipe.setRefreshing(false);
            }

        });
    }

    @Override
    public void onBackPressed(){

        if (webView.canGoBack()){
            webView.goBack();
        }else {
            finish();
        }
    }
}

Activity_main.xml

<?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="eu.test.testappli.MainActivity">

    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/swipe"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <WebView
            android:id="@+id/webView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>

    </android.support.v4.widget.SwipeRefreshLayout>

</android.support.constraint.ConstraintLayout>

It may seem simple, but the java remains obscure for me so I can not implement the pieces of code that I find on the web ..

Rocstar
  • 1,427
  • 3
  • 23
  • 41

5 Answers5

11

Try this i have make some changes in your code

import android.support.v4.widget.SwipeRefreshLayout;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
import android.support.v7.widget.Toolbar;
public class Main2Activity extends AppCompatActivity {

    WebView webView;

    SwipeRefreshLayout swipe;
    ProgressBar progressBar;
    Toolbar toolbar;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        progressBar = (ProgressBar) findViewById(R.id.awv_progressBar);
        swipe = (SwipeRefreshLayout) findViewById(R.id.swipe);
        LoadWeb();

        progressBar.setMax(100);
        progressBar.setProgress(1);


        swipe = (SwipeRefreshLayout) findViewById(R.id.swipe);
        swipe.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                webView.reload();
            }
        });

        webView.setWebChromeClient(new WebChromeClient() {
            public void onProgressChanged(WebView view, int progress) {


                progressBar.setProgress(progress);
            }
        });

        webView.setWebViewClient(new WebViewClient() {


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


            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {

                webView.loadUrl("file:///android_asset/error.html");
            }

            public void onLoadResource(WebView view, String url) { //Doesn't work
                //swipe.setRefreshing(true);
            }

            public void onPageFinished(WebView view, String url) {

                //Hide the SwipeReefreshLayout
                progressBar.setVisibility(View.GONE);
                swipe.setRefreshing(false);
            }

        });


    }

    public void LoadWeb() {

        webView = (WebView) findViewById(R.id.webView);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setAppCacheEnabled(true);
        webView.loadUrl("https://www.google.com/");
        swipe.setRefreshing(true);
    }

    @Override
    public void onBackPressed() {

        if (webView.canGoBack()) {
            webView.goBack();
        } else {
            finish();
        }
    }

}

XML.LAYOUT

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:id="@+id/rel_parent"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".Main2Activity">



    <ProgressBar
        android:id="@+id/awv_progressBar"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="match_parent"
        android:layout_height="15dp"
        android:layout_marginTop="7dp"
        android:indeterminate="true" />


    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/swipe"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <WebView
            android:id="@+id/webView"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

    </android.support.v4.widget.SwipeRefreshLayout>


</LinearLayout>

add this theme to your activity

<style name="AppTheme3" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

OUTPUT

enter image description here

AskNilesh
  • 67,701
  • 16
  • 123
  • 163
  • Thank you but what should I import? Because I have lot of error – Rocstar Dec 19 '17 at 11:20
  • Thank you for your help ! But I don't have the same result. In fcat, I have the title bar and I don't show the progress bar – Rocstar Dec 19 '17 at 11:28
  • 1
    @Rocstar can explain exact problem because it working in my device – AskNilesh Dec 19 '17 at 11:30
  • When I change page (for example when I do a research) I don't have the progress bar so if the network is slow the user does not understand why it does not work – Rocstar Dec 19 '17 at 11:33
  • 1
    I'm in a meeting I tell you that in twenty minutes, thank you very much – Rocstar Dec 19 '17 at 12:01
  • "Unfortunaly, Chrome has spotted" I have this error when I run the app in my Android Emulator, how I can see the exacte error ? Thank you – Rocstar Dec 19 '17 at 12:34
  • We are almost there ! I just have a bar with Nilesh written – Rocstar Dec 19 '17 at 12:47
  • you want to remove that @Rocstar – AskNilesh Dec 19 '17 at 12:47
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/161527/discussion-between-rocstar-and-nilu). – Rocstar Dec 19 '17 at 13:04
  • Thank you ! I have one last question, how can I prevent the page from going down (then back) to each load? I modified View.VISIBLE in onPageFinished but that simulate a load, I would just like a band – Rocstar Dec 19 '17 at 13:29
  • 1
    **EDIT :** t's necessary to put progressBar.setVisibility(View.INVISIBLE); Thank you for all your help @Nilu – Rocstar Dec 19 '17 at 13:45
0

This may help you.

Inside WebViewClient:

  @Override
     public void onPageStarted(WebView view, String url, Bitmap favicon) {

      super.onPageStarted(view, url, favicon);
      findViewById(R.id.progress1).setVisibility(View.VISIBLE);
     }

    @Override
    public void onPageFinished(WebView view, String url) {
        findViewById(R.id.progress1).setVisibility(View.GONE);
    }

In the XML add this :

<ProgressBar
    android:id="@+id/progress1"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
islamdidarmd
  • 725
  • 5
  • 19
0

I think that you best option to do that, it's use Cordova. Include your web in there and compile it like an Android app.

pablofcn
  • 111
  • 1
  • 4
0

You need to set ProgressBar over the WebView, take look my code I have did it.

This is my activity_main.xml

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

    <WebView
        android:id="@+id/webView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerHorizontal="true" />

    <ProgressBar
        android:id="@+id/progressBar1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true" />
</RelativeLayout>

In MainActivity.java I had handled ProgressBar show/hide functionality.

MainActivity.java

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

        webview = findViewById(R.id.webView1);
        pbar =  findViewById(R.id.progressBar1);
        webview.setWebViewClient(new WebViewClient());
        webview.loadUrl("https://www.ndtv.com/");
    }

    public class WebViewClient extends android.webkit.WebViewClient {
        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);

            /*** Hide ProgressBar while page completely load ***/
            pbar.setVisibility(View.GONE);

        }

    }

Check a result,

ProgressBar visible while page being load

enter image description here

ProgressBar will hide while URL completely load

enter image description here

Fenil Patel
  • 1,528
  • 11
  • 28
0

In XML File :-

<ProgressBar
    android:id="@+id/mProgressBar"
    style="@style/Base.Widget.AppCompat.ProgressBar.Horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

In MainActivity :-

        webView.setWebChromeClient(new WebChromeClient(){
        @Override
        public void onProgressChanged(WebView view, int newProgress) {
            super.onProgressChanged(view, newProgress);
            mProgressBar.setProgress(newProgress);
        }

        webView.setWebViewClient(new WebViewClient(){
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String request)
        {
            webView.loadUrl(request);
            return true;
        }
        public void onPageFinished(WebView view, String url) {
            mProgressBar.setVisibility(View.GONE);
            super.onPageFinished(view, url);
        }

Here you get the complete webview code. https://github.com/Talha609/Android-WebView-PDF-ProgressBar

Mohd Talha
  • 17
  • 4