It’s a good habit to use styles in your Android application to do layouts or style fonts that occur in multiple places. It also makes it a lot easier to change the layout of your app later. And styles in Android get even cooler once you figure out how to reuse them!
In a project at work, we have a style called “NormalText” which takes care of a lot of the text in the app. So instead of this:
You can just do this:
Thanks to this styles.xml file:
##Inheritance Now if you need to layout some text with the same properties as another style, this is actually pretty straight forward in Android. All you need to do is prefix your new style name with the name of the parent style.
Not only will this make it easier to create styles, it will also make it easier to change styles. If you need the font to be bigger, you only need to change it in one place. This makes maintaining styles a lot easier!
##Inherit from built-in Android styles There are also a bunch of built-in Android styles available when you’re developing an app. It is possible to inherit from these styles as well, but you need to take a different approach. Instead of prefixing the name, you set the parent attribute of the style you’re creating.
The sad thing is that the built-in Android styles are not really documented by Google. All you get is a list of the constants in the R.style class. So to actually figure out what these styles look like, you will have to experiment with them. I tried to find an overview online, but it seems that doesn’t exist. If you know of such an overview, please let me know, so I can link it here - and use it myself :-)
This simple technique can speed up styling in your app and makes it a lot easier to reuse styling you already did. And don’t forget to take advantage of the built-in Android styles!