Creating a Theme2:24 with Michael Lustig
Let's take a look at how to create a theme. We'll inherit from parent themes, talk a little bit about the difference between themes and styles, and show you how to apply your theme in your app.
Creating a theme is an identical process to creating a style but 0:00 don't be fooled, they're different things. 0:03 Remember, you can think of themes as applying to each view of a layout, 0:05 whereas the styles we're simply styling that particular view to which the style 0:09 has been applied. 0:13 To create a theme we actually use the style tag. 0:14 I know that can be perhaps more than slightly confusing, 0:16 and I would have preferred that themes be created with maybe theme tags, and 0:19 I couldn't find any documentation to why that isn't the case but 0:23 I'm sure there was a good reason behind that particular design decision. 0:26 So let's create a theme called settings theme and 0:29 set its parent to android:theme.material.noActionBar 0:33 By setting the parent we inherit all of the attributes from that theme. 0:49 Material design was introduced with Android 5.0 Lollipop, and 0:53 provides developers with a great set of tools for 0:56 creating beautiful, elegant user interfaces. 0:58 The material theme in Android has a concept of primary colors and 1:01 accent colors. 1:04 That, when applied to your theme, 1:06 will give your Views personality to match your brand. 1:08 As you can see from the mo-cap on the left we wanna use this cool green color 1:10 in a bunch of different views, using styles to do that would be a pain. 1:14 Believe it or not, we can actually just write one line of code here to apply 1:18 that color to all of the Views in our SettingsActivity. 1:21 We'll do that now by creating a new item with the name android:colorAccent and 1:24 setting the value to our predefined color called SettingsAccent. 1:28 After we've created our theme, 1:36 we need to tell Android to use it with our SettingsActivity. 1:38 To do that I'll just add an android:theme to our SettingsActivity in the manifest 1:41 and we'll be good to go. 1:45 Let's run that and see what it looks like. 1:58 I'll click through the SettingsActivity. 2:03 Awesome, the color was applied to the entire settings screen and we only had to 2:10 apply the style once, rather than to every single view in the SettingsActivity. 2:14 In the next video let's see why we need backwards compatibility, and 2:18 learn how to implement a backwards compatible theme. 2:22
You need to sign up for Treehouse in order to download course files.Sign up