Start a New Activity in an Android App4:18 with Ben Jakuben
This video will show you how to switch between screens in an Android app. A new android project starts with one basic activity. There are a variety of ways to switch activities in Android, and in this video we'll see how to use a button on the main screen. First you'll want to create a new activity. Then use a button on the first activity and an onClick listener to switch activities. The intent will execute the switch between activities. Intents are very powerful and can make use of so many apps, classes, and services on your device.
[treehouse™ presents] 0:00 [Quick Tips: Starting a New Activity in an Android App with Ben Jakuben] 0:02 Hi, I'm Ben, and in this Treehouse Quick Tip, 0:06 we'll learn how to switch from one screen to another 0:08 in an Android app. 0:10 A screen, or page, of an Android App is called 0:12 an 'activity.' 0:14 When you create a new Android project, you start with 1 basic activity, 0:16 often known as your 'main' activity. 0:19 You can switch to a different activity 0:21 in a variety of ways, but let's take a look at how to make 0:23 the switch using a button on the main activity. 0:25 Let's start by adding a button to an activity. 0:28 I created a new project with the Blank Activity template, 0:30 but you can use any old activity you have lying around. 0:34 Add a button and change the label to 0:36 Go To Activity #2. 0:38 [typing] 0:41 Click Okay, and save your changes. 0:46 Now, let's create Activity #2. 0:49 Expand Source, right-click on the package, 0:51 select New, 0:53 Other, expand Android, 0:55 select Android Activity, and click Next. 0:58 Click Next for the Blank Activity template, 1:01 and let's name our second activity, 'Second Activity.' 1:03 Click Finish. 1:07 This creates 2 files for us, 1:10 the SecondActivity java file 1:12 and the activity_second XML file, 1:14 which is open here on the right. 1:18 Right-click on the hello_world text, and let's change it to 1:20 This is Activity #2. 1:22 [typing] 1:26 Click Okay, 1:28 and to really make it stand out, 1:31 let's change the background color, too. 1:33 Select the RelativeLayout parent in the outline, 1:35 then set the Background property to #00FF00. 1:37 Hit Enter, and this gives us a nice 1:43 bright-green background that we will definitely notice. 1:45 Okay, let's save our changes here. 1:48 Now, let's open up MainActivity.java. 1:51 Open up our package, 1:54 double-click on the file, 1:56 and we'll start by declaring a button. 1:58 Button switchButton = 2:01 I'm going cast us a button 2:04 from the findViewById method, 2:06 and then our ID is button1. 2:09 Okay. Organize my imports. 2:14 Now, let's add an onClick listener, which listens for a 2:16 click or tap and then runs some code for us. 2:18 Type: switchButton 2:21 setOnClickListener, 2:24 hit Enter, 2:26 and we want a new View.OnClickListener, 2:28 hit Enter, and it drops in the code. 2:31 I'm going to scroll down 2:33 and add a semicolon before I forget. 2:35 Now, let's add the code that we need 2:37 to switch activities inside the onClick method. 2:39 In Android, we switch activities 2:42 by using intents. 2:44 Intents are abstract descriptions 2:46 of operations to be performed. 2:48 We will use an explicit intent 2:51 which provides the exact class 2:54 to be run. 2:56 In this case, the class is our second activity. 2:58 So, back in Eclipse, 3:01 I'm going to delete this comment, 3:03 and add: Intent intent = 3:05 new Intent 3:08 and for the parameters, type: 3:11 MainActivity.this, 3:13 SecondActivity.class. 3:17 The first parameter is the current context, 3:21 but since we're inside the onClick listener, 3:23 which is known as an anonymous inner class, 3:26 we need to reference it by the full class name. 3:28 The second parameter is the name of our target class. 3:30 Okay, let's organize our imports, 3:35 and, now, on a new line, type: 3:38 startActivity, 3:40 and the parameter is the intent we just created. 3:43 That's it. 3:46 Save and Run. 3:48 Run it as an Android application, Click Okay. 3:51 Okay, let's tap on the button, and, 3:53 hurray, there's Activity #2, 3:56 and we can use the Back button to go back to the first activity. 3:59 As you can see, switching activities is pretty easy 4:04 once you have the activities created. 4:06 Intents like these are incredibly versatile 4:08 and powerful and can make use of so many 4:10 classes, apps, and services on your device. 4:12 Switching apps is just as easy as switching activities. 4:14 If you'd like to see more advanced videos and tutorials like this one, 4:18 go to TeamTreehouse.com and start learning for free. 4:22 [treehouse™] 4:26
You need to sign up for Treehouse in order to download course files.Sign up