404response
404response

Reputation: 103

OnClick doesnt work while Animation is running

I want to make a game where an image moves from the left to the right and when you click on it something happens. I made it moving but when I click on it nothing happens. Here is the code:

package com.game.luc08.game;

import android.content.res.Resources;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.TextView;

public class Game extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_game);

    final ImageView image= (ImageView) findViewById(R.id.image);

    final TextView test = (TextView) findViewById(R.id.test);

    int screenWidth = this.getResources().getDisplayMetrics().widthPixels;

    image.setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    test.setText("Clicked");
                }
            }
    );

    Animation animation = new TranslateAnimation(0, screenWidth, 0, 0);
    animation.setDuration(5000);
    animation.setFillAfter(true);
    image.startAnimation(animation);

}

}

Upvotes: 1

Views: 57

Answers (1)

Artur  Dumchev
Artur Dumchev

Reputation: 1330

Instead of your animation you may try ViewPropertyAnimator and you will be able to detect clicks:

image.animate().xBy(screenWidth).setDuration(5000).start();

Upvotes: 1

Related Questions