24 CHAPTER 2:Development Environment 8 Create New Project × New Project Select the form factors your app will run on Different platfors require sepeSDKs ☑Phoneand Tablet Minimum SDK API21:Android 5.(Lollipop) Figure 2-3.Target Android API level 8.As shown in Figure 2-4,select Blank Activity on the next screen,and click the Next button to proceed. Create New Project x Add an activity to Mobile ■w目 Add No Activity Blank Activity Figure 2-4.Android project activity type
24 CHAPTER 2: Development Environment 8. As shown in Figure 2-4, select Blank Activity on the next screen, and click the Next button to proceed. Figure 2-3. Target Android API level Figure 2-4. Android project activity type
CHAPTER 2:Development Environment 25 9.As shown in Figure 2-5,continue with the default values for the new activity. Create New Project Choose options for your new file Createsa new blank activity with an action bar. (■w Activity Name: MainActivity Layout Name: activity_main MainActivity Menu Resource Name menu_main Figure 2-5.Activity name 10.Click the Finish button to complete the New Android Project Wizard. 11.Android Studio will open the new project in edit mode as shown in Figure 2-6
CHAPTER 2: Development Environment 25 9. As shown in Figure 2-5, continue with the default values for the new activity. Figure 2-5. Activity name 10. Click the Finish button to complete the New Android Project Wizard. 11. Android Studio will open the new project in edit mode as shown in Figure 2-6
CHAPTER 2:Development Environment .n 7 Figure 2-6.The new Android project editor Building the Android Application The new Android application project can be built by choosing Build >Make Project from the top menu bar of the Android Studio. Running the Android Application Once the Android application project is built,it can run on both the actual Android devices and the Android emulator that comes as part of the Android toolchain. The Android application can run on any Android device,as long as the attached Android device runs a version of the Android platform that is greater than or equal to the minimum target API level that is specified for the application
26 CHAPTER 2: Development Environment Building the Android Application The new Android application project can be built by choosing Build ➤ Make Project from the top menu bar of the Android Studio. Running the Android Application Once the Android application project is built, it can run on both the actual Android devices and the Android emulator that comes as part of the Android toolchain. The Android application can run on any Android device, as long as the attached Android device runs a version of the Android platform that is greater than or equal to the minimum target API level that is specified for the application. Figure 2-6. The new Android project editor
CHAPTER 2:Development Environment 27 Enabling USB Debug Mode on the Android Device For Android Studio to see and communicate with the attached Android device,the USB Debug mode should first be enabled on the device itself. Different versions of the Android platform allow the developers to do this in different ways. Using your Android device,running Android 4.2 or greater,take the following steps to enable the USB Debug mode: 1.Before you start,make sure that the Android device is not yet connected to the developer machine through the USB cable. 2.Using the Android device,go to the screen 3.Scroll down to the Asour menu item. Note On Samsung Galaxy devices,first choose MoRE/GENERAL,ther choose ABoUr. 4.On the ABour screen,scroll down to BUILD NUMBER. 5.Tap on BuILD NuMBER exactly seven times 6.If the previous step is successful,you will see a toast message saying "DEVELOPER MODE HAS BEEN ENABLED." 7. Now go back to the Settings screen 8.Scroll down and you should now see a new menu item,DEVELOPER OPTIONS.Choose that menu item. 9. On the DEVELOPER OPTONS screen,check USB DEBUGGING. 10.Now plug the Android device into the developer machine through the USB cable. 11.Once the Android device connects to the developer machine,the device will show dialog asking your permission to allow the USB debug connection. 12. Accept that request to proceed
CHAPTER 2: Development Environment 27 Enabling USB Debug Mode on the Android Device For Android Studio to see and communicate with the attached Android device, the USB Debug mode should first be enabled on the device itself. Different versions of the Android platform allow the developers to do this in different ways. Using your Android device, running Android 4.2 or greater, take the following steps to enable the USB Debug mode: 1. Before you start, make sure that the Android device is not yet connected to the developer machine through the USB cable. 2. Using the Android device, go to the SETTINGS screen. 3. Scroll down to the ABOUT menu item. Note On Samsung Galaxy devices, first choose MORE/GENERAL, then choose ABOUT. 4. On the ABOUT screen, scroll down to BUILD NUMBER. 5. Tap on BUILD NUMBER exactly seven times. 6. If the previous step is successful, you will see a toast message saying “DEVELOPER MODE HAS BEEN ENABLED.” 7. Now go back to the Settings screen. 8. Scroll down and you should now see a new menu item, DEVELOPER OPTIONS. Choose that menu item. 9. On the DEVELOPER OPTIONS screen, check USB DEBUGGING. 10. Now plug the Android device into the developer machine through the USB cable. 11. Once the Android device connects to the developer machine, the device will show dialog asking your permission to allow the USB debug connection. 12. Accept that request to proceed
28 CHAPTER 2:Development Environment Now the Android device is configured to allow Android Studio to execute Android applications. Note As these steps may be different on your Android device,please check www.droidviews.com/how-to-enable-developer- optionsusb-debugging-mode-on-devices-with-android-4-2 jelly-bean/for more recipes on how to enable the USB Debug mode on some other Android devices. Running the Application on the Android Device From the top menu bar,choose RunRun 'app'to launch the Choose Device d dialog. As shown in Figure 2-7,the Choose Device dialog will be launched to display the list of attached Android devices on which you can run the Android application. Choose Device Choose a running device Device Serial Number StateCom. Motorola Nexus 6 Android 5.0.1(API 21) Online Yes OLaunch emulator Android virtual device:Nexus 5 API 21 x86 Use same device for future launches OK Cancel Figure 2-7.Android devices that are attached Select the Android device you want to use e and click the OK button to start the application on the selected Android device. For the Android device to show up on this list,the USB Debug mode should be enabled on that device through the Developer Options settings page. www.allitebooks.com
28 CHAPTER 2: Development Environment Now the Android device is configured to allow Android Studio to execute Android applications. Figure 2-7. Android devices that are attached Note As these steps may be different on your Android device, please check www.droidviews.com/how-to-enable-developeroptionsusb-debugging-mode-on-devices-with-android-4-2- jelly-bean/ for more recipes on how to enable the USB Debug mode on some other Android devices. Running the Application on the Android Device From the top menu bar, choose Run ➤ Run ‘app’ to launch the Choose Device dialog. As shown in Figure 2-7, the Choose Device dialog will be launched to display the list of attached Android devices on which you can run the Android application. Select the Android device you want to use and click the OK button to start the application on the selected Android device. For the Android device to show up on this list, the USB Debug mode should be enabled on that device through the Developer Options settings page. www.allitebooks.com