-1

I am first in Android (Java) and I want to get the request response by blocking the user screen with a Progressbar while it ends.

Intent intent= new Intent(iniciarSesion.this, MainActivity.class);
            intent.putExtra("id",id);
            intent.putExtra("nom_usu",nom_usu);
            intent.putExtra("equipo",equipo);
            intent.putExtra("password",password);
            startActivity(intent);





   if (equipo.equals("S")) {
  añadirequipo x =  new añadirequipo();
  x.nombreCancha(id,getApplicationContext());  }



public void nombreCancha(String idUsuario,Context mContext) {


String URL="url"+idUsuario+"";
    JsonArrayRequest jsonArrayRequest=new JsonArrayRequest(URL, new Response.Listener<JSONArray>() {
        @Override
        public void onResponse(JSONArray response) {
            JSONObject jsonObject = null;


                for (int i = 0; i < response.length(); i++) {
                    try {
                        jsonObject = response.getJSONObject(i);

                        id_equipo = jsonObject.getString("idequipo");
                        id_usu = jsonObject.getString("idusuario");
                        nombre_equipo = jsonObject.getString("nombre_equipo");
                        cancha = jsonObject.getString("cancha");

                    } catch (JSONException e) {
                        Toast.makeText(mContext, e.getMessage(), Toast.LENGTH_SHORT).show();
                        Log.v("Fallo_Json:", e.getMessage());
                    }
                }


        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            if (error.toString().equals("com.android.volley.ParseError: org.json.JSONException: Value null of type org.json.JSONObject$1 cannot be converted to JSONArray")){

                Toast.makeText(mContext, "No existe ese usuario", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(mContext, "Fallo json"+error.getMessage(), Toast.LENGTH_SHORT).show();
                Log.v ("Fallo_Listener:", error.getMessage());
            }


        }
    }
    );

    requestQueue= Volley.newRequestQueue(mContext);
    requestQueue.add(jsonArrayRequest);

}

The nombreCancha method sets a String field variable that is then retrieved in a fragment

public String esperarNombre() {
return cancha;

}

What happens is that before the fragment of the response is finished, I want the application to stop while the response is over and place a Progressbar while it ends.

I know that the Request an async thread and I don't want to do it sync I want the application to wait for that to end that with a loading bar.

Note 1:

Hello

Toast.makeText(getApplicationContext(), "Bienvenido "+nom_usu+" ", Toast.LENGTH_SHORT).show();
                Intent intent= new Intent(iniciarSesion.this, MainActivity.class);
                intent.putExtra("id",id);
                intent.putExtra("nom_usu",nom_usu);
                intent.putExtra("equipo",equipo);
                intent.putExtra("password",password);
                startActivity(intent);
                if (equipo.equals("S")) {
                    loader.setVisibility(VISIBLE);
                    getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE,
                            WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
                    añadirequipo x =  new añadirequipo();
                    x.nombreCancha(id,getApplicationContext(),loader);

                }



public void onResponse(JSONArray response) {
                JSONObject jsonObject = null;


                    for (int i = 0; i < response.length(); i++) {
                        try {
                            jsonObject = response.getJSONObject(i);

                            id_equipo = jsonObject.getString("idequipo");
                            id_usu = jsonObject.getString("idusuario");
                            nombre_equipo = jsonObject.getString("nombre_equipo");
                            cancha = jsonObject.getString("cancha");

                        } catch (JSONException e) {
                            Toast.makeText(mContext, e.getMessage(), Toast.LENGTH_SHORT).show();
                            Log.v("Fallo_Json:", e.getMessage());
                        }
                    }

               // progressDialog.dismiss();
                loader.setVisibility(GONE);
                getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
            }

give my a error : Attempt to invoke virtual method 'void android.view.Window.clearFlags(int)' on a null object reference

1 Answers1

1

Use Progress bar in your xml since ProgressDailog is deprictaed now

 <ProgressBar
        android:id="@+id/loader"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:indeterminateTintMode="src_atop"
        android:indeterminateTint="@android:color/black"
        android:layout_gravity="center" />

Then while making network call set flag to the window which will prevent the user interaction

loader.visibility = VISIBLE
getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE,
                           WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);

Then after completion of the network call clear the flags

loader.visibility = GONE
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
Sharath kumar
  • 4,064
  • 1
  • 14
  • 20