Cleaning Up with Styles

Now that you have a working project, take a look at your layout XML. Each button is identical. That is fine for now, but if you want to add an attribute to a button, you will have to repeat your work three times. And what about when you have twelve buttons?

Happily, Android has UI styles, which are meant to eliminate that repetition. Style resources are similar to CSS styles. Each style defines a set of XML attribute-value pairs. Styles can be organized into a hierarchy: a child has the same attributes and values as its parent, but may override them or add additional values.

Like string resources, styles are defined inside of a <resources> tag in an XML file in your res/values folder. Also like strings, the filename ...

Get Android Programming: The Big Nerd Ranch Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.