user1703737
user1703737

Reputation: 533

How to put 4-seconds delay while changing the background image of a buttan in Android

I have two buttons in my sample application and I want to change the background image of the buttons one after another with 4-seconds delay, So far i have developed the code, you can see it below, but it gets sleep my application and display nothing.

Please somebody help me that I could do so.

   int m =0;
    int delay = 4;  //Seconds

    while(m < 4)
    {



     // int i = (int) (Math.random() * num + 1);

    if(m==0)
    {
        button1.postDelayed(new Runnable()
        {
            public void run()
            {
            // TODO Auto-generated method stub
            button1.setBackgroundResource(R.drawable.buttonyellow);
            m++;
            }
        }, 1000*delay);

    }


    else if(m==1)
    {
        button2.postDelayed(new Runnable()
        {
            public void run()
            {
                // TODO Auto-generated method stub
                button2.setBackgroundResource(R.drawable.buttonyellow);
                m++;
            }
        }, 1000*delay);

    }





    if(m==2)
    {
        button1.postDelayed(new Runnable()
        {
            public void run()
            {
            // TODO Auto-generated method stub
            button1.setBackgroundResource(R.drawable.buttonblue);
            m++;
            }
        }, 1000*delay);

    }


    else if(m==3)
    {
        button2.postDelayed(new Runnable()
        {
            public void run()
            {
                // TODO Auto-generated method stub
                button2.setBackgroundResource(R.drawable.buttonblue);
                m++;
            }
        }, 1000*delay);

    }



    }

Upvotes: 1

Views: 880

Answers (3)

Jeremy Roman
Jeremy Roman

Reputation: 16345

postDelayed doesn't delay execution of the current thread, so your thread is stuck posting the m == 0 case repeatedly.

You probably want your posted Runnable to invoke some method that not only sets the background and increments the number, but also calls this logic (to choose a new Runnable to post) again.

e.g. (warning: untested code)

// assuming button1 and button2 are available
new Runnable() {
  private int m = 0;
  private final int delay = 4000;

  @Override
  public void run() {
    if (m == 0) {
      button1.setBackgroundResource(R.drawable.buttonyellow);
      button1.postDelayed(this, delay);
    } else if (m == 1) {
      button2.setBackgroundResource(R.drawable.buttonyellow);
      button2.postDelayed(this, delay);
    }
    // more cases here -- also consider making m wrap around
    m++;
  }
}.run();

Naturally this should be invoked on the UI thread (if it's not called on the UI thread, it should be dispatched to the UI thread with post or similar).

Upvotes: 1

Jug6ernaut
Jug6ernaut

Reputation: 8325

int mark = 0; 

button2.postDelayed(new Runnable()
    {
        public void run()
        {
            switch(mark++){
                 case 0:button2.setBackgroundResource(R.drawable.buttonyellow); break;
                 case 1:button2.setBackgroundResource(R.drawable.buttongreen); break;
                 ..ect
            }
            if(mark==Max)mark=0;

         button2.postDelayed(this,4000);
        }
    }, 4000);

Note, this will go on till the end of time, or when your app stops.

Upvotes: 1

Phil
Phil

Reputation: 36289

Your while loop is causing your UI thread to wait for these operations to complete. Your best two options would be to call this inside a new thread:

new Runnable() {
    @Override
    public void run() {
        //Your code
    }
}.run();

Or to embed them in a chain:

button1.postDelayed(new Runnable()
    {
        public void run()
        {
            button1.setBackgroundResource(R.drawable.buttonyellow);
            button2.postDelayed(new Runnable()
            {
                public void run()
                {
                    button2.setBackgroundResource(R.drawable.buttonyellow);
                    button1.postDelayed(new Runnable()
                    {
                        public void run()
                        {
                            button1.setBackgroundResource(R.drawable.buttonblue);
                            button2.postDelayed(new Runnable()
                            {
                                public void run()
                                {
                                    button2.setBackgroundResource(R.drawable.buttonblue);
                                }
                            }, 1000*delay);
                         }
                    }, 1000*delay);
                }
            }, 1000*delay);
        }
    }, 1000*delay);

Upvotes: -1

Related Questions