Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8603243
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T02:16:43+00:00 2026-06-12T02:16:43+00:00

I have two buttons in my sample Android app. I want to select a

  • 0

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);
        }
    };
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T02:16:44+00:00Added an answer on June 12, 2026 at 2:16 am

    First off, you don’t need multiple Handlers, 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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two simple submit buttons on my form, which work just fine: <button
I have two buttons with the same ID: <button type=submit onclick=//do something id=theID>button 1</button>
I have two buttons in my flex app next to each other, ButtonA and
I am a newbie to Android programming. I want two radio buttons that when
I am writing a simple app that changes the application background randomly on button
I want to do an sample application where in a view i have two
I have the following code which creates a simple window with two buttons which
I have two buttons that moves a movieclip up/down the Y-axis. How do I
I have two buttons, one for start recording and another for stop recording. i
I have two buttons, which the user can click and will open a FileDiagloag

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.