Fixing Compatibility Issues3:08 with Michael Lustig
In the previous video, we fixed the style so that it would work on older versions of Android. However, there's still some work to do. In this video, we start by fixing our activity_settings.xml and SettingActivity.java files. We'll finally run the app and try to spot any lingering differences between how it looks on newer and older versions of Android.
Let's now go into our activity_settings XML file and see what we need to fix here. 0:00 First, you'll notice that the toolbar has a red error line under it. 0:05 If we hover over the error message, we'll see that the toolbar widget wasn't 0:09 available on the platform in API level 19 and was introduced in API level 21. 0:12 To fix this problem, the AppCompat team created an identical view widget called 0:17 Toolbar that sits in the android.support.v7.widget package. 0:21 So, we'll replace the current toolbar with that now. 0:25 That should take care of the activity_settings.xml file. 0:30 The next thing we'll do is look at the activity files themselves and 0:33 see what needs to change. 0:36 Let's take a look at the SettingsActivity.java file. 0:38 There are a handful of errors we need to take care of here. 0:41 First, you'll see that the toolbar is red. 0:44 Hovering over that, we're told that the toolbar can't be found. 0:47 That's because exactly like the widget in the activitt_setting.xml file, 0:50 toolbar isn't available to use on API level 19. 0:54 We'll change the import statement for the toolbar to use the support toolbar and 0:57 that error message will go away. 1:00 Next, it's very important that we should change all of our activities to extend 1:03 from AppCompatActivity, rather that simply activity. 1:07 One requirement for using AppCompatActivity is that the theme that 1:10 the activity uses must extend from AppCompat. 1:13 Since we already changed that, we're good to go. 1:16 If your theme does not extend from AppCompat, 1:18 your app will crash if you attempt to start an AppCompat activity. 1:20 Lastly, in this file, we need to set up the SupportActionBar, 1:24 rather than simply saying setActionBar. 1:27 Along the same lines, 1:30 we need to change our getActionBar call to use getSupportAcitonBar. 1:31 If we don't, 1:35 we'll get a NullPointerException when that line is executed. 1:35 Now that we've got all this visible errors taken care of in the activity, 1:39 let's run the app and see what happens. 1:42 Before we run the app though, we'll have to revert our hack in the build.gradle 1:44 file that lowered our compile SDK version. 1:47 I'll do that real quick and then bring up an emulator running Android 25 as well as 1:49 an emulator running Android 19 so we can compare any differences. 1:53 Clicking through on the newer emulator, the app looks good. 2:03 Let's check out the emulator running the older version of Android. 2:11 List screen looks good, album screen looks good, but the slider for 2:15 the song playback screen looks different. 2:19 It looks like it belongs on an older version of Android. 2:22 Let's check out the settings activity before going back into Android Studio 2:24 to fix these errors. 2:27 In the settings activity, 2:29 the switches look really weird, they don't seem to fit in with our current theme. 2:30 Let's first go back into the SongPlaybackActivity and 2:34 see what's going on. 2:36 There aren't any errors. 2:40 But we haven't changed this activity or 2:41 the album list or detail activities to extend from AppCompatActivity yet. 2:42 Remember, for a consistent visual look and 2:47 feel, it's important to extend your activities from AppCompatActivity. 2:49 Let's go ahead and do that for the SongPlaybackActivity now and 2:52 run the app again. 2:55 Wonderful, now our song playback activity looks the exact same on 3:03 both versions of Android. 3:06
You need to sign up for Treehouse in order to download course files.Sign up