Android Themes, Styles and Attributes

One thing that bugs me from time to time is the appearance and disappearance of the Action bar or the change in its UI with similar XML code and Java API calls but with different themes. I was somehow managing till now but then decided that it’s probably a good time to understand the concept of themes and styles in android to make sure I know why there’s that frustrating difference just by a small change in the theme name (res/values/styles.xml then used in AndroidManifest.xml) that you sometimes do not even realize while creating new projects as it is set automatically based on the API levels you target.

I’m not going to get into full details but the aim of this small article is to sort of compile all sorts of useful resources with regards to themes and styles so that one can quickly go through them and understand. The difference not only affects Action bars but also all sorts of other UI elements including the Window itself that actually gets styled by the themes you specify. Some ship with the app compat support library, some ship with the Android platform itself, some ship with some other library and some can be custom made by yourself or a teammate.

What's the one thing every developer wants? More screens! Enhance your coding experience with an external monitor to increase screen real estate.

Consider going through these resources:

Once your styles, themes and even other resources like dimensions, strings, etc. are defined in their respective resource files (can also be combined but keep separate for sanity), they can be used in the layout files like android:text="@android:string/string_name or android:text="@string/string_name where the former is defined in the platform whereas the latter in your own app.

It is also very important to understand attributes that you can access using this syntax – ?android:attr/someAttribute – where the value of the attribute is generally defined in themes.xml and the definition of attribute (that it can exist) is defined in attrs.xml. Here are some very useful link on this concept of attributes:

So if we take this code for example:

<TextView android:id="@android:id/text1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="?android:attr/listPreferredItemPaddingLeft"
    android:layout_marginTop="8dip"
    android:textAppearance="?android:attr/textAppearanceListItem"
    />

Each of them, i.e., android:marginTop for instance is an attribute that’ll be defined by the platform where android is the package name and marginTop is the attribute name. Similarly listPreferredItemPaddingLeft is another attribute defined first and then somewhere in the themes XML resource its value is set. It is then accessed with that syntax ?[package]:attr/attr_name.

Hope that helps!

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Author: Rishabh

Rishabh is a full stack web and mobile developer from India. Follow me on Twitter.

Leave a Reply

Your email address will not be published. Required fields are marked *