Disable All Touch Screen Interactions While Animation
Asked Answered
E

4

15

I wish to disable all the touch screen interactions while an animation is being displayed. I don't wish to use the setClickable() method on the buttons at the start or end of the animation because there are a large number of buttons. Any suggestions?

Eshman answered 11/6, 2012 at 21:15 Comment(0)
G
6

In your Activity, you can override onTouchEvent and always return true; to indicate you are handling the touch events.

You can find the documentation for that function there.

Edit Here is one way you can disable touch over the whole screen instead of handling every view one by one... First change your current layout like this:

<FrameLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >

    < .... put your current layout here ... />

    <TouchBlackHoleView
        android:id="@+id/black_hole"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />
</FrameLayout>

And then define your custom view with something like this:

public class TouchBlackHoleView extends View {
    private boolean touch_disabled=true;
    @Override
    public boolean onTouchEvent(MotionEvent e) {
        return touch_disabled;
    }
    public disable_touch(boolean b) {
        touch_disabled=b;
    }
}

Then, in the activity, you can disable the touch with

(TouchBlackHoleView) black_hole = findViewById(R.id.black_hole);
black_hole.disable_touch(true);

And enable it back with

black_hole.disable_touch(false);
Governor answered 11/6, 2012 at 21:20 Comment(11)
View view = findViewById(R.id.whole_view); view.setOnTouchListener(new OnTouchListener() { public boolean onTouch(View v, MotionEvent event) { // TODO Auto-generated method stub return true; } }); i wrote this on start of my animation but it didn't work kindly tell me where am wrongEshman
here whole_view is the id of my main layoutEshman
What kind of view is it and what are you trying to disable ? Is it a button ? a ScrollView ? ... ?Governor
i have buttons all over my layout. i wish to disable themEshman
What kind of Layout do you have ? Maybe you could use a FrameLayout and overlay a View over it where you could enable / disable the touch down... let me write some pseudo code in the answer...Governor
and outside that i wrote this public class TouchBlackHoleView extends View { public TouchBlackHoleView(Context context) { super(context); // TODO Auto-generated constructor stub } private boolean touch_disabled=true; @Override public boolean onTouchEvent(MotionEvent e) { return touch_disabled; } public void disable_touch(boolean b) { touch_disabled=b; } }Eshman
and using the TouchBlackHoleView am not able to put a background image By the way, am using table layout inside the linear layoutEshman
Thanks for so much of help and as you said, i tried this. but its showing this error in the LOG .** Unable to start activity ComponentInfo{com.example.touch/com.example.touch.Disable_touchActivity}: java.lang.ClassCastException: android.widget.LinearLayout**. and inside the oncreate function i have written setContentView(R.layout.main); TouchBlackHoleView view; view = (TouchBlackHoleView) findViewById(R.id.black_hole); ((TouchBlackHoleView) view).disable_touch(true);Eshman
are you sure the view you defined with android:id="@+id/black_hole" is the TouchBlackHoleView? From the error, it looks like it is a LinearLayout...Governor
yes sir its the id of linear layout. actually am not using the touchblackholeview coz am not able to put a background image over there.Eshman
Glad you got it working... although I thought you did not want to use setClickable...?Governor
A
2

Easy way to implement that is add transaperent layout over it (add it in your xml fill parent height and width).

In the animation start: transaparentlayout.setClickable(true);

In the animation end: transaparentlayout.setClickable(false);

Aronarondel answered 26/8, 2013 at 17:37 Comment(0)
E
1

answer to this issue

for (int i = 1; i < layout.getChildCount(); i++) { TableRow row = (TableRow) layout.getChildAt(i); row.setClickable(false);

selected all the rows of the table layout which had all the views and disabled them

Eshman answered 12/6, 2012 at 18:42 Comment(0)
S
0

Eventually I took as a basic answer of @Matthieu and make it work such way. I decide to publish my answer because it take me maybe 30 min to understood why I got error.

XML

<...your path to this view and in the end --> .TouchBlackHoleView
    android:id="@+id/blackHole"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

Class

public class TouchBlackHoleView extends View { private boolean touchDisable = false;

public TouchBlackHoleView(Context context) {
    super(context);
}

public TouchBlackHoleView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public TouchBlackHoleView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    return touchDisable;
}

public void disableTouch(boolean value){
    touchDisable = value;
}
}

Using

blackHole = (TouchBlackHoleView) findViewById(R.id.blackHole);
blackHole.disableTouch(true);

Enjoy

Seersucker answered 9/11, 2016 at 11:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.