1 00:00:00,410 --> 00:00:02,060 [treehouseā„¢ presents] 2 00:00:02,060 --> 00:00:05,520 [Quick Tips: Starting a New Activity in an Android App with Ben Jakuben] 3 00:00:06,050 --> 00:00:08,280 Hi, I'm Ben, and in this Treehouse Quick Tip, 4 00:00:08,280 --> 00:00:10,830 we'll learn how to switch from one screen to another 5 00:00:10,830 --> 00:00:12,320 in an Android app. 6 00:00:12,320 --> 00:00:14,890 A screen, or page, of an Android App is called 7 00:00:14,890 --> 00:00:16,180 an 'activity.' 8 00:00:16,180 --> 00:00:19,110 When you create a new Android project, you start with 1 basic activity, 9 00:00:19,110 --> 00:00:21,610 often known as your 'main' activity. 10 00:00:21,610 --> 00:00:23,110 You can switch to a different activity 11 00:00:23,110 --> 00:00:25,280 in a variety of ways, but let's take a look at how to make 12 00:00:25,280 --> 00:00:27,660 the switch using a button on the main activity. 13 00:00:28,310 --> 00:00:30,770 Let's start by adding a button to an activity. 14 00:00:30,770 --> 00:00:34,040 I created a new project with the Blank Activity template, 15 00:00:34,040 --> 00:00:36,830 but you can use any old activity you have lying around. 16 00:00:36,830 --> 00:00:38,300 Add a button and change the label to 17 00:00:38,300 --> 00:00:41,840 Go To Activity #2. 18 00:00:41,840 --> 00:00:46,580 [typing] 19 00:00:46,580 --> 00:00:49,010 Click Okay, and save your changes. 20 00:00:49,010 --> 00:00:51,270 Now, let's create Activity #2. 21 00:00:51,270 --> 00:00:53,960 Expand Source, right-click on the package, 22 00:00:53,960 --> 00:00:55,550 select New, 23 00:00:55,550 --> 00:00:58,320 Other, expand Android, 24 00:00:58,320 --> 00:01:01,510 select Android Activity, and click Next. 25 00:01:01,510 --> 00:01:03,900 Click Next for the Blank Activity template, 26 00:01:03,900 --> 00:01:06,870 and let's name our second activity, 'Second Activity.' 27 00:01:07,760 --> 00:01:10,990 Click Finish. 28 00:01:10,990 --> 00:01:12,840 This creates 2 files for us, 29 00:01:12,840 --> 00:01:14,940 the SecondActivity java file 30 00:01:14,940 --> 00:01:18,040 and the activity_second XML file, 31 00:01:18,040 --> 00:01:20,080 which is open here on the right. 32 00:01:20,080 --> 00:01:22,480 Right-click on the hello_world text, and let's change it to 33 00:01:22,480 --> 00:01:26,090 This is Activity #2. 34 00:01:26,090 --> 00:01:28,680 [typing] 35 00:01:28,680 --> 00:01:31,090 Click Okay, 36 00:01:31,090 --> 00:01:33,110 and to really make it stand out, 37 00:01:33,110 --> 00:01:35,010 let's change the background color, too. 38 00:01:35,010 --> 00:01:37,410 Select the RelativeLayout parent in the outline, 39 00:01:37,410 --> 00:01:43,010 then set the Background property to #00FF00. 40 00:01:43,010 --> 00:01:45,330 Hit Enter, and this gives us a nice 41 00:01:45,330 --> 00:01:48,490 bright-green background that we will definitely notice. 42 00:01:48,490 --> 00:01:51,060 Okay, let's save our changes here. 43 00:01:51,060 --> 00:01:54,300 Now, let's open up MainActivity.java. 44 00:01:54,300 --> 00:01:56,280 Open up our package, 45 00:01:56,280 --> 00:01:58,300 double-click on the file, 46 00:01:58,300 --> 00:02:01,160 and we'll start by declaring a button. 47 00:02:01,160 --> 00:02:04,980 Button switchButton = 48 00:02:04,980 --> 00:02:06,810 I'm going cast us a button 49 00:02:06,810 --> 00:02:09,780 from the findViewById method, 50 00:02:09,780 --> 00:02:12,590 and then our ID is button1. 51 00:02:14,110 --> 00:02:16,430 Okay. Organize my imports. 52 00:02:16,430 --> 00:02:18,980 Now, let's add an onClick listener, which listens for a 53 00:02:18,980 --> 00:02:21,450 click or tap and then runs some code for us. 54 00:02:21,450 --> 00:02:24,600 Type: switchButton 55 00:02:24,600 --> 00:02:26,330 setOnClickListener, 56 00:02:26,330 --> 00:02:28,600 hit Enter, 57 00:02:28,600 --> 00:02:31,490 and we want a new View.OnClickListener, 58 00:02:31,490 --> 00:02:33,890 hit Enter, and it drops in the code. 59 00:02:33,890 --> 00:02:35,140 I'm going to scroll down 60 00:02:35,140 --> 00:02:37,690 and add a semicolon before I forget. 61 00:02:37,690 --> 00:02:39,000 Now, let's add the code that we need 62 00:02:39,000 --> 00:02:42,460 to switch activities inside the onClick method. 63 00:02:42,460 --> 00:02:44,100 In Android, we switch activities 64 00:02:44,100 --> 00:02:46,420 by using intents. 65 00:02:46,420 --> 00:02:48,830 Intents are abstract descriptions 66 00:02:48,830 --> 00:02:51,040 of operations to be performed. 67 00:02:51,040 --> 00:02:54,360 We will use an explicit intent 68 00:02:54,360 --> 00:02:56,700 which provides the exact class 69 00:02:56,700 --> 00:02:58,220 to be run. 70 00:02:58,220 --> 00:03:01,270 In this case, the class is our second activity. 71 00:03:01,270 --> 00:03:03,190 So, back in Eclipse, 72 00:03:03,190 --> 00:03:05,000 I'm going to delete this comment, 73 00:03:05,000 --> 00:03:08,390 and add: Intent intent = 74 00:03:08,390 --> 00:03:11,480 new Intent 75 00:03:11,480 --> 00:03:13,260 and for the parameters, type: 76 00:03:13,260 --> 00:03:17,300 MainActivity.this, 77 00:03:17,300 --> 00:03:21,590 SecondActivity.class. 78 00:03:21,590 --> 00:03:23,870 The first parameter is the current context, 79 00:03:23,870 --> 00:03:26,520 but since we're inside the onClick listener, 80 00:03:26,520 --> 00:03:28,510 which is known as an anonymous inner class, 81 00:03:28,510 --> 00:03:30,490 we need to reference it by the full class name. 82 00:03:30,490 --> 00:03:35,070 The second parameter is the name of our target class. 83 00:03:35,070 --> 00:03:38,970 Okay, let's organize our imports, 84 00:03:38,970 --> 00:03:40,030 and, now, on a new line, type: 85 00:03:40,030 --> 00:03:43,570 startActivity, 86 00:03:43,570 --> 00:03:46,960 and the parameter is the intent we just created. 87 00:03:46,960 --> 00:03:48,010 That's it. 88 00:03:48,010 --> 00:03:51,700 Save and Run. 89 00:03:51,700 --> 00:03:53,840 Run it as an Android application, Click Okay. 90 00:03:53,840 --> 00:03:56,750 Okay, let's tap on the button, and, 91 00:03:56,750 --> 00:03:59,740 hurray, there's Activity #2, 92 00:03:59,740 --> 00:04:02,700 and we can use the Back button to go back to the first activity. 93 00:04:04,060 --> 00:04:06,330 As you can see, switching activities is pretty easy 94 00:04:06,330 --> 00:04:08,480 once you have the activities created. 95 00:04:08,480 --> 00:04:10,340 Intents like these are incredibly versatile 96 00:04:10,340 --> 00:04:12,350 and powerful and can make use of so many 97 00:04:12,350 --> 00:04:14,900 classes, apps, and services on your device. 98 00:04:14,900 --> 00:04:16,680 Switching apps is just as easy as switching activities. 99 00:04:18,810 --> 00:04:22,880 If you'd like to see more advanced videos and tutorials like this one, 100 00:04:22,880 --> 00:04:26,510 go to TeamTreehouse.com and start learning for free. 101 00:04:26,510 --> 00:04:28,510 [treehouseā„¢]