Dave
Dave

Reputation: 477

How to change background color repeatedly on Android?

I have a countdown timer that onFinished I want to have the screen change colors repeatedly.

I'm trying:

public void onFinish() {
findViewById(R.id.screenid).setTag("BLACK");
_timer2=new Timer();
_timer2.scheduleAtFixedRate(Flashscreen, 0,1700);}



TimerTask Flashscreen = new TimerTask()
{
    public void run() {

        if ( findViewById(R.id.screenid).getTag()=="BLACK" )
        {
            findViewById(R.id.screenid).setBackgroundColor(Color.BLUE);
            findViewById(R.id.screenid).setTag("BLUE");
            return;
        }
        if (findViewById(R.id.screenid).getTag()=="BLUE")
        {
            findViewById(R.id.screenid).setBackgroundColor(Color.BLACK);
            findViewById(R.id.screenid).setTag("BLACK");
            return;

        }
                }};

But it will only change the color to blue once. What's happening wrong?

Upvotes: 0

Views: 832

Answers (2)

nandeesh
nandeesh

Reputation: 24820

Can you try putting both changing of color to run on uithread. Something like below

runOnUiThread(new Runnable() {
    @Override  
    public void run() {
        findViewById(R.id.imageView1).setBackgroundColor(Color.BLACK);
        findViewById(R.id.imageView1).setTag("BLACK");
    }
});

Upvotes: 1

Stephan van den Heuvel
Stephan van den Heuvel

Reputation: 2138

Is it checking for object equality and not string content equality?

Try changing

( findViewById(R.id.screenid).getTag()=="BLACK" )

To

(((String)findViewById(R.id.screenid).getTag()).equals("BLACK"))

And likewise for the check for "BLUE"

Basically, '==' checks to see if the reference is the same, as in they are both pointing to the same object. 'equals' actually checks to see if the content of the strings are equal.

Upvotes: 2

Related Questions