Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable user interaction while ProgressBar is visible in android?

I am using a custom ProgressBar. Now while a task is going on, I am showing the progress bar, but user can still interact with the views and controls. How do I disable the user interaction on whole view just like a ProgressDialog does , when it is visible.

Do I need to use a transparent view on top of main view and show the progress bar on that view and hide that view once a task is completed.

Or just get the id of my parentView and set it disabled ? But then I won't be able to dim the background, just like what happens when a dialog appears on the view/Activity/Fragment. Right?

I just want to know the way to disallow the user from any interaction while the progressbar is visible.

Thanks

like image 688
intellignt_idiot Avatar asked Apr 28 '16 14:04

intellignt_idiot


People also ask

What is a difference between SeekBar and ProgressBar in android?

In Android, SeekBar is an extension of ProgressBar that adds a draggable thumb, a user can touch the thumb and drag left or right to set the value for current progress. SeekBar is one of the very useful user interface element in Android that allows the selection of integer values using a natural user interface.

How do I stop indeterminate progress bar?

Avoid ProgressDialog If you need to indicate loading or indeterminate progress, you should follow the design guidelines for Progress & Activity and use a ProgressBar in your layout, instead of using ProgressDialog. Show activity on this post. Show activity on this post.

Which attribute is used to display ProgressBar horizontally?

In Android, by default a progress bar will be displayed as a spinning wheel but If we want it to be displayed as a horizontal bar then we need to use style attribute as horizontal. It mainly use the “android. widget. ProgressBar” class.

What can I use instead of progress dialog android?

ProgressBar is best alternative for ProgressDialog.


1 Answers

Your question: How to disable the user interaction while ProgressBar is visible in android?

To disable the user interaction you just need to add the following code

getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE,                     WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE); 

To get user interaction back you just need to add the following code

getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE); 

Here is an example: Note:I am giving you just an example to show how to disable or retain user interaction

Add a progress bar in your xml.Something like this

<ProgressBar     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:id="@+id/progressBar"     android:visibility="gone"/> 

In MainActivity when a button pressed you show the progressbar and disable the user interaction.

@Override protected void onCreate(Bundle savedInstanceState) {     super.onCreate(savedInstanceState);     setContentView(R.layout.activity_main);     mImageView = (ImageView) findViewById(R.id.imageView);     mProgressBar = (ProgressBar) findViewById(R.id.progressBar);     mImageView.setOnClickListener(new View.OnClickListener() {         @Override         public void onClick(View v) {             mProgressBar.setVisibility(View.VISIBLE);             getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE,                     WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);         }     }); } 

And when user backPressed you remove the progressbar again retain the user interaction.Something like this

  @Override public void onBackPressed() {     super.onBackPressed();     mProgressBar.setVisibility(View.GONE);     getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE); } 

If you want to add a feature of disable and greyed out display, you need to add in your xml layout file a linear layout that fills the parent. Set its background to #B0000000 and its visibilty to GONE. Then programmatically set its visibility to VISIBLE.

Hope this help!

like image 100
Niyamat Ullah Avatar answered Sep 19 '22 23:09

Niyamat Ullah