FridregJohn
FridregJohn

Reputation: 79

Change button background color not working

I make some sort of quiz app and I want that every time an answer button get clicked there will be an animation changing to to yellow. after 0.25sec the next question will appear (and I want it to change back to the old color)

I already did it here:

public void onClick(View v) {

x

for (int i = 0; i < 4; i++) {
            if(v == answerButtons[i]){
                int j = i;
                ValueAnimator valueAnimator = new ValueAnimator();
                valueAnimator.setDuration(250);
                valueAnimator.setEvaluator(new ArgbEvaluator());
                valueAnimator.setIntValues(Color.WHITE,Color.YELLOW);
                valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                    @Override
                    public void onAnimationUpdate(ValueAnimator valueAnimator) {
                        answerButtons[j].setBackgroundColor((int)valueAnimator.getAnimatedValue());
                    }
                });
                valueAnimator.start();


                ((MainActivity)getActivity()).num++;



            }
        }

it does change the color but right after that the next answers(and question) support to appear on the same buttons. I tried this:

for (int i = 0; i < 4; i++) {
        if(v == answerButtons[i]){
            int j = i;


            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    answerButtons[j].setBackgroundColor(Color.rgb(63, 81, 181));
                    Toast.makeText(getActivity(), "Just changed color to: button "+(j+1), Toast.LENGTH_SHORT).show();
                    //display(questions.get(((MainActivity)getActivity()).num));
                }
            },250);



        }
    }

x

}

but it mostly doesn't work sometimes it does. any idea why it doesn't work everytime? (the only thing it doesn't do is to change the color)

Upvotes: 1

Views: 59

Answers (1)

FridregJohn
FridregJohn

Reputation: 79

I got a solution for this one :) apparently the postDelay function is not that precise..

int j = i;
            ValueAnimator valueAnimator = new ValueAnimator();
            valueAnimator.setDuration(250);
            valueAnimator.setEvaluator(new ArgbEvaluator());
            valueAnimator.setIntValues(Color.WHITE,Color.YELLOW);
            valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator valueAnimator) {
                    answerButtons[j].setBackgroundColor((int)valueAnimator.getAnimatedValue());
                }
            });
            valueAnimator.addListener(new AnimatorListenerAdapter() {
                @Override
                public void onAnimationEnd(Animator animation) {
                    super.onAnimationEnd(animation);
                    answerButtons[j].setBackgroundColor(Color.parseColor("#3F51B5"));
                    display(questions.get(((MainActivity)getActivity()).num));
                }
            });
            valueAnimator.start();

Upvotes: 2

Related Questions