Turn off all touches when loading indicator is displayed

I have a simple main layout, and then I add a fragment layout that only shows the loading of the indicator on top of this main layout. The problem is that I can still click the buttons behind the download indicator.

Is there a way to turn off the touch so that it does not go back? I do not want to disable each button on the main screen in turn.

+6
source share
2 answers

You can set the "clickable" attribute to "true" in the layout that your ProgressBar contains:

<FrameLayout android:id="@+id/progressBarContainer" android:layout_height="match_parent" android:layout_width="match_parent" android:clickable="true" > <ProgressBar android:id="@+id/progressBar" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_gravity="center" /> </FrameLayout> 

Then, while the ProgressBar is displayed, its container (which fills the entire screen, although invisible) will intercept any click events so that they do not fall into the base layout while your ProgressBar is displayed.

To use this, do this when you want to show the ProgressBar:

 findViewById(R.id.progressBarContainer).setVisibility(View.VISIBLE); 

and then do it when done:

 findViewById(R.id.progressBarContainer).setVisibility(View.INVISIBLE); 

For example, if you use this in AsyncTask, you can make it visible in onPreExecute (), and then make it invisible in onPostExecute ().

+12
source

Return true while you want to block user touches. Override this method in Activity .

 @Override public boolean onTouchEvent( MotionEvent event ) { return true; } 
+1
source

Source: https://habr.com/ru/post/971678/


All Articles