android button not clickable while playing animations
Asked Answered
M

6

6

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

Mackey answered 13/10, 2011 at 19:31 Comment(0)
R
8

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..
Ruthannruthanne answered 17/10, 2011 at 14:37 Comment(0)
L
1

you are facing same issue that i was recently ... when you apply animation on button or any other view and use setFillAfter(True) it means that the image of view is moved not the actual view thats why its not listening to your click listener because its just image of view not your actual view you have to do something like that i explained in answer to my own question according to your situation... means you have to also move the actual view on the end place of animation and use setFillAfter(false) so that when you click after anmation then it should be an actual view not just image used for animation purpose by android

check this link....

EditText stucks after animation and alive back on scrolling......?

In your code use setFillafter(false) and actually place your button at end position of animation by somehow like setting margin or according to your layout use appropriate properties for placement. By Applying these changes your click listener will work perfectly.

==> if you are trying that your button's click listener work while its moving (being animate) then as far as i know its not possible because android uses just image of your view to perform animation not the actual.

Longfaced answered 21/10, 2011 at 12:35 Comment(0)
W
0

This might be a threading problem. You should read Event dispatch thread and how to do thing in android async by reading painless threading and take a look at AsyncTask JavaDoc.

In short: the main thread should not be blocked, since it is used for responing to UI events, such as button presses. Therefore, whenever you do something that take more than some milliseconds, you should do it asynchroniously in another thread.

Whereinto answered 13/10, 2011 at 19:37 Comment(1)
but I want my button to be clickable while playing the animation. animation will play until the button is clickedMackey
L
0
I think there are two things

1)You can handle one event at one time for one object.Like animation is playing on you button that means you can not click on that untill one work get completed(As i understand you animation is on button not on other part of screen)

2)Second if you have playing on rest part of screen and you want to stop it on click of button.Then i think its a problem of focus.once set onfoucslistener to button and check that when you are clicking it Is it getting focus?

Listerism answered 17/10, 2011 at 7:11 Comment(3)
it's not getting focus when I click on the buttonMackey
So its not possible to click it. Can you send me image of screenListerism
It won't help you, you will see just two buttons on the screen... here is my last version code of my try: pastie.org/2713243Mackey
P
0

I would put the animation into the async task and then the button click should be handled normally on the main thread I think. Because the way you do it at the moment is: The animation starts and blocks the main thread. This means that the click event can't be excecuted in the async task

Privatdocent answered 17/10, 2011 at 7:56 Comment(3)
But you can do changes to UI only from UI thread and not from another thread.Mackey
I understood the documentation like this: in onPostExecute() you can update the UI, so if you put your animation in this method it might workPrivatdocent
I've added animation to button in async class and handled onClick event in UI thread but still doesn't work... here is my code: pastie.org/2713243Mackey
S
-1

You must try to use AsyncTask . Programming Android without it would be and horrible usability serious problem.

See this link for how use an asynctask.

Here an example:

public class MyActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // Initialisation of the Activity 
        MyLongTask task = new MyLongTask();
        task.execute("http://blog.fr4gus.com/api/test.json");
    }

    @Override
    protected void onPause() {
        // If you wanna make something in the pause of the Asynctask
    }

    class MyLongTask extends AsyncTask<String, Void, Void>{

        @Override
        protected void onPreExecute() {
            // Before executing what you want to execute
        }

        @Override
        protected Void doInBackground(String... params) {
            // what you want to execute come here :D
            return null; // Or whatever you want pass to onPostExecute
        }

        @Override
        protected void onPostExecute(Void result) {
            // Here we can update for example the UI with something (who knows :? )
        }
    }

}

MODIFIED

You must always extend the Asynctask Activity , since you are trying to pass params through it , you must define them in the head of your extended class :

 class MyLongTask extends AsyncTask<String, Void, Void>{ ...

Then first is the doInBAckground param , next is the onPreExecute param , and the last is the onPostExecute param . That way you can send the params like a Button.

You can learn more about here or in my first link.

Sjoberg answered 13/10, 2011 at 19:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.