Search code examples
androidanimationbuttonclickable

android button not clickable while playing animations


I have a button and while this button is playing an animation, I'm not able to click on button. I've set click listener and touch listener but in debug mode it's not entering in OnClick and in onTouch methods. Do you know why? Thanks

edit: I've tried something like:

        AsyncTask task = new AsyncTask() {

        @Override
        protected Object doInBackground(Object... objects) {
            button1.setOnClickListener(new View.OnClickListener() {

                public void onClick(View view) {
                    Toast toast = Toast.makeText(MyActivity.this, button1.getText(), Toast.LENGTH_SHORT);
                    toast.show();
                }
            });
            return null;
        }

        ;
    };
    task.execute(button1);

but it's not working

Edit: here is the full source code


Solution

  • Before Android 3.0, using any of the animation classes only changes where the view is drawn - it won't adjust its touchable-bounds during (or after) the animation: http://android-developers.blogspot.com/2011/02/animation-in-honeycomb.html

    You could:

    • Make a ViewGroup which moves its children every onDraw
    • Override your View's onDraw to move itself - Could use margin or padding, or position (if view is in a FrameLayout or something similar).
    • Only use 3.0+ devices
    • Override the parent's onTouch (or onInterceptTouchEvent), calculate where the View is being drawn (you can get how far into and the offset from real position from the Animation *) and handle accordingly... * Looking at your code (since you generate a random direction each time it finishes), this might not be possible without tracking which directions you've previously take..