I have activity and when it is created it sends some requests, so it takes time to load this activity. Is it possible to show "loading layout" while requests are proceeding? I tried something like this, but it doesn't work:
setContentView(R.layout.loading);
sendRequests();
setContentView(R.layout.main);
CodePudding user response:
To accomplish this, you could add a "loading layout" to your main layout.
Something like this:
<FrameLayout
android:id="@ id/loading_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Loading stuff" />
</FrameLayout>
Then when it is time to show your loading layout you can do:
FrameLayout loadingLayout = findViewById(R.id.loading_layout);
loadingLayout.setVisibility(View.VISIBLE);
When you want to hide it simply:
loadingLayout.setVisibility(View.GONE);
You will want to make sure that you do network requests off of the main thread, and return to the main thread whenever it is time to update the UI (i.e. show/hide the loading layout).
CodePudding user response:
you can add a progress bar in activity or fragment
if you are using Retrofit or etc., use this approach:
visible the progrees bar before request and invisible it when onResponse or onFailure called
binding.progressBar.setVisibility(View.VISIBLE);
// request
call.enqueue(new Callback<MovieResponse>() {
@Override
public void onResponse(Response<MovieResponse> response) {
Log.d(TAG, "onResponse is called");
// invisible progress bar here
binding.progressBar.setVisibility(View.INVISIBLE);
if (!response.isSuccess()) {
Log.d(TAG, "No Success");
}
mMovieList = response.body().getMovies(); // <- response is null here
mMovieAdapter.notifyDataSetChanged();
}
@Override
public void onFailure(Throwable t) {
Log.i(TAG, "onFailure is called");
// invisible progress bar here
binding.progressBar.setVisibility(View.INVISIBLE);
Toast.makeText(getActivity(), "Failed !", Toast.LENGTH_LONG).show();
}
});