Introduction
A style (similar to stylesheets in web design) is a collection of attributes that specify the appearance for a single View. It can specify attributes such as font color, font size, background color, and much more. A theme is a type of style that’s applied to an entire app, activity, or view hierarchy. When you apply style as a theme, every view in the app or activity applies each style attribute that it supports.
Styles and themes are declared in a style resource file in res/values/styles.xml
.
Create Theme
To create a new theme, follow these steps in res/values/styles.xml
- Add a <style> element with a name that uniquely identifies the style.
- Add an <item> element for each style attribute you want to define. The name in each item specifies an attribute, value in the <item> element is the value for that attribute.
To extend a style, specify the base style using parent
attribute. You can then override the inherited style attributes and add new ones.
<style name="Theme" parent="Theme.AppCompat.Light.DarkActionBar"> <item name="colorPrimary">#FF0000</item> <item name="colorPrimaryDark">#0000FF</item> <item name="colorAccent">#00FF00</item> <item name="actionMenuTextColor">#0000FF</item> <item name="android:textColor">#00FF00</item> </style>
In the above example, Theme
derived from parent theme, where
- colorPrimary – Color of the app bar.
- colorPrimaryDark – Color of the status bar and contextual app bars; this is normally a dark version of colorPrimary.
- colorAccent – Color of UI controls such as check boxes, radio buttons, and edit text boxes.
- windowBackground – Color of the screen background.
- textColorPrimary – Color of UI text in the app bar.
- statusBarColor – Color of the status bar.
- navigationBarColor – Color of the navigation bar.
Apply Theme
Apply a theme with the android:theme
attribute on either the <application>
tag or an <activity>
tag in the AndroidManifest.xml
file.
<!--Code snippet of AndroidManifest.xml --> <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.aphalaprepsunaa.picturepuzzle"> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <application android:theme="@style/AppTheme"> <activity android:name=".activity.SplashActivity" android:theme="@style/SplashTheme"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>