Progress bar Does not work

Asked

Viewed 364 times

0

People I have two threads, one that accesses the webview with the url and the other responsible for progressibar, but the Progress bar is not running. What can it be and how to fix it ?

import android.os.SystemClock;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.net.ConnectivityManager;
import android.content.Context;
import android.net.NetworkInfo;
import android.net.Network;
import android.widget.ProgressBar;

public class ConectActivity extends Activity {


    private WebView webView;
    private ProgressBar progress;

    public  boolean verificaConexao() {
        boolean conectado;
        ConnectivityManager conectivtyManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        if (conectivtyManager.getActiveNetworkInfo() != null
                && conectivtyManager.getActiveNetworkInfo().isAvailable()
                && conectivtyManager.getActiveNetworkInfo().isConnected()) {
            conectado = true;
        } else {
            conectado = false;
        }
        return conectado;
    }

    Boolean  conect;
    String  url = "http://google.com";

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_conect);
        // *** roda qnd abre - Augusto Furlan ***
        conect = verificaConexao();
        webView = (WebView) findViewById(R.id.webView);
        progress = (ProgressBar) findViewById(R.id.progress);
        //webView.setWebViewClient(new CustomWebViewClient());
        webView.setVisibility(webView.GONE);

        WebSettings ws = webView.getSettings();
        ws.setJavaScriptEnabled(true);
        ws.setSupportZoom(true);
        webView.setWebViewClient(new WebViewClient());



        new Thread(new Runnable() {
            @Override
            public void run() {

                SystemClock.sleep(10000);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        showWebView();
                    }
                });
            }
        }).start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                if(conect == true) {
                    webView.loadUrl(url);
                } else {webView.loadUrl("file:///android_asset/not-found.html");;}

                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        showWebView();
                    }
                });
            }
        }).start();
        }



    private void showWebView() {
        webView.setVisibility(View.VISIBLE);
        progress.setVisibility(View.GONE);
    }

}

1 answer

2


In fact, you have 3 threads. In the second thread you create (remembering that the Main Thread, responsible for the interface, is created automatically when creating the application process, so this is the second you explicitly created), this:

new Thread(new Runnable() {
            @Override
            public void run() {
                if(conect == true) {
                    webView.loadUrl(url);
                } else {webView.loadUrl("file:///android_asset/not-found.html");;}

                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        showWebView();
                    }
                });
            }
        }).start();
        }

You are calling the method that removes the Progress, that:

 private void showWebView() {
        webView.setVisibility(View.VISIBLE);
        progress.setVisibility(View.GONE);
    }

Since this thread is created on the Activity onCreate (when it is not yet visible), you are probably removing the Progress before you can even see it on the screen.

  • friend, there was no error in the code, but it continues without displaying the pre-loader

  • the current code is http://pastebin.com/ZryMVMJX

  • I know you are not in error as there really are no errors. The problem is that you remove progressBar from the screen, right at the start of Activity. Then you don’t even get to see it. Comment the following code: // progress.setVisibility(View.GONE); And then you will see that the Progress will be shown on the screen. As long as in xml it is marked as visible.

  • When commenting the Preview is eternally running.

  • Friend solved, obg

  • I’m glad you solved it. Anything, we’re there!

Show 1 more comment

Browser other questions tagged

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