I have three difficulty modes for my app; Easy, Medium, and Hard. I have a variable that keeps track of the difficulty (0-2). How can I use this variable to change the background color of my app? For example, when the mode is easy I want the background to be green, yellow for medium, and red for hard.
Here is what I have in my layout.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="@+id/relativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/myBackground" >
</RelativeLayout>
And I have the string resource:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="myBackground">#01CC18</color>
</resources>
However, what I want to do is be able to change the background in the program itself instead of in the xml layouts. Can this be done?
Fill your main view with a LinearLayout. Then get a handle to this layout:
LinearLayout layout = (LinearLayout) findViewById(R.id.linearLayout1);
You can use this handle to update the background color whenever your difficulty changes:
switch(difficulty) {
case 0:
layout.setBackgroundColor(android.R.color.green);
break;
case 1:
layout.setBackgroundColor(android.R.color.orange);
break;
case 2:
layout.setBackgroundColor(android.R.color.red);
break;
default:
break;
}
I think this is what are you looking for:
http://android-developers.blogspot.com/2009/03/window-backgrounds-ui-speed.html
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.