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 7778799
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T18:29:19+00:00 2026-06-01T18:29:19+00:00

In my app I have 4 main buttons, plus another 2. These 4 buttons

  • 0

In my app I have 4 main buttons, plus another 2. These 4 buttons are declared at the beginning of the main activity.

Button button1, button2, button3, button4;
button1 = (Button) findViewById(R.id.button1);
button1.setTag("blue");

(each button has a tag and is set the same way as button1)

The four buttons I want to cycle through different colours when they are pressed. I manage this by;

public void button1(View v) {

    if ("blue".equals(button1.getTag())) {
        button1.setBackgroundDrawable(getResources().getDrawable(
                R.drawable.brown));
        button1.setTag("brown");
    } else if ("brown".equals(button1.getTag())) {
        button1.setBackgroundDrawable(getResources().getDrawable(
                R.drawable.red));
        button1.setTag("red");
    } else if //...etc

This works all well and good until I press any of the two buttons, an example code of one of the buttons

    public void back(View v) {
    setContentView(R.layout.main);
    t = new TextView(this);
    t = (TextView) findViewById(R.id.textView1);
    t.setText("");
}

Once I press any of the two buttons the colours change back to the original drawable set in the xml file

android:background="@drawable/blue"

Now when I press the 4 main buttons the drawable does not change, but I definitely know that it is getting re-tagged, so why won’t it change the drawable after I press the button?

  • 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-01T18:29:21+00:00Added an answer on June 1, 2026 at 6:29 pm

    If your ‘two buttons’ onClick handler makes a call to Activity.setContentView(int), then all of the buttons will reset to how they are specified in the original XML layout. New views will be inflated and these will not have a tag (you do not seem to be re-setting the tags after the call to setContentView). A null tag will not match any of your colour strings and so your buttons will not cycle their background.

    If you want to maintain the views how they were, then do not reset the content view of the Activity. In most cases, setContentView is only called once per lifetime of an Activity, although obviously there can be a few exceptions.

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

Sidebar

Related Questions

in the main activity of my app i have placed a button named as
I have a winforms app and the main (and only) form has several buttons.
The main view of my app don't have a UINavigationController but when a button
I have a main C++ app built in Visual Studio 2005, called A.exe. It
We have App A as main app. Now we build from it App B
I have a main branch with a file called app.js . I made changes
I have a simple android app to play videos. I have a main screen
I have an app using a ListView as a main screen. Each row displays
I have an app that opens a non-modal form from the main form. The
I have an app that so far consists of two Activities: The Main Menu

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.