The powerful Android Studio
June 2016
Android Studio is the official tool for Android development these days. Being developed at the top of Project IntelliJ idea, takes to advantage (almost in its entirety) features of edition, DE Bugging, analysis, refactor among many and other categories for developing on an effective.
In the latest version (2.2 at the time of the writing), Android Studio includes a IoT of improvements like the new UI edit Or, interaction with the newconstraintlayout ViewGroup and much more.
This article doesn ' t put the focus on covering these new features. In this one, I would like to give importance to the role that plays IntelliJ idea Overandroid Studio Besides a few more Ti PS that I use every day.
Because of given at the amazing event exfest ' all, the intention of this article are to serve as media support ( There weren ' t slides) of the said, and of course, a new excuse to write a new article:),. Let ' s get started!
Disclaimer
All shorcuts shown in this article corresponds to theMac OS X 10.5+
The green box which suggests the shotcut used at the bottom of the GIFs are a nice plugin called Presentation Assistant, PE Rfect for presentations and pair programming:).
Miscellaneousavoid Show the logcat automatically
It could is interesting to disable the expansion of the Windows Android Monitor during every time when run our app (Since the default behavior on a run configuration was to show it).
To achieve this, the Run Configuration , the We is using the in and our project just disable that tick at ' Edit Config Urations/android application/miscellaneous.
No tabs
As Hadi Hadiri rightly says in he article, using tabs could result in the following hassles:loss of context, a valuable Space in the editor consumed and the fact, also the interaction with tabs are used to require using the trackpad or the Mouse.
If You believe this tabs don ' t offer a benefit to you, disable them under settings/editor/tabs
setting the Placement preference To None.
IntelliJ Idea offers many ways to move effectively through the code without need tabs.
Navigation
One of the goals which the JetBrains team encourages to the users are that we had to use the mouse as less as possible. There is tons of actions and commands which allow working in a very effective it without leaving the hands from the keyb Oard gaining speed and accuracy.
Find classes, files, and actions
IntelliJ idea and, as the result, Android Studio offers solutions finding files, classes, actions.
Search Everywhere-? + ?
It shows a dialog for search all types of elements:classes, files, actions, etc. It ' s recommended to avoid this action and use the specific ones since search everywhere could be slower and expen Sive in the term of your machine resources.
Search types-? + O
It allows finding enumerations, classes, interfaces, etc fastly.
Search files-? + ? + O
It allows finding every type of files, useful for XML files like layouts, resources, etc.
Search actions-? + ? + O + A
It's possible to search and the run actions from this feature, the actions that live under menus, preferences, tool windows, etc, Also suggests the shortcut of some actions, so, if you forget it can fastly remind it searching for the action.
Bonus:
It's not necessary-to-write the full name of whatever we is looking for. If we are searching for a class called CharacterDetailPresenter
, we ll find it just typing CharacterPresenter
.
We can go to a specific line looking for classes (? + O) Writing a colon and the number of the "the" after the searched CLA SS name, for example: CharacterDetailPresenter:50
.
If we type on /
the beginning of the text of the file, the we is looking for, we'll find folders. For example, with /land
, the dialog would suggest the folders related to landscape configurations.
We can filter the folder which contains the searched file, for example, values-es/strings.xml
orvalues/strings.
Project window-? + 1
For browsing through the project files, a nice solution are to use the Project window which can be shown or hidden with the Shortcut ? + 1.
Bonus:
With ? +? +?/?, we can modify the size of the focused panel without using the mouse.
We can perform searches writing the name of the the file or folder that we is looking for right in the browser, it'll highl Ight the matches and would restrict the positions selected with the arrows for these matches.
Jump to navigation bar-? +↑
The navigation bar is a interesting element to interact with. Using it, we can navigate through the project files as we usually do with the project window. The shortcut +↑ displays the bar and puts the focus into it even if it ' s disabled.
Bonus:
- We can perform some file operations inside the navigation bar context, like create new files ? + N or delete them ?.
Show and hide Windows
There is different ways to manipulate windows:show and hide a single one, the last active window giving it focus , restore the focus back to the editor when we are using a panel, etc.
Hide/restore All Windows-? + ? + F12
As its name indicates, it allows to hide and restore all visible windows.
Jump-to-last Tool window-f12
Restores the visibility of the last window used, also puts the focus into that tool.
Back to the editor
When we have the "focus in a" window, we can back to the editor without using the mouse with the key ?
Recently files
These three actions can help us to save time if we are working with a group of files:
Recently Files-? B
It shows files that has been opened recently.
Recently Changed Files-? + ? B
It shows files that has been opened and changed recently.
Structure of a file
A quick method to navigate through the different attributes and methods in a class is using the action file scructure< /c1>, can is used with shortcut? + F12, or finding the action with? + A and typing file structure.
This feature, as many other, allows searching on it, allowing find quickly what we is looking for and place the caret ACC ordingly.
Similarly, the window structure (? + 7), shows the same information but in a permanent the.
Show implementations and Super Method
It's useful, sometimes, to show the current implementations of a superclass, interface or a method. Using? + B in the name of the class or method selected, allows, in a glance, check the current implementations navigate to them p Ressing the ENTER key.
Similarly, with the shortcut? + U in a implementation, we can navigate to the method or class which is being implemented.
Next highlighted ERROR-F2
When Android Studio highlights some compilation errors, usually we use the mouse-scroll until their location and fix th Em.
With IntelliJ idea, we can place the care right on the left of the Erros using the feature Next highlighted Error (F2), avoiding leave your hands from your amazing keyboard.
First, F2 'll iterate the caret over all the errors. Once They is solved, the cursor would be a placed left of the warnings according to the current inspectionprofile.
Las Edit location-? + ? + ? -
Last Edit location
When we is changing the code in classes with a huge number of lines, we tend to edit different sections of the class. Put Some declarations, edit the body of a method, etc.
It could is useful after add some declarations place the caret back to the previous edition without using your mouse.
The action last Edit location (? +? +?), does exactly. Moves the caret to the previous edition location without changing the code.
Even if the edition have been made in a different file, the editor would place the caret right in the file where the edition Was made.
Editionreplace instead append
When we is writing code, usually we use the completion dialog on order to add suggestions.
When we decide to add a suggestion pressing the ENTER key, the new sentence was added left of the old one, causing an error .
If instead ENTER we press TAB, and if desired method call have the same number of parameters, the new code would be Automati Cally set with the old parameters.
Complete Current Statement-? + ? + ENTER-
Complete current Statement
Using a powerful IDE like Android Studio, there are no need to worry on some syntactic elements like braces or semicolon S.
There is a action called complete current Statement which automatically add the necessary elements in some Conte XTs.
Add Selection to the Next ocurrence-? + G
In some situations, it could is very effective use multiple carets on order to change multiple sentences at the same time.
We can achieve it, with the feature Add Selection to the Next ocurrence (? + G), selecting the pattern where we w Ant to add carets.
Android Studio and IntelliJ idea give us even some cool editing tools in this feature, like cut and paste fragments of cod E, selection tools, etc.
A practical use could is, for instance, changes the binding of our views in an activity or fragment to replace them by Terknife annotations.
Join Lines-? + ? + J
When there was a string concatenated multiple times in different lines, it could was useful to join those lines int o A single one, with the action join lines We'll achieve a single sentence without have to worry about delete t He concat operators.
Check the parameters info-? + P
Using the completion for add-a call-to-a method, Android Studio shows a popup with the required parameters, this popup hid Es after a few seconds.
Using the action Parameter Info with? + P, we can check what parameters is needed for the desired method call every time we want.
Surround with-? + T
Sometimes, it could is interesting to wrap certain code for evaluating it with a condition, iterate through it or capture An exception.
For the purpose, IntelliJ idea and in consequence, Android Studio, offers a tasty feature called surroundwith, Operable with a dialog fired by the shortcut? + T.
With that dialog we can choose among different actions:conditions, loops, exceptions, etc, even live templates.
Bonus:
If we fire this dialog in a XML context like a layout file, a few suggestions would be shown. With a bit of ingenuity and a live template We could, for example, wrap a view or ViewGroup into another ViewGroup, in a very easy.
A example of a live template used with the surround with dialog to wrap views Insider ViewGroup S.
Move sentence Up/down-? + ? +↑/↓
In order to move code was not needed to cut and paste sentences manually. The action Move sentence Up/down under the selection of a sentence of a group of ones, would be useful to Move cod E in an effective.
Bonus
This action could the result useful editing layout files in XML. If We select a view, we could move it into an existing viewgroup in the same file.
Extend/shrink selection-? +↑/↓
Android Studio, thanks to IntelliJ idea inherits a mechanism really interesting whe we select Code.
Using the shorcut? +↑/↓in a specific section of the code, would extend or collapse the selection until the next point regarding the nearest scope.
Completitionlive templates-? + J
The Live Templates is a powerful mechanism to avoid to write boilerplate code. They can customized with a lot of different options in settings/editor/live templates
.
Android Studio, by default, have a lot of live templates ready to use, both for contexts like Java and XML.
With the shortcut? + J We can show a dialog with the available live templates for the context where we are.
Debuggingattach
DebuggerTo Android Process
It could is interesting avoid to run a debug session from Android Studio, (? D), because we have tons of breakpoints configured, we want to force a state before INIT the debug session, etc.
For that, Android Studio includes a action called Attach Debugger to Android Process.
Conditional Breakpoints
In code which are called multiple times, it could be annoying if we breakpoint is called every time which are fire, and may Be, we purpose is to check for that code in a specific situation.
We can configure the breakpoint for being fired only if a condition returns true and pressing the right click at the Breakpoi Nt.
The input would open the completion dialog with the context of the breakpoint while typing.
Force a State
When a breakpoint was fired, we usually check the state of the execution. Instead Just read we can also write and force the State to enable a certain condition of our code.
With the action evaulate expression (also from the Watches window) in adition to check the states of certain vari Ables We can also write the desired value on them.
Different Types of breakpoints
Unsetting the button suspend tick in the breakpoint configuration dialog with a right click on a breakpoint, we C Ould enable the option Log message to console.
Messages would be a print under the tab console at the Debug tool window (? + 5).
Besides, we could print logs with an specific expression, which can is set at the same dialog under the Log E valuated Expression option. Once again it would suggest sentences of your context with the completion dialog while you were typing the expression.
Conclusion
Print the Cheatsheet and hang it to your wall in order to has something to look during your gradle build times (You alway s could read your code btw! :D).
Useful shortcuts for Android