1 00:00:00,700 --> 00:00:04,340 All right let's take a look at how to set portrait only mode in code. 2 00:00:04,340 --> 00:00:06,140 Once again, this video is optional, 3 00:00:06,140 --> 00:00:09,410 you don't need to watch this video to complete the section or the course, but 4 00:00:09,410 --> 00:00:11,850 it's here if you wanna go a bit further, and try this out. 5 00:00:11,850 --> 00:00:14,790 Don't worry if you don't fully understand everything we cover here, but 6 00:00:14,790 --> 00:00:18,270 certainly feel free to ask questions and discuss with our community, right. 7 00:00:18,270 --> 00:00:20,830 So to start, I'm going to get rid of the screen orientation 8 00:00:20,830 --> 00:00:23,590 attribute we just added, make sure the angle bracket is still there. 9 00:00:24,700 --> 00:00:27,740 And now, let's take a look at a helpful stack overflow question about this 10 00:00:27,740 --> 00:00:28,790 exact problem. 11 00:00:28,790 --> 00:00:31,960 So here the question is, how to set an entire application in portrait mode only? 12 00:00:31,960 --> 00:00:36,940 If we go down to the first answer, we see a helpful response, this is for 13 00:00:36,940 --> 00:00:41,160 any Android version, but since our app is targeting API level 16 and up, 14 00:00:41,160 --> 00:00:44,480 we can use this other solution down here, which I like better. 15 00:00:44,480 --> 00:00:48,150 So, the basic idea is that we wanna create custom activity lifecycle listeners, 16 00:00:48,150 --> 00:00:50,900 inside of custom application class. 17 00:00:50,900 --> 00:00:55,235 When an activity switched from portrait to landscape, it goes through a number of 18 00:00:55,235 --> 00:00:59,190 lifecycle methods, it's a actually destroyed, and then recreated. 19 00:00:59,190 --> 00:01:01,490 And when it's recreated in this other mode, 20 00:01:01,490 --> 00:01:04,360 it gets a different layout if needed. 21 00:01:04,360 --> 00:01:07,850 So we wanna do, is attach a listener to those events, and 22 00:01:07,850 --> 00:01:10,770 then when a certain event is called when, the activity is created, 23 00:01:10,770 --> 00:01:15,050 we want to force it, into SCREEN_ORIENTATION_PORTRAIT. 24 00:01:15,050 --> 00:01:16,990 All right, let's see how this works in code. 25 00:01:16,990 --> 00:01:19,710 So we'll start by creating a custom application class, 26 00:01:19,710 --> 00:01:24,530 right click on our main package here, select New, Java Class, 27 00:01:24,530 --> 00:01:28,460 let's call this InteractiveStoryApplication. 28 00:01:28,460 --> 00:01:31,182 By convention, if we do a custom application class, 29 00:01:31,182 --> 00:01:33,366 we usually end with the word Application. 30 00:01:33,366 --> 00:01:36,107 As a Superclass, let's start typing Application, and 31 00:01:36,107 --> 00:01:39,610 we can select android.app.Application, and click OK. 32 00:01:39,610 --> 00:01:43,120 Okay, so here we have our new class, and it's extending our base application. 33 00:01:43,120 --> 00:01:47,660 The base application class is used when an app first start ups, and 34 00:01:47,660 --> 00:01:49,900 there are methods there that happen by default. 35 00:01:49,900 --> 00:01:53,850 Now that we're using a custom one, we need to specify that in the manifest itself, so 36 00:01:53,850 --> 00:01:57,950 here for the application element, we want to add a new attribute. 37 00:01:57,950 --> 00:02:00,740 So drop it on a new line, and type android:name and 38 00:02:00,740 --> 00:02:05,490 then in here we want the value, we can start typing InteractiveStory, and 39 00:02:05,490 --> 00:02:08,020 from auto complete, we can select our new class. 40 00:02:08,020 --> 00:02:10,630 So now, Android knows when our app starts to 41 00:02:10,630 --> 00:02:14,230 use this custom application class instead of the default one. 42 00:02:14,230 --> 00:02:20,810 Back in our class, we want to override the onCreate method, so start typing onCreate, 43 00:02:20,810 --> 00:02:24,270 and we can select it from auto complete, and this is the onCreate method for 44 00:02:24,270 --> 00:02:28,300 the entire application, not an individual activity like we've seen before. 45 00:02:28,300 --> 00:02:33,110 So back in our sample code we see that, inside onCreate we want to register a new 46 00:02:33,110 --> 00:02:37,520 ActivityLifeCycleCallBack, and then in there we're gonna use a specific method. 47 00:02:39,050 --> 00:02:42,250 So, let's add a new line after super.onCreate, we wanna make sure we call 48 00:02:42,250 --> 00:02:45,890 that, so that it does all the functionality in the superclass. 49 00:02:45,890 --> 00:02:50,420 And now we wanna type register, and there it is, ActivityLifecycleCallbacks. 50 00:02:50,420 --> 00:02:54,290 And here we can add a new anonymous inner class, we can type new 51 00:02:54,290 --> 00:02:59,090 ActivityLifecycleCallbacks, hit Enter, and we get a bunch of methods we can override. 52 00:02:59,090 --> 00:03:00,400 Now we don't have to do anything here, 53 00:03:00,400 --> 00:03:03,370 we're only going to override this first one onActivityCreated, 54 00:03:03,370 --> 00:03:07,570 but you can see there are other methods that we could override for other reasons. 55 00:03:07,570 --> 00:03:12,399 So in here, when an activity is created, we want to force it to portrait mode, 56 00:03:12,399 --> 00:03:16,204 we do that by referencing the activity that's passed in, and 57 00:03:16,204 --> 00:03:20,668 on it we call setRequestedOrientation, and we set it to portrait mode, 58 00:03:20,668 --> 00:03:23,156 which we get through ActivityInfo, and 59 00:03:23,156 --> 00:03:27,663 I just remember this from the stack overflow question, ActivityInfo. 60 00:03:27,663 --> 00:03:31,008 And here we have a bunch of screen orientation parameters, and 61 00:03:31,008 --> 00:03:34,670 we want SCREEN_ORIENTATION_PORTRAIT, there we go. 62 00:03:34,670 --> 00:03:36,840 So, if we run this we should be able to see, 63 00:03:36,840 --> 00:03:39,520 if we change our emulator to landscape mode, it should stay portrait. 64 00:03:40,990 --> 00:03:44,490 Okay it loads up, and we can use the buttons here to switch, and sure enough, 65 00:03:44,490 --> 00:03:45,920 it doesn't change to landscape view. 66 00:03:45,920 --> 00:03:47,220 Cool, that's exactly what we wanted.