Android Programming The Big Nerd Ranch Guide by Bill Phillips, Chris Stewart, Kristin Marsicano (z-lib.org)

(gtxtreme123) #1

Chapter 22  Styles and Themes


Modifying the theme


When you created BeatBox, it was given a default theme. Navigate to AndroidManifest.xml and look
at the theme attribute on the application tag.


Listing 22.6  BeatBox’s theme (AndroidManifest.xml)


<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.bignerdranch.android.beatbox" >


<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
...




The theme attribute is pointing to a theme called AppTheme. AppTheme was declared in the styles.xml
file that you modified earlier.


As you can see, a theme is also a style. But themes specify different attributes than a style does (as you
will see in a moment). Themes are also given superpowers by being declared in the manifest. This is
what causes the theme to be applied across the entire app automatically.


Navigate to the definition of the AppTheme theme by Command-clicking (Ctrl-clicking) on @style/
AppTheme. Android Studio will take you to res/values/styles.xml.


Listing 22.7  BeatBox’s AppTheme (res/values/styles.xml)





...

(As of this writing, when new projects are created in Android Studio, they are given an AppCompat
theme. If you do not have an AppCompat theme in your solution, follow the instructions from
Chapter 13 to convert BeatBox to use the AppCompat library.)


AppTheme is inheriting attributes from Theme.AppCompat.Light.DarkActionBar. Within AppTheme,
you can add or override additional values from the parent theme.


The AppCompat library comes with three main themes:



  • Theme.AppCompat – a dark theme

  • Theme.AppCompat.Light – a light theme

Free download pdf