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

  • SEARCH
  • Home
  • 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 8646269
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T12:45:49+00:00 2026-06-12T12:45:49+00:00

I am developing a simple application in which buttons background change dynamically after one

  • 0

I am developing a simple application in which buttons background change dynamically after one second. I have a button in my app name stopButtonBackgroundChanging, as I click the button it stops the changing of buttons background but after 2-seconds the app Stops Unexpectedly”. Please help me in this respect and also see my code below you might get better idea that what’s really I want to do.

UpdateRunnable updateRunnable;  
private Runnable mEndlessRunnable;


     public void onCreate(Bundle savedInstanceState) 
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.myLayout);


            Button stopButtonBackgroundChanging = (Button)findViewById(R.id.buttonStop);
            stopButtonBackgroundChanging.setOnClickListener(new OnClickListener() 
            {

                public void onClick(View v) 
                {
                    //See implementation of stop() method at the end of the code
                    updateRunnable.stop();

                }
            });


            mEndlessRunnable = (Runnable) new UpdateRunnable(new Handler(), new Button[] 
            {

                    (Button) findViewById(R.id.button1),
                    (Button) findViewById(R.id.button2),
                    (Button) findViewById(R.id.button3),
                    (Button) findViewById(R.id.button4),

            });
            mEndlessRunnable.run();






        }// End of onCreate()



     //Start of UpdateRunnable
     private static class UpdateRunnable implements Runnable 
     {

         private boolean myContinue = true;


            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() 
            {
                if(myContinue)
                {
                    // 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.buttonyellow);
                        mState = 1;
                        break;
                    case 1:
                        mCurButton.setBackgroundResource(R.drawable.buttonblue);
                        // reset state and nullify so this continues endlessly
                        mState = 0;
                        mCurButton = null;
                        break;
                    }

                    mHandler.postDelayed(this, 1000); 

                }




            }// End of run()

            public void stop()
            {
             this.myContinue =false;    
            }

        }//End of class UpdateRunnable
  • 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-12T12:45:50+00:00Added an answer on June 12, 2026 at 12:45 pm

    I believe you will get this to work by changing the your onClick:

    public void onClick(View v)
    {
        //See implementation of stop() method at the end of the code
        ((UpdateRunnable) mEndlessRunnable).stop();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am developing one application and in which i have to display a five
i am developing a simple view based application which contains two buttons and a
I am thinking about developing a fairly simple facebook application which i have modelled
I am developing a simple application which has to send an SMS message from
I'm developing a simple Android application that would have an action bar on the
I am developing a simple To-do List application which involves obtaining 'things to do'
I am developing a simple application which hava a file Constants.py containing all configuration,
I am developing a very simple application which accesses a written url. So i
I have been developing an Android application which sends NDEF messages via Android Beam
I'm developing the most simple ASP.NET application which is giving me an annoying error.

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.