Last Updated:

Styles and themes in Android

This article begins a series of lessons on programming for Android. To begin with, we will get acquainted with the tools and components for working with the appearance of applications, namely styles and themes in Android.

The appearance of the application is an important part of the development for Android and requires special attention from the developer. Therefore, in this article we will learn how to use existing and create new styles and themes for Android applications.

Design Patterns in Android

Android is the most popular mobile platform today. As in any other platform, Android offers established rules and approaches to application development. This is all done to make your application look beautiful, simple and be as productive as possible. Google developers regularly publish best practices for developing applications for Android and advise you to follow their recommendations.

Use your styles in Android

The Android platform supports the creation of new custom widgets using styles.

A style is a set of properties that define the appearance and display of a widget on devices with different screen sizes. You can use styles to set properties such as height, padding, font color and size, as well as background color, and more. In Android, styles work similarly (cascading style sheets) to those used in web development.CSS

 

In Android, styles are XML files that are placed in the folder of your Android project. At the root of this XML file, the tag and the tag attached to it must be used:/res/values<resources><style>

As you can see from the example above, this tag contains several tags that define the colors that will be used in the theme. For example, in the example below, we'll create . To do this, we will use the standard Android XML layout editor:<item>TextView

This is a rather cumbersome markup code, but for one view "will go", but what if we still want to use the same markup for some , for example, in other places of our application? The first thing that comes to mind is copying the same style, but this will lead to two problems:TextView

  1. What if in a month we want to slightly change the design of the android application? In this case, we will have to make changes to all the places where the same "copy-paste" style was used. And I know it's going to take a lot of time.
  2. Re-writing the same code will eventually result in a large application that is unrealistically effective to maintain.

To avoid such problems, we immediately need to do the right thing and declare the style for . The same style can be used multiple places in our android app. A change in the style itself will automatically be picked up in other views where it is used. Let's take a look at the code snippet below:TextView

As you can see, refers to the style .TextView@style/mTextViewStyle

Create a style in Android

An XML file with styles can be created once and used in other layouts. To do this, you create a new XML file and save it to your project folder. As you already know, there should be a tag at the root of the file.res/values/<resources>

Several styles can be defined in a single file. For each style that you want to define in the file, you must add a tag with a unique style ID. Then, add a tag for each property of that style. The tag value can be a string, a hexadecimal color code, a reference to another resource type, or another value, depending on the style property.style.xmlstyle.xml<style><item><item>

Let's look at an example:TextView

Apply the created style

 

Apply a style to a theme

A theme is a style that is applied to or the application as a whole.Activity

Creating a theme is similar to creating a style. Let's see an example:

Note that this style overrides the default theme and overrides the value .android:Theme.Lightandroid:windowNoTitle

To set this theme for all of your apps, open the file and edit the tag by adding an attribute with a style name. Example:ActivityAndroidManifest.xml<application>android:theme

If you want to apply the theme to only one activity of your app, then simply add the attribute to the .android:theme<activity>

This was the first article in a series of Android programming lessons for beginners. Stay tuned and share the article with your friends!