

For example, here's how you would apply LeftAlignedView to the second TextView: Once a style has been created, you can apply it to any view. You can take a look at the definition of the style by opening res/values/styles.xml. The TextView now has a style attribute that points to the LeftAlignedView style. When you press OK, you'll see that the code for the first view has changed. Let the name be LeftAlignedView and select all the attributes except layout_marginTop. You will now see a dialog where you can give a name to the style and select the attributes that should be included in it.

You can do so by right-clicking on the TextView and selecting Refactor > Extract > Style. One way to avoid the duplication and make the styling of these Views easier is to define a separate style that holds the values of all these common attributes.

The situation would get worse if there were more such elements. You will have to make changes in two different places for it to work. Let's say you wanted to change the layout_marginStart value for these TextView elements. Īs you can see in the above code, properties such as layout_width, layout_height, and layout_marginStart are explicitly included in the definition of each view, and they have the same value. Therefore, let's start by creating a new empty activity and adding two TextView elements to its layout XML file. Styles are obviously applied to UI components. You'll also learn how styles and themes are different from each other and how to use them properly. In this tutorial, you'll learn how to create custom styles and themes for your Android apps. If you are familiar with web development, the first approach is akin to using inline CSS styles, and the second approach is akin to using stylesheets. The second approach involves creating and using custom styles and themes. This approach is feasible only if you are working on a simple app that has a small number of views and activities. The first approach involves directly modifying the properties of views in layout XML files. There are two approaches to modifying the look of Android apps. If you find that hard to believe, I suggest you take a quick look at the apps in the Top Charts section of Google Play. On Google Play, which hosts well over a million apps today, looks are just as important as functionality, if not more. Functionality alone, however, is rarely enough. As Android developers, we tend to focus primarily on the functionality of our apps.
