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

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


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.

@Overrideprotected 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

  @Overridepublic 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!


I have fixed this issue by adding root layout to the ProgressBar.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_height="match_parent"    android:layout_width="match_parent"    android:clickable="true"    android:gravity="center"    android:visibility="gone"    android:id="@+id/progress">    <ProgressBar        style="?android:attr/progressBarStyleLarge"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_centerInParent="true"        android:indeterminate="true"        android:indeterminateTintMode="src_atop"        android:indeterminateTint="@color/primary"/></LinearLayout>

Made the root layout clickable

android:clickable="true"

NOTE: In my main view, I had RelativeLayout as root and have added above-mentioned code inside the root layout at the last position (last child).

Hope this helps!!


just set:

android:clickable="true" 

in your xml

<ProgressBar...

Only this makes magic!