1 00:00:00,450 --> 00:00:03,580 It's great that we've reduced some of the code related to styling our views. 2 00:00:03,580 --> 00:00:06,770 But we still haven't made our managers happy by allowing our app to be run on 3 00:00:06,770 --> 00:00:08,900 devices all the way back to KitKat. 4 00:00:08,900 --> 00:00:10,830 Let's make our first attempt at fixing that. 5 00:00:10,830 --> 00:00:14,640 I'm in the apps build back creative file where I'll change the minSdkVersion to 19, 6 00:00:14,640 --> 00:00:18,330 which corresponds to Android 4.4 KitKat. 7 00:00:18,330 --> 00:00:21,938 Let's sync Gradle and run the app on our emulator with KitKat to see what happens. 8 00:00:23,832 --> 00:00:25,668 Well, the toolbar shouldn't be there but 9 00:00:25,668 --> 00:00:29,910 everything else looks good in the album list activity and album details activity. 10 00:00:29,910 --> 00:00:32,310 What about in the play back activity? 11 00:00:32,310 --> 00:00:34,060 Whoops, there's a crash. 12 00:00:34,060 --> 00:00:37,870 Turns out that there's an image view that's attempting to use a resource that 13 00:00:37,870 --> 00:00:41,291 isn't available on versions of Android before Lollipop or API 21. 14 00:00:41,291 --> 00:00:43,085 I'll show you how to fix that crash and 15 00:00:43,085 --> 00:00:45,920 other issues of backwards compatibility now. 16 00:00:45,920 --> 00:00:49,660 First, we need to grab the Android support library called AppCompat 17 00:00:49,660 --> 00:00:51,080 that I mentioned earlier. 18 00:00:51,080 --> 00:00:53,220 To get the most recent version of AppCompat, 19 00:00:53,220 --> 00:00:56,860 just perform a Google search for support library packages Android. 20 00:00:56,860 --> 00:00:59,820 Click the link titled Support Library Packages and 21 00:00:59,820 --> 00:01:02,130 then click v7 AppCompat library from the sidebar. 22 00:01:03,730 --> 00:01:06,060 The most recent version will be listed below. 23 00:01:06,060 --> 00:01:09,640 Copy that and paste it into your app's build.gradle file along with a compile 24 00:01:09,640 --> 00:01:11,500 statement and some single quotes. 25 00:01:11,500 --> 00:01:14,330 Then when Gradle ask you to sync, go ahead and do so. 26 00:01:17,006 --> 00:01:18,551 Now that we've added AppCompat, 27 00:01:18,551 --> 00:01:21,770 we can begin to fix the errors that were plaguing our application. 28 00:01:21,770 --> 00:01:25,111 The first change we'll make is removing the android:namespace declaration 29 00:01:25,111 --> 00:01:27,630 from the android:background attribute 30 00:01:27,630 --> 00:01:31,166 of the image view in our song playback activity layout that is causing the crash. 31 00:01:31,166 --> 00:01:35,850 The reason the crash was happening here is that on Android version 19, Android can't 32 00:01:35,850 --> 00:01:42,750 find the resource specified by ?:attr/SelectableItemBackgroundBorderless. 33 00:01:42,750 --> 00:01:46,160 That resource simply wasn't available back at that time, so 34 00:01:46,160 --> 00:01:49,830 by removing the add Android we tell the system to look up this value, 35 00:01:49,830 --> 00:01:53,140 selectableItemBackgroundBorderless, within the current theme. 36 00:01:53,140 --> 00:01:56,970 In our case, since we're now going to be using AppCompat, the system can find that 37 00:01:56,970 --> 00:01:59,800 resource and we're good to continue our hunt for errors. 38 00:01:59,800 --> 00:02:04,110 Let's check and fix any issues with our styles.xml file first. 39 00:02:04,110 --> 00:02:07,700 When we first look at this file, we'll see that there's some red error markers. 40 00:02:07,700 --> 00:02:10,290 However, I wanna drive home this point a little further. 41 00:02:10,290 --> 00:02:12,850 So bear with me as I perform a quick little hack to bring these 42 00:02:12,850 --> 00:02:14,430 errors into more focus. 43 00:02:14,430 --> 00:02:17,830 In order to see more obviously what attributes were available in KitKat, 44 00:02:17,830 --> 00:02:19,340 the version that we're targeting, 45 00:02:19,340 --> 00:02:21,760 I'm gonna go into my app's build.grade file again and 46 00:02:21,760 --> 00:02:26,880 temporarily change the compileSdkVersion to 19 to match my minSdkVersion. 47 00:02:26,880 --> 00:02:30,610 Now I'll go back into styles.xml and sync the Gradle files. 48 00:02:31,680 --> 00:02:34,520 If you're following along and you get an error message about failing to 49 00:02:34,520 --> 00:02:39,290 find target with hashstring Android 19, just click Install Missing Platforms and 50 00:02:39,290 --> 00:02:41,690 Sync Project and Android Studio will do the rest. 51 00:02:43,460 --> 00:02:46,720 After verifying you have the older version of the Android SDK installed and 52 00:02:46,720 --> 00:02:51,010 successfully synced Gradle, a bunch of errors will pop up and that makes sense. 53 00:02:51,010 --> 00:02:54,330 We just told Android to use an older version of the platform so it can't find 54 00:02:54,330 --> 00:02:58,180 the resources it needs for the material theme that's currently applied to our app. 55 00:02:58,180 --> 00:03:01,826 It's safe to ignore these errors for now, though, as we're only temporarily lowering 56 00:03:01,826 --> 00:03:04,465 the compile list decay to perform some maintenance for the app. 57 00:03:04,465 --> 00:03:07,869 When I synced my Gradle files, a file called 58 00:03:07,869 --> 00:03:14,900 ldltr-v21/values/ltltr-v21.xml pops up, and I get an error message. 59 00:03:14,900 --> 00:03:18,930 It's safe to close the file and navigate back to the styles.xml file now. 60 00:03:18,930 --> 00:03:22,433 Now the errors in the styles.xml file are lit up like a Christmas tree and 61 00:03:22,433 --> 00:03:23,877 they are much easier to spot. 62 00:03:23,877 --> 00:03:27,387 What we are going to do now is replace the previous attributes from the Android 63 00:03:27,387 --> 00:03:30,100 platform with the attributes from the AppCompat library. 64 00:03:30,100 --> 00:03:34,550 First, we will change the theme to inherit from theme.appcompat.NoActionBar for 65 00:03:34,550 --> 00:03:36,740 both the app theme and the settings theme. 66 00:03:36,740 --> 00:03:40,741 To select the next occurrence of the highlighted text, click Ctrl+G. 67 00:03:40,741 --> 00:03:43,780 I find that's an incredibly useful trick in any IDE. 68 00:03:43,780 --> 00:03:46,632 Then we'll need to change android:colorAccent, 69 00:03:46,632 --> 00:03:50,585 which sets the accent color for the android:theme.material themes, 70 00:03:50,585 --> 00:03:54,278 to set the accent color for the Theme.AppCompat themes by removing 71 00:03:54,278 --> 00:03:57,410 the android:namespace before the words colorAccent. 72 00:03:57,410 --> 00:03:59,956 You may notice that we still use the @android for 73 00:03:59,956 --> 00:04:03,410 setting the color itself to @android:color/darker_gray. 74 00:04:03,410 --> 00:04:07,220 We can do this because the color darker gray still exists on the platform on older 75 00:04:07,220 --> 00:04:08,110 versions. 76 00:04:08,110 --> 00:04:12,900 However, it's the color accent property that did not exist back on API 19. 77 00:04:12,900 --> 00:04:15,717 Next we'll remove the android: in our playback button style, 78 00:04:15,717 --> 00:04:19,379 as well as in our settings toolbar style, and that should do it for our styles file. 79 00:04:23,447 --> 00:04:25,570 It is now backwards compatible. 80 00:04:25,570 --> 00:04:28,440 However, there is still some work to do to complete the process.