1

Here's my main:

@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main_menu);

    Button btn = (Button) findViewById(R.id.newgame_button);

    btn.setOnClickListener(new View.OnClickListener() 
    {
        @Override
        public void onClick(View v) {
            changeBackground(v);
        }
    }); 
}

I've tried the follow methods, both of which have failed:

public void changeBackground(View v) 
{
      View someView = findViewById(R.id.main);
      View root = someView.getRootView();
      root.setBackgroundColor(getResources().getColor(R.color.red));
}

public void changeBackground(View v) 
{
      View root = v.getRootView();
      root.setBackgroundColor(getResources().getColor(R.color.red));
}

I searched and found the solution for how to solve this in How to set background color of Activity to white programmatically?; the solution posted was:

  // Now get a handle to any View contained 
  // within the main layout you are using
  View someView = findViewById(R.id.randomViewInMainLayout);

  // Find the root view
  View root = someView.getRootView()

  // Set the color
  root.setBackgroundColor(android.R.color.red);

When I try android.R.color.red, eclipse tells me that it should be in the form in my examples.

I did manage to change the background of my button with:

public void changeBackground(View v) 
{
    v.setBackgroundColor(getResources().getColor(R.color.red));
}

So, I'm pretty confident that the issue is not with: getResources().getColor(R.color.red). I've tried many different ways, and I'm not getting anywhere. For reference, here's my xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" 
    android:background="@color/LightCyan"
    android:id="@+id/main">

   <TextView
        android:layout_width="fill_parent"
        android:layout_height="0dip"
        android:text="@string/welcome"
        android:gravity="center"
        android:layout_weight="3" />

   <LinearLayout
        android:layout_width="200dp"
        android:layout_height="0dip"
        android:orientation="vertical"
        android:layout_gravity="center"
        android:layout_weight="2" > 

       <Button
           android:id="@+id/resume_button"
           android:layout_width="fill_parent"
           android:layout_height="wrap_content"
           android:text="@string/resume" 
           android:background="@color/Plum"/>

       <Button
           android:id="@+id/newgame_button"
           android:layout_width="fill_parent"
           android:layout_height="wrap_content"
           android:text="@string/new_game"
           android:background="@color/Plum" 
           android:onClick="changeBackground"/>

    </LinearLayout>

</LinearLayout> 

Any help would be appreciated. Thanks in advance.

Community
  • 1
  • 1
Steve P.
  • 14,489
  • 8
  • 42
  • 72
  • You can try [this](http://stackoverflow.com/questions/16697291/how-to-change-color-of-background-on-app-loadoncreate/16697561#16697561) answer. – Gunaseelan Jun 10 '13 at 04:20

4 Answers4

2
@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main_menu);

    Button btn = (Button) findViewById(R.id.newgame_button);

    btn.setOnClickListener(new View.OnClickListener() 
    {
        @Override
        public void onClick(View v) {
            changeBackground(v);
        }
    }); 
}

Change this to

   Activity activity;
@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main_menu);
    actvity = this;
    Button btn = (Button) findViewById(R.id.newgame_button);

    btn.setOnClickListener(new View.OnClickListener() 
    {
        @Override
        public void onClick(View v) {

           actvity.findViewById(android.R.id.content).setBackgroundColor(Color.BLACK);
        }
    }); 
}
Gunaseelan
  • 14,415
  • 11
  • 80
  • 128
  • It grabs the current `View` from `android.R.id.content` because you said `activity = this`? So, `android.R.id.content` is a special variable? – Steve P. Jun 10 '13 at 04:30
  • @SteveP. Not like that. Used to get the activity's content. refer [here](http://stackoverflow.com/questions/5273436/how-to-get-activitys-content-view). Here `this` refers to current activity only. – Gunaseelan Jun 10 '13 at 04:34
0

In my application I use the following code to change the background:

View view = this.getWindow().getDecorView();
view.setBackgroundColor(Color.BLACK);

Try it, maybe it will work for you.

Marek
  • 3,935
  • 10
  • 46
  • 70
  • If this is not working then probably you have something overlaying your Activity background. The code that I posted changes the background of the root, which means that if you have any Layout in your activity this change will not be visible because of view hierarchy. – Marek Jun 10 '13 at 04:11
  • I posted all of my code above. There isn't anything relevant missing. – Steve P. Jun 10 '13 at 04:13
  • Then I am really curious why my code is not working for you... Strange – Marek Jun 10 '13 at 06:45
  • I'm relatively new to android, so I have no idea. What I did in my answer ended up working, though. – Steve P. Jun 10 '13 at 06:47
0

Figured it out:

public void changeBackground(View v) 
{
      View root = findViewById(R.id.main);
      root.setBackgroundColor(getResources().getColor(R.color.red));
}

Apparently, using getRootView() is not good when you're dealing with your main View. I don't know, but this worked.

Steve P.
  • 14,489
  • 8
  • 42
  • 72
0

@Steve P.

Change this

  public void changeBackground(View v)  {
    v.setBackgroundColor(getResources().getColor(R.color.red)); }

with this one

 public void ChangeBackground(View v)
{
    View parentsView = (View) v.getParent();
    parentsView.setBackgroundColor(Color.RED);
}