THEMES PART II
What are styles and themes and their difference
How a programmer can create a style
Inheritance of system styles
from inheriting custom styles. You only have to change the way you communicate
the parent-child relationship to android.
childStylewill use all
parentStyleand can define its own as
well to override
with the whole notation in your layouts like so:
all styles available to
childStyle, and may be adds a
new attribute, just go ahead and create a new style and call it
reference it from your layouts like so:
attribute to your
Button and TextView explicitly inherit from the parent Button and TextView
style for a view, first inherit all attributes of the parent style in the
system and then go ahead and add your own attributes.
doing implicitly (by default). I.e. android has been inheriting all default styles
for our Buttons and TextViews and simply
taking our explicitly defined attributes as overrides.
custom styles and default system styles.
styles which determine the visual representation of your app (how your app
looks). I believe the idea is now clearer, since we have just covered styles.
only a set of styles grouped under one parent (the theme).
A style and theme are both defined in
values/*.xml files with root element
The child element of root i.e.
styleworks for both styles and themes. The
nameattribute references either the theme name or the
style name. likewise the
parentattribute references a
theme or a style any of them is inheriting from. Only that of course, a theme
inherits from another theme and a style, from another style.
references an attribute for both. For a theme, each item element references the
nameof style resource and for a style, each item
element references the
nameof an attribute.
file, it’s the android default theme for API level 10 and lower. And this styles.xml file which defines the global theme styles of android.
to your application or activity
stylewhose scope is
limited to a single view, the scope of a theme spans a whole
remember?, like so;
Theme.Black.), for my API level of 8, it’s the default.
file, you should see several themes which all inherit
Theme.Light.NoTitleBaretc. Your task is just to
familiarize yourself with the themes available for your target API level since
each new API level almost ships with new themes. You can actually tell your app
to detect the API level and apply a theme accordingly. That is a topic for
Theme.Light. Just go to your manifest file and add this attribute to the
Applicationelement and of course you now know that
you can as well choose to apply it to a specific
|screen shot with Theme.Light|
specific theme, we are going to create our own theme and make it first inherit
Theme.Light. This will help us understand:
How to create custom themes
How to inherit styles of a system them
How to apply custom themes to activity or
directory in our ThemeAndStyle
project created in part I of this series. Right click and go to New->android
xml. And call it themes.
res/valuesdirectory and select the android xml icon in your IDE toolbar.
idea, however all of them are defined in the same file. Here is the content of themes.xml
they really don’t inherit from any custom theme, but are rather used for
clarity since we are creating 3 different basic themes.
android namespaceand the
Notice the little tweak we have added to
system theme that inherits from
adds a single property i.e. hides the title bar.
item elementunder a theme
definition represents a whole style definition, contrary to the styles where an
item elementrepresents a single attribute. The style
names you are seing in the
item element’s are the
system names referencing styles for the corresponding views (Button and
TextView). You must have noticed that if you took a look at the system themes.xml. The philosophy is simple,
we are inheriting a system theme, so each
gives us an opportunity to override an existing style in the system theme,
similar to styles.xml, where we
inherit a system style applying to a view and then in each
item, we override a given attribute.
are references to styles we have to define. As you can see, we shall define
style attributes for each view under each theme. Interesting right???
names, create a second
stylesxml file and call it styles2.xml, below is the content.
inheriting any custom styles here, it’s just any ordinary string but I wanted the style names to be clear
since I have created 3 separate styles corresponding to each theme above.
style definitions as well as any color style attributes. Let us leave this
style file strictly for layout attributes such as
layout_height. Remove also the inheritance and just
leave it as an isolated style definition.
shall see how to apply a custom theme to an
android namespaceand just reference the theme using a
style reference like so:
Theme.Red, you can go ahead and test with
created and applied.
to look at switching themes programmatically and then see how to use simple and
complex colors to give a powerful visual to our user interface.