Lab 1 Introduction To Android Studio
Lab 1 Introduction To Android Studio
Installation
1.
Figure 1
2. Install the downloaded exe file. Follow the setup wizard to install Android Studio and any
necessary SDK tools. Android Studio provides everything you need to start developing apps
for Android, including the Android Studio IDE and the Android SDK tools.
Page 1 of 19
Figure 2
Android SDK
By default, the Android SDK does not include everything you need to start developing. The SDK
separates tools, platforms, and other components into packages you can download as needed using
the Android SDK Manager.
1. On the Quick Start screen, click Configure and Android SDK Manager.
Figure 3
Page 2 of 19
As a minimum when setting up the Android SDK, you should download the latest tools
and Android platform.
2. Open the Tools directory and select:
o
3. Open the first Android X.X folder (the latest version) and select:
o
SDK Platform
Page 3 of 19
Figure 4
The elements that comprise the editor window can be summarized as follows:
A Document Tabs Android Studio is capable of holding multiple files open for editing at any one
time. As each file is opened, it is assigned a document tab displaying the file name in the tab bar
located along the top edge of the editor window. A small dropdown menu will appear in the far right
hand corner of the tab bar when there is insufficient room to display all of the tabs. Clicking on this
menu will drop down a list of additional open files. A wavy red line underneath a file name in a tab
indicates that the code in the file contains one or more errors that need to be addressed before the
project can be compiled and run.
Switching between files is simply a matter of clicking on the corresponding tab or using the Alt-Left
and Alt-Right keyboard shortcuts. Navigation between files may also be performed using the Switcher
mechanism (accessible via the Ctrl-Tab keyboard shortcut).
To detach an editor panel from the Android Studio main window so that it appears in a separate
window, click on the tab and drag it to an area on the desktop outside of the main window. To return
the editor to the main window, click on the file tab in the separated editor window and drag and drop it
onto the original editor tab bar in the main window.
B The Editor Gutter Area - The gutter area is used by the editor to display informational icons and
controls. Some typical items, among others, which appear in this gutter area are debugging
Page 4 of 19
breakpoint markers, controls to fold and unfold blocks of code, bookmarks, change markers and line
numbers. Line numbers are switched off by default but may be enabled by right-clicking in the gutter
and selecting the Show Line Numbers menu option.
C The Status Bar Though the status bar is actually part of the main window, as opposed to the
editor, it does contain some information about the currently active editing session. This information
includes the current position of the cursor in terms of lines and characters and the encoding format of
the file (UTF-8, ASCII etc.). Clicking on these values in the status bar allows the corresponding setting
to be changed. Clicking on the line number, for example, displays the Go to Line dialog.
D The Editor Area This is the main area where the code is displayed, entered and edited by the
user. Later sections of this chapter will cover the key features of the editing area in detail.
E The Validation and Marker Sidebar Android Studio incorporates a feature referred to as onthe-fly code analysis. What this essentially means is that as you are typing code, the editor is
analyzing the code to check for warnings and syntax errors. The colored square at the top of the
validation sidebar will change color from green (no warnings or errors detected) to yellow (warnings
detected) and red (errors have been detected). Clicking on this square will display a popup containing
a summary of the issues found with the code in the editor as illustrated in Figure 2:
Figure 5
The sidebar also displays markers at the locations where issues have been detected using the same
color coding. Hovering the mouse pointer over a marker when the line of code is visible in the editor
area will display a popup containing a description of the issue :
Figure 6
Hovering the mouse pointer over a marker for a line of code which is currently scrolled out of the
viewing area of the editor will display a lens overlay containing the block of code where the problem
is located allowing it to be viewed without the necessity to scroll to that location in the editor:
Page 5 of 19
Figure 7
It is also worth noting that the lens overlay is not limited to warnings and errors in the sidebar.
Hovering over any part of the sidebar will result in a lens appearing containing the code present at
that location within the source file.
Having provided an overview of the elements that comprise the Android Studio editor, the remainder
of this chapter will explore the key features of the editing environment in more detail.
Page 6 of 19
Figure 8
The orientation of a split panel may be changed at any time by right clicking on the corresponding tab
and selecting the Change Splitter Orientation menu option. Repeat these steps to unsplit a single
panel, this time selecting the Unsplit option from the menu. All of the split panels may be removed by
right-clicking on any tab and selecting the Unsplit All menu option.
Window splitting may be used to display different files, or to provide multiple windows onto the same
file, allowing different areas of the same file to be viewed and edited concurrently.
Code Completion
The Android Studio editor has a considerable amount of built-in knowledge of Java programming
syntax and the classes and methods that make up the Android SDK, as well as knowledge of your
own code base. As code is typed, the editor scans what is being typed and, where appropriate,
makes suggestions with regard to what might be needed to complete a statement or reference. When
a completion suggestion is detected by the editor, a panel will appear containing a list of suggestions.
In Figure 6, for example, the editor is suggesting possibilities for the beginning of a String declaration:
Page 7 of 19
Figure 9
If none of the auto completion suggestions are correct, simply keep typing and the editor will continue
to refine the suggestions where appropriate. To accept the top most suggestion, simply press the
Enter or Tab key on the keyboard. To select a different suggestion, use the arrow keys to move up
and down the list, once again using the Enter or Tab key to select the highlighted item.
Completion suggestions can be manually invoked using the Ctrl-Space keyboard sequence. This can
be useful when changing a word or declaration in the editor. When the cursor is positioned over a
word in the editor, that word will automatically highlight. Pressing Ctrl-Space will display a list of
alternate suggestions. To replace the current word with the currently highlighted item in the
suggestion list, simply press the Tab key.
In addition to the real-time auto completion feature, the Android Studio editor also offers a system
referred to as Smart Completion. Smart completion is invoked using the Shift-Ctrl-Space keyboard
sequence and, when selected, will provide more detailed suggestions based on the current context of
the code. Pressing the Shift-Ctrl-Space shortcut sequence a second time will provide more
suggestions from a wider range of possibilities.
Code completion can be a matter of personal preference for many programmers. In recognition of this
fact, Android Studio provides a high level of control over the auto completion settings. These can be
viewed and modified by selecting the File -> Settings menu option and choosing Editor -> Code
Completion from the IDE Settings section of the settings panel as shown in Figure7:
Page 8 of 19
Figure 10
Statement Completion
Another form of auto completion provided by the Android Studio editor is statement completion. This
can be used to automatically fill out the parentheses and braces for items such as methods and loop
statements. Statement completion is invoked using the Shift-Ctrl-Enter (Shift-Cmd-Enter on Mac OS X)
keyboard sequence. Consider for example the following code:
protected void myMethod()
Having typed this code into the editor, triggering statement completion will cause the editor to
automatically add the braces to the method:
protected void myMethod() {
}
Page 9 of 19
Parameter Information
It is also possible to ask the editor to provide information about the argument parameters accepted by
a method. With the cursor positioned between the brackets of a method call, the Ctrl-P (Cmd-P on
Mac OS X) keyboard sequence will display the parameters known to be accepted by that method,
with the most likely suggestion highlighted in bold:
Figure 11
Code Generation
In addition to completing code as it is typed the editor can, under certain conditions, also generate
code for you. The list of available code generation options shown in Figure 13 can be accessed using
the Alt-Insert keyboard shortcut when the cursor is at the location in the file where the code is to be
generated.
Figure 12
For the purposes of an example, consider a situation where we want to be notified when an Activity in
our project is about to be destroyed by the operating system. As will be outlined in a later chapter of
this book, this can be achieved by overriding the onStop() lifecycle method of the Activity superclass.
To have Android Studio generate a stub method for this, simply select the Override Methods option
from the code generation list and select the onStop() method from the resulting list of available
methods:
Page 10 of 19
Figure 13
Having selected the method to override, clicking on OK will generate the stub method at the current
cursor location in the Java source file as follows:
@Override
protected void onStop() {
super.onStop();
}
Code Folding
Once a source code file reaches a certain size, even the most carefully formatted and well organized
code can become overwhelming and difficult to navigate. Android Studio takes the view that it is not
always necessary to have the content of every code block visible at all times. Code navigation can be
Page 11 of 19
made easier through the use of the code folding feature of the Android Studio editor. Code folding is
controlled using markers appearing in the editor gutter at the beginning and end of each block of code
in a source file. Figure 14, for example, highlights the start and end markers for a method declaration
which is not currently folded:
Figure 14
Clicking on either of these markers will fold the statement such that only the signature line is visible as
shown in Figure 15:
Figure 15
To unfold a collapsed section of code, simply click on the + marker in the editor gutter. To see the
hidden code without unfolding it, hover the mouse pointer over the {} indicator as shown in Figure
16. The editor will then display the lens overlay containing the folded code block:
Figure 16
All of the code blocks in a file may be folded or unfolded using the Ctrl-Shift-Plus and Ctrl-Shift-Minus
keyboard sequences.
Page 12 of 19
By default, the Android Studio editor will automatically fold some code when a source file is opened.
To configure the conditions under which this happens, select File -> Settings and choose the Editor
-> Code Folding entry listed under IDE Settings in the resulting settings panel (Figure 17):
Figure 17
Page 13 of 19
Figure 18
Once displayed, the documentation popup can be moved around the screen as needed. Clicking on
the push pin icon located in the right hand corner of the popup title bar will ensure that the popup
remains visible once focus moves back to the editor, leaving the documentation visible as a reference
while typing code.
Code Reformatting
In general, the Android Studio editor will automatically format code in terms of indenting, spacing and
nesting of statements and code blocks as they are added. In situations where lines of code need to be
reformatted (a common occurrence, for example, when cutting and pasting sample code from a web
site), the editor provides a source code reformatting feature which, when selected, will automatically
reformat code to match the prevailing code style.
To reformat source code, press the Ctrl-Alt-L keyboard shortcut sequence to display the Reformat
Code dialog (Figure 19). This dialog provides the option to reformat only the currently selected code,
the entire source file currently active in the editor or all files in a given project folder.
Figure 19
The full range of code style preferences can be changed from within the project settings dialog. Select
the File -> Settings menu option and choose Code Style listed under Project Settings in the left hand
Page 14 of 19
panel to access a list of supported programming and markup languages. Selecting a language will
provide access to a vast array of formatting style options, all which may be modified from the Android
Studio default to match your preferred code style.
Figure 20
From the main screen, you can create, edit, and start AVDs as well as see the details of each AVD.
Page 15 of 19
Creating an AVD
1. In the AVD Manager screen, click Create Virtual Machine button.
Figure 21
Figure 22
Page 16 of 19
Figure 23
Your AVD is now ready and you can close the AVD Manager, create more AVDs, or launch an
emulator with the AVD by selecting a device and clicking Start.
Hardware options
If you are creating a new AVD, you can specify the following hardware options for the AVD to emulate:
Characteristic
Description
Touch-screen support
Trackball support
Keyboard support
DPad support
Camera support
Maximum horizontal
camera pixels
Maximum vertical
camera pixels
GPS support
Battery support
Page 17 of 19
Accelerometer
SD Card support
Keyboard Commands
The following tables list keyboard shortcuts for common operations.
Table 1. Programming key commands
Action
Command look-up
(autocomplete command
name)
CTRL + SHIFT + A
ALT + ENTER
Reformat code
CTRL + Q (Win)
F1 (Mac)
CTRL + P
Generate method
Jump to source
F4 (Win)
CMD + down-arrow (Mac)
Delete line
CTRL + Y (Win)
CMD + Backspace (Mac)
Page 18 of 19
Build
CTRL + F9 (Win)
CMD + F9 (Mac)
ALT + 1 (Win)
CMD + 1 (Mac)
Page 19 of 19