How to set a universal background for an application - android

I am trying to set a background for every XML layout in my application, and I am using the same picture for them all. At the moment, I am going through all hundred or so layouts and just typing in the code:
android:background="#drawable/dirtwall"
Into all of the layouts. How would I be able to set it automatically so I wouldn't have to change other backgrounds as I made more, and so I wouldn't have to go through all of my XML files?
Also, I am using Eclipse.

You can use styles. This way, you can define all common elements in a single place and then update it only there.
Example style.xml, which should be located in your project's /res/values/ folder
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="appearance">
<item name="android:background">#drawable/dirtwall</item>
</style>
</resources>
and use it like
<LinearLayout
...
style = "#style/appearance"
>

Apply a theme to an Activity or application
To set a theme for all the activities of your application, open the AndroidManifest.xml file and edit the <application> tag to include the android:theme attribute with the style name. For example:
<application android:theme="#style/CustomTheme">
If you want a theme applied to just one Activity in your application, then add the android:theme attribute to the <activity> tag instead.
if you want the background to be transparent, use the Translucent theme:
<activity android:theme="#android:style/Theme.Translucent">
here is the declaration for a custom theme which is simply the standard platforms default light theme. It would go in an XML file under
res/values (typically res/values/styles.xml):
<style name="CustomTheme" parent="android:Theme.Light">
...
</style>

Related

Themes in android

A create a simple Theme as
<style name='one'>
<item name='android:textColor'>#eea</item>
<item name='android:textSize'>20sp</item>
</style>
However on viewing in the emulator the screen goes black.when i do not apply theme the screen has a white background .
what really happens here.i am just starting with android.
In addition ,if a apply a theme to my activity then the attributes of the theme applies to all components of my activity say button,textfields and edittexts .
why would i then write
android:textSize=?android:textSize
to reference value from the theme for any button in my layout when the same value would already be applying.
is the syntax above the correct way to reference an attribute from my theme to assign to attribute for any view in my layout.
thanks
tejinder
Yeah, so you need to do a little more reading.
Let's start with the basics,
You need to understand the differente betweent an Attribute, a Style, and a Theme.
An Attribute is something that can be styled. For instance: android:textSize is an attribute that can have any value.
A Style is a set of specific attributes that will be applied to a Widget. They are defined
in your /values/styles.xml
For instance:
<style name="normalTextThin" parent="android:Widget.Holo.Light.TextView">
<item name="android:gravity">left|center_vertical</item>
<item name="android:padding">8dp</item>
<item name="android:textColor">#FFFFFF</item>
<item name="android:textSize">16sp</item>
</style>
The styles can be applied either as part of a theme or directly as theme-independent.
Theme-indepentent styling of a widget is like this:
<TextView
android:id="#+id/text"
style="#style/normalTextThin"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
You are then theming only that one TextView.
A Theme is a collection of Styles that can be applied to a part of your UI, such a a whole Activity, or your whole Application.
For instance:
<style name="AppTheme" parent="android:Theme.Light">
<item name="android:editTextStyle">#style/EditTextAppTheme</item>
<item name="android:buttonStyle">#style/ButtonAppTheme</item>
<item name="android:imageButtonStyle">#style/ImageButtonAppTheme</item>
</style>
Here, we are declaring that all EditText in your application will use the style named EditTextAppTheme, and so forth and on. When done like this, in order to actually have the theme be active, you declare it in the manifest:
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
That means that you are not required to declare the style on each widget you create.
<EditText
android:id="#+id/input"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="#string/hint_search">
<requestFocus />
</EditText>
That widget right there would already be styled using EditTextAppTheme without the need of you explicitely declaring so.
I recommend you try to read on what attributes can be styled, how to style them, and so forth and on.
If you don't want to though, it's fine, you can still get a lot done with the following tools for styling:
ActionBarStyleGenerator to help you create styles for the ActionBar.
Android Holo Colors to help you style standard widgets.
Hope that helps.
Additional Info
Let me clarify on the whole ?attr/attributeName
The ? means that the system will choose the specific attributeName value for the current Configuration (not specific to different themes). This should be used only when you want the value to be different on different configurations. For example:
?android:attr/actionBarSize
This line is a dimension, and it will be different not based on the current theme, but on the current device screen size and orientation (values, values-land, values-sw600dp).
It's important to know that specifying ?android: means you are accessing preset Android values, not yours. If you have or want to create and use your own attribute values for specific configurations, you must do the following:
Create a file named attrs.xml on your /values/ folder.
Declare the desired custom attribute:
<?xml version="1.0" encoding="UTF-8"?>
<resources>
<attr name="my_custom_attr" format="reference" />
</resources>
Declare a value for the custom attribute, let's say on your own theme.
<style name="AppTheme" parent="android:Theme.Light">
<item name="my_custom_attr">#resource_type/resource_name</item>
<item name="android:editTextStyle">#style/EditTextAppTheme</item>
<item name="android:buttonStyle">#style/ButtonAppTheme</item>
<item name="android:imageButtonStyle">#style/ImageButtonAppTheme</item>
</style>
And then you can use it on the Widget you'd like:
Hope that clears things out.
EDIT 2
For a better answer to your question, please update your question. And like I said, read more on how to properly create styles.
The Theme named 'one', what do you want to apply it to? An activity, a Widget, the whole Application?
How are you applying the theme? Show the lines of code where you specify the usage of theme 'one'.
Your theme as you specified is simply not a properly constructed theme/style.
<style name='one'>
<item name='android:textColor'>#eea</item>
<item name='android:textSize'>20sp</item>
</style>
This says absolutely nothing, and it is definitely not suitable for an Activity-level theme. The reason you specify a parent is so your theme can inherit all of the attributes from the parent, and then you specifiy which ones to change.
For instance, if you want to use your theme and have a light background, do this:
<style name='one' parent="android:Theme.Holo.Light>
<item name='android:textColor'>#eea</item>
<item name='android:textSize'>20sp</item>
</style>
But even here, despite the fact that it will apply, you don't want to have the same text color and size for the whole application do you? That'd be nonsense, different text color and sizes account for a big part of the user experience, so rather than setting those values from what we can refer to as the main style, we can create substyles and apply them to certain widgets.
I can't really go any more detailed that what I already have, the above explains how to accomplish Widget-specific styling, and activity/application level theming.
For a complete start-up guide, read the Android Developer Site, try the test styles declared there, see how they work, and until then try to create your own, don't try to create something out of nowhere if no reading has been made.

What is difference among styles.xml and themes.xml

May I know what is the difference between styles.xml and themes.xml? To me, they just look same as both XML are in the same format.
<style name="...
<item name="...
So, in my app which provide customization coloring, size, drawable, ... do I need both styles.xml and themes.xml as well? How should I decide which XML to put in which file?
Out of the whole page of the Styles and Themes. You may be looking for this line.
When you apply a style to a single View in the layout, the properties
defined by the style are applied only to that View. If a style is
applied to a ViewGroup, the child View elements will not inherit the
style properties—only the element to which you directly apply the
style will apply its properties. However, you can apply a style so
that it applies to all View elements—by applying the style as a theme.
When you apply as theme, it changes everything in scope, depending if you applied it on Activity or Application. Style is more 'local'.
Quoted from Android API guide:
To create a set of styles, save an XML file in the res/values/ directory of your project. The name of the XML file is arbitrary, but it must use the .xml extension and be saved in the res/values/ folder.
The root node of the XML file must be <resources>.
Full documentation
So I guess it really doesn't matter if you put any styles in any files as long as it's an xml file which locates in res/values/ folder.
There is no functional difference between styles.xml and themes.xml as many answers have indicated.
It is worth noting that Google's iosched2014 app has ONLY a styles.xml (no themes.xml).
https://github.com/google/iosched
Taken from the Styles and Themes document, in the Defining Styles section:
To create a set of styles, save an XML file in the res/values/ directory of your project. The name of the XML file is arbitrary, but it must use the .xml extension and be saved in the res/values/ folder.
The root node of the XML file must be <resources>.
I am just realizing that for example Toolbar text colors go into a theme declaration, and for other aspects go into a style declaration
<style name="ToolbarTheme" parent="ThemeOverlay.AppCompat.Light">
<item name="android:textColorPrimary">#android:color/white</item>
<item name="android:textColorSecondary">#android:color/white</item>
</style>
<style name="ToolbarStyle" parent="Base.Widget.AppCompat.Toolbar">
<item name="android:minHeight">?attr/actionBarSize</item>
<item name="android:background">#color/colorPrimary</item>
</style>
And in the toolbar declaration
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="#style/ToolbarStyle"
android:theme="#style/ToolbarTheme">
</android.support.v7.widget.Toolbar>
I tried to omit the theme by moving its items into the style, but that didn't work. So in this case it seems as these are just styles which are as theme since they affect the inherited children in the toolbar such as the textfields.
Hi here is an article regarding to the difference between styles and themes XML in android please go through
And also here the android documentation regarding to styles and themes in Android.

Android app theme - difference when using theme from style xml file

Why is there a difference between theme defined in AndroidManifest.xml and theme taken from styles.xml?
1) AndroidManifest.xml:
<application ... android:theme="#android:style/Theme.Black">
2) AndroidManifest.xml
<application ... android:theme="#style/AppTheme">
styles.xml
<resources>
<style name="AppTheme" parent="#android:style/Theme.Black" />
</resources>
1st setting gives black theme and no action bar. 2nd has dark action bar and light menu.
EDIT : options 1) and 2) - notice Menu and ActionBar
EDIT 2:
Why doesn't the 2nd option actually use the AppTheme (Theme.Black) ? (tested on SGS3)
You probably have another styles.xml file, perhaps under a directory like "values-v11", that is defining the #style/AppTheme differently than #android:style/Theme.Black and taking precedence over the file you're viewing/modifying.
#android:style/Theme.Black implements the exact theme implemented by Android (or device manufacturer). However, #style/AppTheme allows you to perform custom modification in your theme which actually extends the original Theme.Black from android, and in order to perform custom modifications, you use style resources.
In simple words, its just like using Activity class or YourOwnActivity class which extends Activity with extra features inside.
Styles.xml enables you to create your own themes. In AndroidManifest, you set the theme you want for an app or activity. You may want to use a system theme or your own. You can also extend other themes as you're doing setting "parent" attribute. For further information, check this out:
http://developer.android.com/guide/topics/ui/themes.html
You should try to put:
<resources>
<style name="AppTheme" parent="#android:style/Theme.Black" />
</resources>
in a xml file called res/themes.xml

Android: HowTo make device theme

perhaps I don't know how to search for that - everything I found only is about changing the app theme.
I want to know how I can develop a whole theme for the android device itself?
On the market you'll find many themes out there - how can I build one, too?
I don't want to use 3rd party licences (ThemeMaker etc)...anyone who has a tutorial for me?
Thanks for your help!
You should read up on the Android Developer site - Styles and Themes http://developer.android.com/guide/topics/ui/themes.html
A style is a collection of properties that specify the look and format for a View or window. A style can specify properties such as height, padding, font color, font size, background color, and much more. A style is defined in an XML resource that is separate from the XML that specifies the layout.
Styles in Android share a similar philosophy to cascading stylesheets in web design—they allow you to separate the design from the content.
Apply a theme to an Activity or application
To set a theme for all the activities of your application, open the AndroidManifest.xml file and edit the <application> tag to include the android:theme attribute with the style name. For example:
<application android:theme="#style/CustomTheme">
If you like a theme, but want to tweak it, just add the theme as the parent of your custom theme. For example, you can modify the traditional light theme to use your own color like this:
<color name="custom_theme_color">#b0b0ff</color>
<style name="CustomTheme" parent="android:Theme.Light">
<item name="android:windowBackground">#color/custom_theme_color</item>
<item name="android:colorBackground">#color/custom_theme_color</item>
</style>

Android - how to set background color of all screens?

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?
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" />
Update your android studio to 1.4 it has inbuilt theme editor. as you can see in below image
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).

Categories

Resources