Search code examples
javaandroidandroid-fragmentsandroid-spinnerandroid-loader

spinner or loader while fragment page get completedly loaded


I have created an android application using Android Fragments as described in Dynamically Prevent Rotation On Android Fragment.

The application is working fine but the problem is that say a particular fragment say Photo Gallery might calls several http calls and might certain time for bringing all the images. So when we click the Photo Gallery it seems stuck for sometime.

How can we show some slider or progress bar like something till the fragment view get loaded completely?

An example like is as shown below:

enter image description here

My PhotoGalleryFragment is given below

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class PhotoGalleryFragment extends Fragment {

    public PhotoGalleryFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_photo_gallery, container, false);

        // some http calls to be triggered

        return rootView;
    }
} 

Solution

  • You can use ProgressDialog, for example:

    public class PhotoGalleryFragment extends Fragment {
    
        private ProgressDialog mProgressDialog;
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_photo_gallery, container, false);
    
            // some http calls to be triggered
    
            return rootView;
        }
    
        @Override
        public void onAttach(Activity activity) {
            super.onAttach(activity);
    
            mProgressDialog = ProgressDialog.show(activity, "", "Loading...", true, false);
        }
    }
    

    When you http calls are ended you can dismiss the ProgressDialog with mProgressDialog.dismiss().