Search code examples
javaandroidprogress-bar

progressBar.setVisibility(View.GONE); Is there anybody please tell where i can use Progressbar gone after stop loading


I'm new in Java Android Development & I was trying to develop something but I have to use the progress bar in WebView but I don't know How I have to use code stop progress bar after completed loading.

    private ProgressBar progressBar;

   mhelp = (LinearLayout) root.findViewById(R.id.lythelp);
                mhelp.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        progressBar.setVisibility(View.VISIBLE);
                        Intent intent = new Intent(getActivity(), Help.class);
                        startActivity(intent);
progressBar.setVisibility(View.GONE);


                    }
                });

Solution

  • You should not set the progressBar visibility after you start a new activity. The simplest thing you can do is use a flag variable to hold the state of your progress bar.

    startActivity(intent);
    progressBar.setVisibility(View.GONE); // should be avoided
    

    SOLUTION:

    private ProgressBar progressBar;
    String progressBarState = "empty"
    String PROGRESS_BAR_LOADING = "LOADING"
    String PROGRESS_BAR_LOADED = "LOADED"
    
    mhelp = (LinearLayout) root.findViewById(R.id.lythelp);
    
    mhelp.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
    if(PROGRESS_BAR_LOADING == "LOADING")
      progressBar.setVisibility(View.VISIBLE);
    
      Intent intent = new Intent(getActivity(), Help.class);
      startActivity(intent);
    }
    

    Inside HelpActivity (or whatever activity you want to start):

    override onCreate(): void {
      /*
      * You will need a progress bar on this activity's view 
      * as well if you are transitioning between activities and
      * want to maintain the progress.
      */
    
      progressBar.setVisibility(View.GONE);
    
      /*
      * You can pass the progress between the activities by
      * passing the completion rate through the bundle that is
      * sent through the activities.
      * You can also add a  key value pair for the progress bar's
      * current state in that bundle and check for that state in the
      * onCreate.
      */
    }