1

I have a linearLayout as the main layout in my android xml file. The background is set to "@drawable/window_background_red". Inside the activity (at run time) I want to set this to "@drawable/window_background_green".

window_background_green.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape>
            <gradient
                android:type="linear"
                android:startColor="#000000"
                android:endColor="#004400"
                android:angle="-90"
                />
        </shape>
    </item>
</selector>
David
  • 692
  • 8
  • 21
  • Duplicate: http://stackoverflow.com/questions/4761686/how-to-set-background-color-of-activity-to-white-programmatically – Raul Agrait Sep 03 '11 at 01:40

1 Answers1

0

let's say that for linearLayout android:id="@+id/linearLayout" then in code:

LinearLayout linearLayout = (LinearLayout)findViewById(R.id.linearLayout);

linearLayout.setBackgroundResource(R.drawable.window_background_green);

And there, you've set the background to be @drawable/window_background_green

Reed
  • 14,703
  • 8
  • 66
  • 110
  • Thanks, for some reason "R.id.linearLayout" was throwing an error. I came back a few hours later and it worked, not sure what was going on. – David Sep 03 '11 at 07:08
  • Idk. I've had weird, unexplainable things happen before, too. Sometimes uninstalling, re-installing the app (or factory resetting the phone) helps. Glad I could help. – Reed Sep 04 '11 at 02:22
  • That, or if go to Project > Clean and then run it again that fixes it. Maybe one your R.id.linearLayout somehow was messed up in the generated file. – Reed Sep 05 '11 at 19:24