I am using multiple Fragments in tabbed activity to show json data.
I want to show progress bar whenever the response is received in every fragment.
private void loadJSON() {
  Retrofit retrofit = new Retrofit.Builder()
                                  .baseUrl(BASEURL)
                                  .addConverterFactory(GsonConverterFactory.create())
                                  .build();
  newsAPI = retrofit.create(NewsAPI.class);
  Call<JSONResponse> call = newsAPI.topNews("soure", "api-key");
  call.enqueue(new Callback<JSONResponse>() {
    @Override
    public void onResponse(Call<JSONResponse> call, Response<JSONResponse> response) {
      Log.v("this", "Yes!");
    }
    @Override public void onFailure(Call<JSONResponse> call, Throwable t) {
      Log.v("this", "No Response!");
    }
  });
}
With something like this, using progressDialog :
private void loadJSON() {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(BASEURL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        newsAPI = retrofit.create(NewsAPI.class);
        Call < JSONResponse > call =
                newsAPI.topNews("soure", "api-key");
        // Set up progress before call
        final ProgressDialog progressDoalog;
        progressDoalog = new ProgressDialog(MainActivity.this);
        progressDoalog.setMax(100);
        progressDoalog.setMessage("Its loading....");
        progressDoalog.setTitle("ProgressDialog bar example");
        progressDoalog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        // show it
        progressDoalog.show();
        call.enqueue(new Callback < JSONResponse > () {
            @Override
            public void onResponse(Call < JSONResponse > call, Response < JSONResponse > response) {
                // close it after response
                progressDoalog.dismiss();
                Log.v("this", "Yes!");
            }
        }
        @Override public void onFailure(Call < JSONResponse > call, Throwable t) {
            // close it after response
            progressDoalog.dismiss();
            Log.v("this", "No Response!");
        }
    });
Simple Trick progressBar in your layout
  <ProgressBar
        android:id="@+id/progressBar"
        style="?android:attr/progressBarStyle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="?attr/actionBarSize"
        android:layout_weight="1" />
Initialize the progressbar
 loadProgress = findViewById(R.id.progressBar);
Finally make it gone after json load competed
 loadProgress.setVisibility(View.GONE);
then make it visible in some user interaction, then finally gone when loading of json data is completed. Simple fake trick
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With