Android - Delete a style attribute from a parent style

   Android's style framework is quite powerful and offers different customization options without having to change the source code. Developers could just extend base style and override existing attributes or provide new ones.

  A custom style based on TextAppearance.StatusBar.EventContent.Title might want to change the text style,

    <style name="TextAppearance.StatusBar.EventContent.Title">
        <item name="textColor">#ffffff</item>
        <item name="fontFamily">sans-serif-light</item>
        <item name="textSize">@dimen/notification_title_text_size</item>
        <item name="textStyle">bold</item>
    </style>

    <style name="TextAppearance.StatusBar.EventContent.Title.Italic">
        <item name="textStyle">italic</item>
    </style>

  But what if there is a need to completely remove a particular attribute value from the parent style? The logical response would be as to why bother using a style if we don't like its default behavior and why not fork the entire style and remove the property from the forked style? This works fine to an extent but has a drawback when the parent style is a platform style. Developers have to play catch up with platform specific definition of the style with every new version of Android. In this case, developers need to have api level specific folders like values-20, values-21 and keep forking the parent style into each one of these and keep looking out in future releases of Android, too much of a pain for a simple task.

  Fortunately, Android offers an easy alternative to achieve this. The undesired attributes can be deleted from a parent style just by specifying @null as its value.

    <style name="TextAppearance.StatusBar.EventContent.Title.NoTextStyle">
        <item name="textStyle">@null</item>
    </style>


No comments: