Reputation: 533
I have two buttons in my sample Android app. I want to select a button randomly and change its background image(to yellow) and display it for 4-seconds. After the 4-seconds now again I want to change the background image(to blue) and display it for 4-seconds. Now to repeat the process of buttons random selections and do the same with the radomly selected button as I stated above.
I have developed some code, when I test the code for indvidual button it works fine but when I run for both the buttons its not working accordingly.
Please help me, I would be very thankfull to you. You can check my code as wl....
int mainCount =0;// mainCount- could be a random number
int count_1 =0;
int count_2 =0;
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.mlayout);
mainHandler = new Handler();
mHandler1 = new Handler();
mHandler2 = new Handler();
.
.
.
mainRunnable.run();
}
Runnable mainRunnable = new Runnable(){
public void run(){
mainHandler.postDelayed(new Runnable(){
public void run(){
switch(mainCount){
case 0:
runButton1.run();
mainCount++; // mainCount- could be a random number to select a button randomly
break;
case 1:
runButton2.run();
mainCount++;// mainCount- could be a random number to select a button randomly
break;
}
if(count==2)
mainCount =0;
mainHandler.postDelayed(this,4000);
}
}, 4000);
}
};
Runnable runButton1 =new Runnable(){
public void run(){
mHandler1.postDelayed(new Runnable(){
public void run(){
switch(count_1){
case 0:
button1.setBackgroundResource(R.drawable.buttonyellow);
count_1++;
break;
case 1:
button1.setBackgroundResource(R.drawable.buttonblue);
count_1++;
break;
}
if(count_1==2)
count_1 = 0;
mHandler1.postDelayed(this,4000);
}
}, 4000);
}
};
Runnable runButton2 =new Runnable(){
public void run(){
mHandler2.postDelayed(new Runnable(){
public void run(){
switch(count_2){
case 0:
button2.setBackgroundResource(R.drawable.buttonyellow);
count_2++;
break;
case 1:
button2.setBackgroundResource(R.drawable.buttonblue);
count_2++;
break;
}
if(count_2==2)
count_2 = 0;
mHandler2.postDelayed(this,4000);
}
}, 4000);
}
};
Upvotes: 0
Views: 385
Reputation: 10857
First off, you don't need multiple Handler
s, one will be enough. Second, you're calling mainRunnable.run()
in onCreate
to run another inner runnable so this would be better suited to just being a method. Regardless, here's my take:
public class MyActivity extends Activity {
private Runnable mEndlessRunnable;
@Override
public void onCreate(Bundle savedState) {
super.onCreate(savedState);
setContentView(R.layout.my_activity);
mEndlessRunnable = new UpdateRunnable(new Handler(), new Button[] {
(Button) findViewById(R.id.button_1),
(Button) findViewById(R.id.button_2)
});
mEndlessRunnable.run();
}
private static class UpdateRunnable extends Runnable {
private Random mRand = new Random();
private Handler mHandler;
private Button[] mButtons;
private Button mCurButton;
private int mState;
public UpdateRunnable(Handler handler, Button[] buttons) {
mHandler = handler;
mButtons = buttons;
}
public void run() {
// select a button if one is not selected
if (mCurButton == null) {
mCurButton = mButtons[mRand.nextInt(mButtons.length)];
}
// check internal state, `0` means first bg change, `1` means last
switch (mState) {
case 0:
mCurButton.setBackgroundResource(R.drawable.blue_bg);
mState = 1;
break;
case 1:
mCurButton.setBackgroundResource(R.drawable.yellow_bg);
// reset state and nullify so this continues endlessly
mState = 0;
mCurButton = null;
break;
}
mHandler.postDelayed(this, 4000);
}
}
}
I haven't tested the above, but I'd use the above as a reference. Enjoy
Upvotes: 1