I have a simple main layout, and then I add a fragment layout that only shows an indicator loading on top of that main layout. Problem is that I can still press the buttons behind the loading indicator.
Is there anyway to disable touches so it doesn't pass through to the back? I don't want to have to disable each button on the main screen one by one.
You could set the "clickable" attribute to "true" in the layout that contains your ProgressBar:
<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 visible, its container (which fills the entire screen although it's invisible) will intercept any click events so they won't fall through to the underlying layout while your ProgressBar is showing.
To use this, do this when you want to show the ProgressBar:
findViewById(R.id.progressBarContainer).setVisibility(View.VISIBLE);
and then do this when you're done with it:
findViewById(R.id.progressBarContainer).setVisibility(View.INVISIBLE);
For instance, if you're using this in an AsyncTask, you could make it visible in onPreExecute(), then make it invisible in onPostExecute().
Return true
while you wanna block user touches. Override this method in your Activity
.
@Override
public boolean onTouchEvent( MotionEvent event ) {
return true;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With