Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disabling all touches when loading indicator is shown

Tags:

android

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.

like image 939
mskw Avatar asked Jul 02 '14 21:07

mskw


2 Answers

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().

like image 149
JDJ Avatar answered Nov 06 '22 08:11

JDJ


Return true while you wanna block user touches. Override this method in your Activity.

@Override
public boolean onTouchEvent( MotionEvent event ) {
    return true;
}
like image 34
Idemax Avatar answered Nov 06 '22 08:11

Idemax