Optional: Setting Portrait-Only in Code3:47 with Ben Jakuben
You can skip this video if you'd like! This is an optional solution to make your app force all Activities into portrait-only mode.
All right let's take a look at how to set portrait only mode in code. 0:00 Once again, this video is optional, 0:04 you don't need to watch this video to complete the section or the course, but 0:06 it's here if you wanna go a bit further, and try this out. 0:09 Don't worry if you don't fully understand everything we cover here, but 0:11 certainly feel free to ask questions and discuss with our community, right. 0:14 So to start, I'm going to get rid of the screen orientation 0:18 attribute we just added, make sure the angle bracket is still there. 0:20 And now, let's take a look at a helpful stack overflow question about this 0:24 exact problem. 0:27 So here the question is, how to set an entire application in portrait mode only? 0:28 If we go down to the first answer, we see a helpful response, this is for 0:31 any Android version, but since our app is targeting API level 16 and up, 0:36 we can use this other solution down here, which I like better. 0:41 So, the basic idea is that we wanna create custom activity lifecycle listeners, 0:44 inside of custom application class. 0:48 When an activity switched from portrait to landscape, it goes through a number of 0:50 lifecycle methods, it's a actually destroyed, and then recreated. 0:55 And when it's recreated in this other mode, 0:59 it gets a different layout if needed. 1:01 So we wanna do, is attach a listener to those events, and 1:04 then when a certain event is called when, the activity is created, 1:07 we want to force it, into SCREEN_ORIENTATION_PORTRAIT. 1:10 All right, let's see how this works in code. 1:15 So we'll start by creating a custom application class, 1:16 right click on our main package here, select New, Java Class, 1:19 let's call this InteractiveStoryApplication. 1:24 By convention, if we do a custom application class, 1:28 we usually end with the word Application. 1:31 As a Superclass, let's start typing Application, and 1:33 we can select android.app.Application, and click OK. 1:36 Okay, so here we have our new class, and it's extending our base application. 1:39 The base application class is used when an app first start ups, and 1:43 there are methods there that happen by default. 1:47 Now that we're using a custom one, we need to specify that in the manifest itself, so 1:49 here for the application element, we want to add a new attribute. 1:53 So drop it on a new line, and type android:name and 1:57 then in here we want the value, we can start typing InteractiveStory, and 2:00 from auto complete, we can select our new class. 2:05 So now, Android knows when our app starts to 2:08 use this custom application class instead of the default one. 2:10 Back in our class, we want to override the onCreate method, so start typing onCreate, 2:14 and we can select it from auto complete, and this is the onCreate method for 2:20 the entire application, not an individual activity like we've seen before. 2:24 So back in our sample code we see that, inside onCreate we want to register a new 2:28 ActivityLifeCycleCallBack, and then in there we're gonna use a specific method. 2:33 So, let's add a new line after super.onCreate, we wanna make sure we call 2:39 that, so that it does all the functionality in the superclass. 2:42 And now we wanna type register, and there it is, ActivityLifecycleCallbacks. 2:45 And here we can add a new anonymous inner class, we can type new 2:50 ActivityLifecycleCallbacks, hit Enter, and we get a bunch of methods we can override. 2:54 Now we don't have to do anything here, 2:59 we're only going to override this first one onActivityCreated, 3:00 but you can see there are other methods that we could override for other reasons. 3:03 So in here, when an activity is created, we want to force it to portrait mode, 3:07 we do that by referencing the activity that's passed in, and 3:12 on it we call setRequestedOrientation, and we set it to portrait mode, 3:16 which we get through ActivityInfo, and 3:20 I just remember this from the stack overflow question, ActivityInfo. 3:23 And here we have a bunch of screen orientation parameters, and 3:27 we want SCREEN_ORIENTATION_PORTRAIT, there we go. 3:31 So, if we run this we should be able to see, 3:34 if we change our emulator to landscape mode, it should stay portrait. 3:36 Okay it loads up, and we can use the buttons here to switch, and sure enough, 3:40 it doesn't change to landscape view. 3:44 Cool, that's exactly what we wanted. 3:45
You need to sign up for Treehouse in order to download course files.Sign up