Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Updating the Main Layout5:28 with Ken Alger
We need to add a way to get to our new Activity.
We'll be using a new activity to view our list. 0:00 First though, we need a way to navigate to it. 0:03 To keep things simple, we're going to add a button to the bottom 0:06 of our main activity layout for the hourly forecast. 0:09 Let's go look at our layout. 0:13 Stormy is designed with a constraint layout. 0:18 Let's add a button at the bottom of the design and center it in the layout. 0:22 We'll grab a button down here, let's zoom in a little bit. 0:27 We'll constrain it to the right and the left. 0:34 The top we'll constrain to our summary, and 0:39 the bottom to our dark sky attribution. 0:42 Let's change the ID to be hourly button. 0:48 Go down to the background, 0:56 we want the background color to be slightly transparent. 0:58 So 40 and six fs, which will give us a slightly transparent white. 1:04 We want the text to say HOURLY FORECAST. 1:12 And we want the text to be an Android white. 1:21 Color > Android White and we want our layout height to be 30 dp. 1:29 Layout_height, we want 30 dp. 1:39 Let's also add some left and right padding of about 5 dp so 1:45 that our text isn't crowded so much inside the button. 1:50 So padding left, we'll do 5 dp, and right we'll do 5 dp. 1:54 Let's zoom out a little bit, that looks pretty good. 2:05 Now we need a new activity, let's create a new one in the UI package. 2:10 New > Activity > Basic activity. 2:17 We'll name it HourlyForecastActivity. 2:24 And we'll set the hierarchical parent to be main activity, just as a best practice. 2:30 Even though for this app, we won't be utilizing navigation. 2:37 Project> 2:40 Main Activity. 2:51 Everything else we can keep here just as default. 2:54 Now we can setup the button configuration to handle on click events. 3:03 We head over to the main_activities XML, 3:07 go to Text, scroll down here to the hourlyButton. 3:13 We'll want to add android.onClick = hourlyOnClick. 3:20 Next we need to define that method. 3:33 Remember from our previous courses, to utilize this method for 3:36 setting onClick handling, we define a method in our activity by this same name. 3:40 It has to be public, with a void return type. 3:45 It also needs to take a view as its only parameter, so 3:48 let's go back to MainActivity, and down at the bottom. 3:52 Public void, we called it hourlyOnClick And it'll take our View. 4:01 Inside this new method, we want it to start the HourlyForecastActivity. 4:12 We can do that by creating a new intent, and 4:17 passing it into the start activity method. 4:20 So we'll do intent, intent. 4:24 New Intent, add our imports with the quick fix. 4:34 Let's scroll up here a little bit. 4:42 So inside this Intent context, we'll pass in this and 4:45 we want to start the HourlyForecastActivity class. 4:50 And then we startActivity and pass in our intent. 4:58 That should do it for now, nice work. 5:05 I encourage you to play around and customize this layout. 5:08 The best way to learn is by trying something in a new or different way. 5:12 If you come up with a new user interface you're really proud of, 5:16 share it with the Treehouse community. 5:20 Sharing your projects and 5:22 encourages others to share their projects as well, don't forget to let us know, too. 5:23
You need to sign up for Treehouse in order to download course files.Sign up