Changes

Jump to: navigation, search

The Basics of the Android Studio Code Editor

No change in size, 13:40, 18 July 2014
no edit summary
== The Android Studio Editor ==
The Android Studio editor appears in the center of the main window when a Java, XML or other text based file is selected for editing. Figure 57-1, for example, shows a typical editor session with a Java source code file loaded:
[[Image:android_studio_editor_overview_tagged.png|The key areas of the Android Studio Editor window]]
Figure 57-1
'''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 “on-the-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 57-2:
[[Image:android_studio_editor_analysis.png|Android Studio Editor analysis popup]]
Figure 57-2
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 57-3):
[[Image:android_studio_editor_warning_popup.png|Android Studio Editor analysis warning popup]]
Figure 57-3
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 (Figure 57-4) allowing it to be viewed without the necessity to scroll to that location in the editor:
[[Image:android_studio_analysis_overlay.png|Android Studio Editor error and warning lens overlay]]
Figure 57-4
== Splitting the Editor Window ==
By default, the editor will display a single panel showing the content of the currently selected file. A particularly useful feature when working simultaneously with multiple source code files is the ability to split the editor into multiple panes. To split the editor, right-click on a file tab within the editor window and select either the Split Vertically or Split Horizontally menu option. Figure 57-5, for example, shows the splitter in action with the editor split into three panels:
[[Image:android_studio_editor_splitter.png|Splitting the Android Studio editor window]]
Figure 57-5
== 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 57-6, for example, the editor is suggesting possibilities for the beginning of a String declaration:
[[Image:android_studio_autocompletion.png|The Android Studio code editing auto completion suggestion list]]
Figure 57-6
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 Figure 57-7:
[[Image:android_studio_code_completion_settings.png|Configuring code completion settings in the Android Studio Editor]]
Figure 57-7
[[Image:android_studio_parameter_info.png|The Android Studio Editor Parameter information popup]]
Figure 57-8
== 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 57-10 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.
[[Image:android_studio_generate_code.png|The Android Studio Editor code generate menu]]
Figure 57-9
[[Image:android_studio_generate_override.png|Selecting an override method from the Android Studio code generation list]]
Figure 57-10
== 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 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 57-11, for example, highlights the start and end markers for a method declaration which is not currently folded:
[[Image:android_studio_code_folding.png|The Android Studio Editor code folding controls in gutter area]]
Figure 57-11
Clicking on either of these markers will fold the statement such that only the signature line is visible as shown in Figure 57-12:
[[Image:android_studio_code_folded.png|A code block folded in the Android Studio Editor]]
Figure 57-12
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 57-13. The editor will then display the lens overlay containing the folded code block:
[[Image:android_studio_folded_lens_view.png|Android Studio folded code popup lens]]
Figure 57-13
All of the code blocks in a file may be folded or unfolded using the Ctrl-Shift-Plus and Ctrl-Shift-Minus keyboard sequences.
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 57-14):
[[Image:android_studio_code_folding_settings.png|Configuring Android Studio Code Folding Settings]]
Figure 57-14
== Quick Documentation Lookup ==
Context sensitive Java and Android documentation can be accessed by placing the cursor over the declaration for which documentation is required and pressing the Ctrl-Q keyboard shortcut (Ctrl-J on Mac OS X). This will display a popup containing the relevant reference documentation for the item. Figure 57-15, for example, shows the documentation for the Android Menu class.
[[Image:android_studio_quick_documentation.png|Android Studio Editor Quick Documentation Lookup]]
Figure 57-15
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 57-16). 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.
[[Image:android_studio_editor_reformat_code.png|The Android Studio Editor Reformat Code dialog]]
Figure 57-16

Navigation menu