AppCompat and Themes4:29 with Michael Lustig
Let's make our first attempt at running the application on older devices and see what looks the same and what looks different. We'll also explore potential crashes and their fixes that we’ll have to keep in mind when backporting our visual look and feel of our app. AppCompat is our friend here, and we'll introduce you to grabbing the bits for that library in this video.
It's great that we've reduced some of the code related to styling our views. 0:00 But we still haven't made our managers happy by allowing our app to be run on 0:03 devices all the way back to KitKat. 0:06 Let's make our first attempt at fixing that. 0:08 I'm in the apps build back creative file where I'll change the minSdkVersion to 19, 0:10 which corresponds to Android 4.4 KitKat. 0:14 Let's sync Gradle and run the app on our emulator with KitKat to see what happens. 0:18 Well, the toolbar shouldn't be there but 0:23 everything else looks good in the album list activity and album details activity. 0:25 What about in the play back activity? 0:29 Whoops, there's a crash. 0:32 Turns out that there's an image view that's attempting to use a resource that 0:34 isn't available on versions of Android before Lollipop or API 21. 0:37 I'll show you how to fix that crash and 0:41 other issues of backwards compatibility now. 0:43 First, we need to grab the Android support library called AppCompat 0:45 that I mentioned earlier. 0:49 To get the most recent version of AppCompat, 0:51 just perform a Google search for support library packages Android. 0:53 Click the link titled Support Library Packages and 0:56 then click v7 AppCompat library from the sidebar. 0:59 The most recent version will be listed below. 1:03 Copy that and paste it into your app's build.gradle file along with a compile 1:06 statement and some single quotes. 1:09 Then when Gradle ask you to sync, go ahead and do so. 1:11 Now that we've added AppCompat, 1:17 we can begin to fix the errors that were plaguing our application. 1:18 The first change we'll make is removing the android:namespace declaration 1:21 from the android:background attribute 1:25 of the image view in our song playback activity layout that is causing the crash. 1:27 The reason the crash was happening here is that on Android version 19, Android can't 1:31 find the resource specified by ?:attr/SelectableItemBackgroundBorderless. 1:35 That resource simply wasn't available back at that time, so 1:42 by removing the add Android we tell the system to look up this value, 1:46 selectableItemBackgroundBorderless, within the current theme. 1:49 In our case, since we're now going to be using AppCompat, the system can find that 1:53 resource and we're good to continue our hunt for errors. 1:56 Let's check and fix any issues with our styles.xml file first. 1:59 When we first look at this file, we'll see that there's some red error markers. 2:04 However, I wanna drive home this point a little further. 2:07 So bear with me as I perform a quick little hack to bring these 2:10 errors into more focus. 2:12 In order to see more obviously what attributes were available in KitKat, 2:14 the version that we're targeting, 2:17 I'm gonna go into my app's build.grade file again and 2:19 temporarily change the compileSdkVersion to 19 to match my minSdkVersion. 2:21 Now I'll go back into styles.xml and sync the Gradle files. 2:26 If you're following along and you get an error message about failing to 2:31 find target with hashstring Android 19, just click Install Missing Platforms and 2:34 Sync Project and Android Studio will do the rest. 2:39 After verifying you have the older version of the Android SDK installed and 2:43 successfully synced Gradle, a bunch of errors will pop up and that makes sense. 2:46 We just told Android to use an older version of the platform so it can't find 2:51 the resources it needs for the material theme that's currently applied to our app. 2:54 It's safe to ignore these errors for now, though, as we're only temporarily lowering 2:58 the compile list decay to perform some maintenance for the app. 3:01 When I synced my Gradle files, a file called 3:04 ldltr-v21/values/ltltr-v21.xml pops up, and I get an error message. 3:07 It's safe to close the file and navigate back to the styles.xml file now. 3:14 Now the errors in the styles.xml file are lit up like a Christmas tree and 3:18 they are much easier to spot. 3:22 What we are going to do now is replace the previous attributes from the Android 3:23 platform with the attributes from the AppCompat library. 3:27 First, we will change the theme to inherit from theme.appcompat.NoActionBar for 3:30 both the app theme and the settings theme. 3:34 To select the next occurrence of the highlighted text, click Ctrl+G. 3:36 I find that's an incredibly useful trick in any IDE. 3:40 Then we'll need to change android:colorAccent, 3:43 which sets the accent color for the android:theme.material themes, 3:46 to set the accent color for the Theme.AppCompat themes by removing 3:50 the android:namespace before the words colorAccent. 3:54 You may notice that we still use the @android for 3:57 setting the color itself to @android:color/darker_gray. 3:59 We can do this because the color darker gray still exists on the platform on older 4:03 versions. 4:07 However, it's the color accent property that did not exist back on API 19. 4:08 Next we'll remove the android: in our playback button style, 4:12 as well as in our settings toolbar style, and that should do it for our styles file. 4:15 It is now backwards compatible. 4:23 However, there is still some work to do to complete the process. 4:25
You need to sign up for Treehouse in order to download course files.Sign up