Working with Themes Android

A theme is a collection of one or more styles (as defined in the resources) but instead of applying the style to a specific control, the style is applied to all View objects within a specified Activity. Applying a theme to a set of View objects all at once simplifies making the user interface look consistent and can be a great way to define color schemes and other common control attribute settings.

An Android theme is essentially a style that is applied to an entire screen. You can specify the theme programmatically by calling the Activity method setTheme() with the style resource identifier. Each attribute of the style is applied to each View within that Activity, as applicable. Styles and attributes defined in the layout files explicitly override those in the theme.

For instance, consider the following style:

You can apply this as a theme to the whole screen, which causes any view displayed within that Activity to have its gravity attribute to be right-justified. Applying this theme is as simple as making the method call to the setTheme() method from within the Activity, as shown here:

You can also apply themes to specific Activity instances by specifying them as an attribute within the <activity> element in the AndroidManifest.xml file, as follows:

Unlike applying a style in an XML layout file, multiple themes can be applied to a screen. This gives you flexibility in defining style attributes in advance while applying different configurations of the attributes based on what might be displayed on the screen. This is demonstrated in the following code:

In this example, both the right style and the green_glow style are applied as a theme to the entire screen. You can see the results of green glow and right-aligned gravity, applied to a variety of TextView controls on a screen, as shown in Figure. Finally, we set the layout to the Activity. You must do this after setting the themes. That is, you must apply all themes before calling the method setContentView() or the inflate() method so that the themes’ attributes can take effect.

Packaging styles for glowing text, padding, and alignment into a theme.

Packaging styles for glowing text, padding, and alignment into a theme

A combination of well-designed and thought-out themes and styles can make the look of your application consistent and easy to maintain. Android comes with a number of built-in themes that can be a good starting point. These include such themes as Theme_Black, Theme_Light, and Theme_NoTitleBar_Fullscreen, as defined in the android.R.style class. They are all variations on the system theme, Theme, which builtin apps use.



Face Book Twitter Google Plus Instagram Youtube Linkedin Myspace Pinterest Soundcloud Wikipedia

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Android Topics