yuva ツ
yuva ツ

Reputation: 3703

image button changing in android

In my application i'm changing image of button for 1sec.how to compare image of button.i tried a lot.please help me.when i chang image 1st tyme after 1sec its not changing agian.its remain the same.below is the code-

   myTimer = new Timer();

    myTimer.schedule(new TimerTask() {  

        @Override
        public void run() {
            if(time==-1){
                onStop();
            }
            else
            runOnUiThread(new Runnable() {
                public void run() {

                    Random rand=new Random();
                    System.out.println("timer...."+time);               
                    time=time-1;
                    int num = rand.nextInt(buttonIds.length);
                    int buttonId = buttonIds[num];
                    Button bb=(Button) findViewById(buttonId);

                    if((bb.getBackground()).equals(button.getBackground()))
                    {
                     bb.setBackgroundResource(R.drawable.happy);
                     wrong++;
                     System.out.println("llllllllllll"+wrong);
                    }
                    else
                     {
                        bb.setBackgroundResource(R.drawable.whoa);
                        count++;
                        System.out.println("mmmmmm"+count);
                     }

                }
            });
        }

    },0, 1000);


}

Upvotes: 0

Views: 132

Answers (1)

Pragnani
Pragnani

Reputation: 20155

Edit you are getting the button freshly like this

Button bb=(Button) findViewById(buttonId);

so it will always remains whoa....because the condition is always false.....


I would suggest you to set Content Description while setting the background resource and then compare the content Description....You can't compare by bb.getBackground()).equals(button.getBackground()) do something like this

 but1=(Button) findViewById(R.id.b1);
    but1.setBackgroundResource(R.drawable.happy);
but1.setContentDescription("happy");

and

if(bb.getContentDescription().equals(button.getContentDescription())

Upvotes: 1

Related Questions