Friday, 20 May 2016

Disable background touch events on showing ProgressBar

When showing the progress bar loader while performing some operation, to disable your background controls or in other words if I say that, showing a progress bar spinner only with the properties of a Progress Dialog, here is the solution, how:-

In order to use Progress Bar, define it in the xml like this.

<ProgressBar                                                                          
   android:id="@+id/progressBar1"                                       
   style="?android:attr/progressBarStyleLarge"                     
   android:layout_width="wrap_content"                               
   android:layout_height="wrap_content"                              
   android:layout_centerHorizontal="true" />                        

After defining it in xml, get its reference in java file as follows−

private ProgressBar spinner;                                                  
spinner = (ProgressBar)findViewById(R.id.progressBar1); 


Now use the below method to show the spinner loader and disable user touch events on background like in the case of a progress dialog.

 void showLoading(){                                                                                                              
        spinner.setVisibility(View.VISIBLE);                                                                             
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE,
                WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);                           
    }                                                                                                                                           

In order to dismiss the spinner loader and enable the backgroung touch events, use the below method:-

 void dismissLoading(){                                                                                                               
        spinner.setVisibility(View.INVISIBLE);                                                                             
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
    }                                                                                                                                                


Also like in the case of a Progress Dialog, if you want to add the feature of semi-transparent greyed display while your loading spinner is visible, you need to set the background of the linear layout to #80000000 and its visibilty to Gone in your xml file . Then programmatically set its visibility to Visible when required.

Enjoy..!! and Code to make it better..!!!



No comments:

Post a Comment