90

What is the best practice to maintain styles of fonts and colors. I made a colors.xml file which I have used to change colors on seperate elements like buttons, but I am not sure how Android wants developers to organize their styles.

For example, I would like all screens to have the same background color. How do I do that? Is it something I need to specify for each Activity layout xml? Or elsewhere? How do I accomplish it?

Janusz
  • 187,060
  • 113
  • 301
  • 369
GeekedOut
  • 16,905
  • 37
  • 107
  • 185
  • you create [styles](http://developer.android.com/guide/topics/ui/themes.html) and apply them to your UI elements – zapl Apr 14 '12 at 19:55
  • @zapl rigth its what I was doing. What I was wondering was how to apply a style to the entire screen. Like a background color for every screen? – GeekedOut Apr 14 '12 at 20:03
  • you can either apply some background theme to each topmost LinearLayout (or whatever you use) or you can do that via a [theme for your activity](http://developer.android.com/guide/topics/ui/themes.html#ApplyATheme) – zapl Apr 14 '12 at 20:06

3 Answers3

218

A quick and easy way to make sure every activity has the same background color, is to create a theme for your activities to use. That theme would specify the android:windowBackground.

First define the color in values/colors.xml

<resources>
    <color name="background">#FF0000 </color>
</resources>

Create a themes.xml file in res/values that references that color:

<resources>
 <style name="MyTheme" parent="@android:style/Theme.Light"> 
  <item name="android:windowBackground">@color/background</item>
 </style>
</resources>

... and then in your AndroidManifest.xml specify this as the theme for your activities to use.

 <activity
        android:name=".MyActivity"
        android:theme="@style/MyTheme" />
NPike
  • 13,136
  • 12
  • 63
  • 80
20

Update your android studio to 1.4 it has inbuilt theme editor. as you can see in below image

enter image description here

kundan roy
  • 1,936
  • 1
  • 18
  • 21
9

You can also set the background at the application level in AndroidManifest.xml

<application
   ...
   android:theme="@style/MyTheme">

(Note: I cannot this as a comment of the accepted answer as my reputation is too low).

chris
  • 99
  • 1
  • 1