webView is left blank when I try to use Android Studio progress bar

0

I'm trying to make an app and in one of the screens I load a page and in turn I send the latitude and longitude of the phone. The problem is that it is half slow to load so I would like to put a progress bar or something that indicates that it is loading and not that the phone was labeled or failed the app. My code is as follows.

public class yaviene extends Activity {
    double latitud = 0.0;
    double longitud = 0.0;
    boolean Primeraubicacion = true;
    @SuppressLint({"CutPasteId", "SetJavaScriptEnabled"})
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.yaviene);
       locationStart();
    }
    private void locationStart() {
        LocationManager mlocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        Localizacion Local = new Localizacion();
        Local.setYaviene(this);
        assert mlocManager != null;
        final boolean gpsEnabled = mlocManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
        if (!gpsEnabled) {
            Intent settingsIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
            startActivity(settingsIntent);
        }
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION,}, 1000);
            return;
        }
        mlocManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, (LocationListener) Local);
        mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, (LocationListener) Local);
    }
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        if (requestCode == 1000) {
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                locationStart();
            }
        }
    }



     //LLAMA AL SERVIDOR PARA CARGAR EL WEB VIEW (asi quedó segun entendí lo que me comentò Bruno)
    @SuppressLint("SetJavaScriptEnabled")
    private void Cargarwebview(double latitud,double longitud) throws MalformedURLException {

        final ProgressDialog progressDialog;
        progressDialog = new ProgressDialog(this);
        progressDialog.show();
        progressDialog.setContentView(R.layout.progressbar);
        progressDialog.setCancelable(false);
        progressDialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));

        WebView myWebView = (WebView) findViewById(R.id.webView);
        WebSettings webSettings = myWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        myWebView.setWebViewClient(new WebViewClient());
       // myWebView.loadUrl("http://www.yaviene.com/usuariocontrol/index.php?lat="+latitud+"&Lng="+longitud);
        Log.e("scan","longitud"+longitud+"latitud"+latitud);
        myWebView.loadUrl("http://www.yaviene.com/usuariocontrol/index.php?lat="+latitud+"&Lng="+longitud);
        progressDialog.dismiss();
    }
//============================================================//


    @SuppressLint({"ShowToast","SetTextI18n"})
    public void setLocation(Location loc) {
        if (loc.getLatitude() != 0.0 && loc.getLongitude() != 0.0) {
          latitud  = loc.getLatitude();
          longitud = loc.getLongitude();
            try {
                Geocoder geocoder = new Geocoder(this, Locale.getDefault());
                List<Address> list = geocoder.getFromLocation(
                        loc.getLatitude(), loc.getLongitude(), 1);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    /* Aqui empieza la Clase Localizacion */
   public class Localizacion implements LocationListener {
        yaviene yaviene;
        public yaviene getYaviene() {
            return yaviene;
        }
        public void setYaviene(yaviene yaviene) {
            this.yaviene = yaviene;
        }
        @SuppressLint("SetJavaScriptEnabled")
        @Override
        public void onLocationChanged(Location loc) {
        // Este metodo se ejecuta cada vez que el GPS recibe nuevas coordenadas
        // debido a la deteccion de un cambio de ubicacion
            latitud  = loc.getLatitude();
            longitud = loc.getLongitude();
        //CONSULTA SI ES LA PRIMERA UBICACION QUE TOMA EL GPS AL PRESIONAL EL BOTON YA VIENE Y LA ENVÍA, CASO CONTRARIO NO INGRESA
            if (Primeraubicacion){
                this.yaviene.setLocation(loc);
                try {

                    //CargarMapaGPS(latitud,longitud);
                    Cargarwebview(latitud,longitud);

                } catch (MalformedURLException e) {
                    e.printStackTrace();
                }
               // enviarcoordenadasGET(Latitud,Longitud);
                Primeraubicacion = false;
            }
        }
        @Override
        public void onProviderDisabled(String provider) {
// Este metodo se ejecuta cuando el GPS es desactivado
           // mensaje1.setText("GPS Desactivado");
        }
        @Override
        public void onProviderEnabled(String provider) {
// Este metodo se ejecuta cuando el GPS es activado
            Log.e("scan","ACTIVADO"+latitud);
        }
        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
            switch (status) {
                case LocationProvider.AVAILABLE:
                    Log.d("debug", "LocationProvider.AVAILABLE");
                    //break;
                case LocationProvider.OUT_OF_SERVICE:
                    Log.d("debug", "LocationProvider.OUT_OF_SERVICE");
                    //break;
                case LocationProvider.TEMPORARILY_UNAVAILABLE:
                    Log.d("debug", "LocationProvider.TEMPORARILY_UNAVAILABLE");
                    //break;
            }
        }
    }
    
asked by Luis 03.05.2018 в 20:38
source

1 answer

0

To put a rotating progressDialog you can do something like that

final ProgressDialog progressDialog;
        progressDialog = new ProgressDialog(this);
        progressDialog.show();
        progressDialog.setContentView(R.layout.progressbar);
        progressDialog.setCancelable(false);
        progressDialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));

Where the layout progressbar is:

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/transparent"
    >
    <ProgressBar
        android:id="@+id/progressBar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:background="@android:color/transparent"/>
</LinearLayout>

To close it, since you set it is not possible to cancel, you must do

progressDialog.dismiss();
    
answered by 03.05.2018 / 20:52
source