Android Button Doesn't Respond After Animation

I have a basic animation of a button after it is pressed currently in my application. After the button finishes animating, I can no longer click on it. It doesn't even press with an orange highlight.

Any help?

Here's my code:

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

animation = new AnimationSet(true);
animation.setFillAfter(true);
Animation translate = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 5.0f);
translate.setDuration(500);
animation.addAnimation(translate);

LayoutAnimationController controller = new LayoutAnimationController(animation, 0.25f);


generate = (Button)findViewById(R.id.Button01);

generate.setOnClickListener(new View.OnClickListener(){
    public void onClick(View v){
            keyFromTop();

        }
    });


}

public void keyFromTop(){   
    generate.setAnimation(animation);    
}


Asked by: Lenny440 | Posted: 24-01-2022






Answer 1

Animations affect only the drawing of widgets, which means after the animation is done, your button is still at its previous location. You need to manually update the layout parameters of your button if you want to move it to the new location. Also, your AnimationSet and your AnimationController are useless.

Answered by: Lenny861 | Posted: 25-02-2022



Answer 2

If you want to be able to click the button after the animation is complete, you have to manually move the component. Here's an example of a translate animation applied to a button:

public class Test2XAnimation extends Activity {

    private RelativeLayout buttonContainer;

    private Button button;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        button = (Button) this.findViewById(R.id.button1);
        buttonContainer = (RelativeLayout) button.getParent();
    }

    public void startTapped(View view) {
        animateButton(200, 100);
    }

    public void buttonTapped(View view) {
        Toast.makeText(this, "tap", Toast.LENGTH_SHORT).show();
    }

    private RelativeLayout.LayoutParams params;
    private CharSequence title;

    private void animateButton(final int translateX, final int translateY) {
        TranslateAnimation translate = new TranslateAnimation(0, translateX, 0,
                translateY);
        translate.setDuration(1500);
        translate.setAnimationListener(new AnimationListener() {

            public void onAnimationEnd(Animation animation) {
                buttonContainer.removeView(button);
                button = new Button(Test2XAnimation.this);
                button.setText(title);
                button.setOnClickListener(new OnClickListener() {

                    public void onClick(View v) {
                        buttonTapped(button);
                    }
                });

                params.leftMargin = translateX + params.leftMargin;
                params.topMargin = translateY + params.topMargin;
                params.rightMargin = 0 + params.rightMargin;
                params.bottomMargin = 0 + params.bottomMargin;
                button.setLayoutParams(params);
                buttonContainer.addView(button);
            }

            public void onAnimationRepeat(Animation animation) {

            }

            public void onAnimationStart(Animation animation) {
                params = (RelativeLayout.LayoutParams) button.getLayoutParams();

                title = button.getText();
            }

        });
        button.startAnimation(translate);
    }
}

The method startTapped() is triggered when a user clicks a button in the UI. The other button moves by (200,100). At the end, I remove the old one and create a new one, thenaI add it to the parent view. You can see that buttonTapped() is called after the animation.

A suggestion: you can use NineOldAndroids project if you want to support both the new and the old way of animating a component, then you can check the OS version and run this code only on Gingerbread and lower versions.

Answered by: Catherine655 | Posted: 25-02-2022



Answer 3

I really struggled with shuffling layout params to make the "real" button match up to its animated location. I finally succeeded by using this approach. I extended ImageButton and overrode getHitRect:

    @Override
public void getHitRect(Rect outRect) {
    Rect curr = new Rect();
    super.getHitRect(curr);
    outRect.bottom = curr.bottom + 75;
    outRect.top = curr.top + 75;
    outRect.left = curr.left;
    outRect.right = curr.right;
}

Then, I could use this button in the troublesome view:

<com.sample.YPlus75ImageButton a:id="@+id/....

It's worth noting that all of this changes for 3.0.

Answered by: Sam685 | Posted: 25-02-2022



Similar questions

android - How to avoid memory leaks in frame animation?

I've created simple test app with one activity. The activity contains frame animation. The total size of images in frame animation(riffle_forward_anim) is about 1MB. Here is the code I use to load animation: public class MainScreen extends Activity { /** Called when the activity is first created. */ private ImageView imgForward; private ImageVi...


Android Frame Animation

I would like to know if it is possible to remove one of the frames I added using addFrame in Android? Thanks Kelvin


Android slide animation problem

I have a 3 horizontal views - an image on the left which is a thin vertical handle-type bar and an image to its right followed by a textview. What I'm trying to achieve is a sliding animation, activated with a click on the handle, so that the center image slides in and out to the left (and so disappearing when fully left) with the textview following this animation smoothly. The effect will be the handle remains in place (h...


How to do 'busy' animation in Android?

Doesn't seem to be covered in the books Ive looked at.


animation - Android - Skewed button

is it possible to create a skewed shape button? (i.e like a stripe shape from bottom left of screen to top right of screen instead of the regular square or rectangle shape button)? Something like this image http://www.codeproject.com/KB/silverlight/SilverLightFAQPart2/9.JPG And I need to make sure the...


animation - glow when touch the screen in android?

when i touch whereever in the screen that point will be glow(nothing but like a flash or glittering) for some time. how to do that? any example or idea?? i have to implement to put buttons on it. exactly when i touch the screen it will glow some time and then the button will appear on the point where i touched.


Slide expand animation in android

I have a simple ListView listing results in android. Upon click of each item, I would like it to slide down expand and show the content. Is there an easy way to do this in android? Any help will be appreciated.


Android animation doesn't work, probably some kind of screen redraw problem

I have created a custom component in my program by extending a ViewGroup. This component listens to touch events and are supposed to start animations when the user has move their finger past some certain points. I'm able to start animations while the user is touching the screen. But I'm not able to start animations if the user doesn't move their finger. It's probably that the phone thinks it doesn't have to update ...


how to do android image animation

i am trying to get a simple image animation going. i want to make it looks as if the helicopter's propeller blades are turning. i have 3 images for the helicopter, and i am showing one of these images depending on the animation progress. the problem is that all three images end up overlapping each other as opposed to just one image showing up at one time, thereby creating the animation. this is what i did so far, i even ...


Android: how to change a low quality image to the hi quality one when the animation stops in gallery?

I want to do an image gallery like in iphone. I want to show low quality (pre-resized) images and when the image is active I want to process the big image and show the result in the gallery. I have two questions. How to attach a listener on the animation stop in gallery? And how to access an image after this action?


android - How to avoid memory leaks in frame animation?

I've created simple test app with one activity. The activity contains frame animation. The total size of images in frame animation(riffle_forward_anim) is about 1MB. Here is the code I use to load animation: public class MainScreen extends Activity { /** Called when the activity is first created. */ private ImageView imgForward; private ImageVi...


Android Frame Animation

I would like to know if it is possible to remove one of the frames I added using addFrame in Android? Thanks Kelvin


Android slide animation problem

I have a 3 horizontal views - an image on the left which is a thin vertical handle-type bar and an image to its right followed by a textview. What I'm trying to achieve is a sliding animation, activated with a click on the handle, so that the center image slides in and out to the left (and so disappearing when fully left) with the textview following this animation smoothly. The effect will be the handle remains in place (h...


How to do 'busy' animation in Android?

Doesn't seem to be covered in the books Ive looked at.


Showing animation to call function in android

Is it possible to show any animation when I call a function in same class in android? In my application, I called a function "doAddition" so many times. And I want whenever I call this function, it shows some animation. Thank you


animation - Android - Skewed button

is it possible to create a skewed shape button? (i.e like a stripe shape from bottom left of screen to top right of screen instead of the regular square or rectangle shape button)? Something like this image http://www.codeproject.com/KB/silverlight/SilverLightFAQPart2/9.JPG And I need to make sure the...


listview - Android Animation for all the items in a list

i have a listView which uses a custom .xml for showing each item. Fine, in this xml i have a CheckBox (with visibility set GONE) and a TextView with a text. What i want is have an animation which moves the text to the right and shows the CheckBox. I have the animation working fine for one item, but if i want to start it for all the items the problem comes. I've tried with a loop over all th...


android - Animation resetting view state?

i am trying to do some simple animation, i have list with items, those items include also checkboxes on a checkbox button click i want to show with animation some button from the buttom, something like this: private int mPosition; private CheckBox chkBox; OnItemClickListener(CheckBox mChkBox, View v) { chkBox = mChkBox; chkBox.setClickable(false); chkBox.se...


animation - How do you drop a pin on a MapView in Android?

I can locate the pin for corresponding locations in MapView. I want to animate that pin dropping down onto the map. How to use animation for that? Any ideas?






Still can't find your answer? Check out these communities...



Android Google Support | Android Community | Android Community (Facebook) | Dev.io Android



top