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);
}
}
Note, this will go on till the end of time, or when your app stops.