Android Basic Trainning Course: Defining and Using Styles

    This chapter will explore the concept of styles, including how you can create them and how you can apply them to their own widgets.

Create your own style

    The goal of style is to encapsulate a set of attributes that you plan to use continuous, intermittent, or to separate from your own layouts. The main purpose for the use of style is "do not repeat yourself" (DRY) - if you have a bunch of widgets look the same, use the same style to define the "we look the same", rather than copying the values ​​in this style all of this widget to other widgets.
    The above statements  will mean a little more if we look at an example. Open styles.xml file and create a new style like this:
    <style>  tag give us the name of the style, it's what we used to refer to the layout style. The child element <item> of the <style> representation of the attribute value to be applied to any other, that style applies to. Thus, our widget if apply this style will have a relatively large font (attribute android: click Text is set value 30sp) and display its contents will be red (android: textcolor is set value # FFFF0000), output with a TextView and a Button:

Where to apply the style?

    Style can be applied to a widget, and will only affect that widget alone.
    Style can also be applied to a container, and would only affect that container. However, this work does not automatically apply the style to the child elements of the container. For example, suppose the file res/layout/main.xml as follows:
    Output will not have the text of the Button with red and larger font like above, although the style attribute of the root container. Style is only effective to a container, not the contents of the container.
    You can also apply the style to an activity or an application, in case it is referred to as a theme - will be discussed later in this chapter.

Inheriting a style

    You can also specify that you want to inherit the style attributes from a different style, by showing parent property in the <style>. For example, consider the style resource below:
    Here, we specify that inherit the style Theme.Holo in Android. Therefore, in addition to the appointment of all the attribute definitions of ourselves, we specify that we want all the defined attributes from Theme.Holo.
In many cases, this is not necessary. If you do not specify a parent attribute, attribute definitions you will comply with any of the default styles that are applied to the widget and container.
    Style of the Android system is not much mentioned in its document, for that reason that Google recommends users should look at the lists of Android styles for choosing which can be used.
    It also explains why inheriting a style becomes important. In the first example of this section, we inherit from Theme.Holo, because we want to use activatedBackgroundIndicator property from Theme.Holo. The other style is not worth it or not worth that we desire.

Theme: another name of Style

    The theme is also the style, applied to an activity or application, via the attribute android:theme on the <activity> or <application> tag. If your theme is being applied is that you create, simply refer to it via @style/ ... just as you do for a style of a widget. If you applied the theme of Android, basically you will use a value for the prefix @android:style/, such as @android:style/Theme.Holo.Dialog or @android:style/Theme.Appcompat.Light.
    And here is an example of a Theme based on Android default theme:
    In this theme, ActionBar is hidden (by attribute NoActionBar), it can be used in building fullscreen Activity or moreover, you can use Toolbar flexibly by this theme.


    Through this post, I hope that you can learn how to use Style and it's advance feature: Theme to styling your app based on the default. Further, you can read the official docs about Style and Theme by Google or figure out Style resource by this doc page.


Previous post
« Prev Post
Next post
Next Post »