Using Android Studio for Android Development Wenbing Zhao
Using Android Studio for Android Development Wenbing Zhao Department of Electrical Engineering and Computer Science Cleveland State University w. zhao 1@csuohio. edu 5/20/2021 CIS 470: Mobile App Development 1
What is Android? n n n Based on Linux Originally developed by a startup company named “Android” 2005 Google purchased the startup Most of the Android code is open-source Android offers a unified approach to application development 5/20/2021 CIS 470: Mobile App Development 2
Android Versions n n n 1. 1: Feb 9, 2009; 1. 5: April 30, 2009, Cupcake 1. 6: Sep 15, 2009, Donut; 2. 0/2. 1: Oct 26, 2009, Eclair 2. 2: May 20, 2010, Froyo; 2. 3: Dec 6, 2010, Gingerbread 3. 0/3. 1/3. 2: Feb 22, 2011, Honeycomb 4. 0: Oct 8, 2011, Ice Cream Sandwich 4. 1: July 9, 2012, Jelly Bean 4. 4: Oct 31, 2013, Kit. Kat 5. 0: Nov 12, 2014, Lollipop 6. 0: Oct 5, 2015, Marshmallow 7. 0: Aug 22, 2016, Nougat 8. 0: Aug 21, 2017, Oreo 9. 0: Aug 6, 2018, Pie 5/20/2021 CIS 470: Mobile App Development 3
Android Architecture 5/20/2021 CIS 470: Mobile App Development 4
Android Development Tools n Android Studio: http: //developer. android. com/sdk/index. html q n Android SDK Java SE Development Toolkit 8: www. oracle. com/technetwork/javase/downloads/jdk 8 downloads-2133151. html 5/20/2021 CIS 470: Mobile App Development 5
5/20/2021 CIS 470: Mobile App Development 6
Creating Android Virtual Devices n Android Studio offers an emulator for testing of Android apps 5/20/2021 CIS 470: Mobile App Development 7
Set target Android devices. Accept default, click next 5/20/2021 CIS 470: Mobile App Development 8
Creating your first Android App n n n File -> New->New Project Name the app: Hello. World Then select default option for all remaining steps 5/20/2021 CIS 470: Mobile App Development 9
Creating your first Android App 5/20/2021 CIS 470: Mobile App Development 10
Creating your first Android App 5/20/2021 CIS 470: Mobile App Development 11
Creating your first Android App 5/20/2021 CIS 470: Mobile App Development 12
Creating your first Android App 5/20/2021 CIS 470: Mobile App Development 13
Launching your first Android App Select Run ➪ Run app from the Android Studio menu bar. You should see the Select Deployment Target dialog shown below: 5/20/2021 CIS 470: Mobile App Development 14
5/20/2021 CIS 470: Mobile App Development 15
Using Android Studio for Android Development n n n How to move around in the Integrated Development Environment (IDE) How to use code completion to make writing applications easier How to use breakpoints to debug your applications 5/20/2021 CIS 470 Mobile App Development 16
Exploring the IDE n n n Open the IDE Start a new project Select options Project view Android monitor 5/20/2021 CIS 470 Mobile App Development 17
Give the project name: IDEExplorer; use whatever domain name you like 5/20/2021 CIS 470 Mobile App Development 18
5/20/2021 CIS 470 Mobile App Development 19
The default option is Empty Activity. This is the most useful for our examples because it creates a basic activity for you, with no code in it 5/20/2021 CIS 470 Mobile App Development 20
It is accepted practice in Android development to name your main activity—that is, the Activity that is loaded on startup by your application—as Main. Activity The startup layout, that is the layout for the screen elements that will be displayed when your application is started by the user, is the activity_main layout. All other layouts should be named according to the activity that they support (activity_input, activity_delete) Click the Finish button to finish creating the project and jump into exploring the IDE 5/20/2021 CIS 470 Mobile App Development 21
The Android Studio IDE 5/20/2021 CIS 470 Mobile App Development 22
The left side of the IDE shows the Project window. The Project window enables you to quickly navigate the files within your project. 5/20/2021 CIS 470 Mobile App Development 23
On the right side of the IDE are the Editor tabs. The Editor tabs are where you write and work with your code files. 5/20/2021 CIS 470 Mobile App Development 24
• To work on a new file, simply locate the file in the Project window and double-click it to open a new Editor tab that contains that file’s code. • If you need to create a new file from scratch, right-click the directory into which you want to place your file, and select New ➪ <File Type> from the context menu. • At the bottom of the IDE, you should see a button labeled Log. Cat. Logcat displays most of the helpful messages that are output by your application while you are trying to debug it. 5/20/2021 CIS 470 Mobile App Development 25
Using Code Completion n n Code completion: a tool that shows contextual options for completing the piece of code that you are trying to write Example: q In the editor tab for the Main. Activity. java file, locate the line that reads q set. Content. View(R. layout. activity_main); q Place your cursor after this line and press the Enter key. On the new line, type the letter R, and then type a period, as shown here: q R. q Android Studio Code Completion should display a list of values that you could use to try to complete the code statement 5/20/2021 CIS 470 Mobile App Development 26
Code completion example If the code completion window does not open, press Ctrl+Space to force it to open. 5/20/2021 CIS 470 Mobile App Development 27
Debugging Your Application n n Common way to debug: set breakpoints to help you find what is going on with your code Breakpoints are a mechanism by which you can tell Android Studio to temporarily pause execution of your code, which allows you to examine the condition of your application q q 5/20/2021 You can check on the values of variables in your application while you are debugging it You can check whether certain lines of code are being executed as expected—or at all CIS 470 Mobile App Development 28
Click the margin of the editor tab next to line of code you want to break at, to set a breakpoint. A red circle is placed in the margin, and the corresponding line is highlighted in red (clicked it again to remove the breakpoint) Method Breakpoint A method breakpoint is represented by a red circle containing four dots placed at the method signature Android Studio pauses execution when the method is hit, and it also automatically sets a corresponding breakpoint and pauses at the end of the method 5/20/2021 CIS 470 Mobile App Development 29
Temporary Breakpoints Useful in a loop To set a temporary breakpoint, place your cursor at the location in the code where you want it to break and select Run ➪ Toggle Temporary Line Breakpoint. Android Studio only stops at this breakpoint the first time your code enters it 5/20/2021 CIS 470 Mobile App Development 30
Conditional Breakpoints A condition breakpoint is a breakpoint at which Android Studio only pauses when specific conditions are met. To set a conditional breakpoint, first set a simple breakpoint at the line of code you want to examine, then right-click the simple breakpoint to bring up the condition context menu You would then set the condition in the breakpoint such as: foo == true 5/20/2021 CIS 470 Mobile App Development 31
Navigating Paused Code When Android Studio hits, and pauses at, a breakpoint, the red circle in the margin next to the corresponding line of code changes to a circle with a check mark Once a breakpoint has been hit, the debug window opens at the bottom of Android Studio Step Over and Step Into 5/20/2021 CIS 470 Mobile App Development 32
- Slides: 32