Published on

Getting Started with Android Studio and IntelliJ

Authors

IntelliJ and Android Studio are some of Jet Brain's most popular products. They have different focuses as IDEs: IntelliJ is a polyglot IDE with a strong focus on Java dev while Android Studio (as the name suggests) is geared more towards Android App development. Despite these 2 different areas of focus these IDEs still have quite a bit in common. The core commonality is that they are both keyboard centric meaning that to squeeze as much productivity out them as possible you should master the keyboard shortcuts. This is easier said than done but the quickest way to learn these shortcuts is through muscle memory and reminders. We will go through the initial IDE setup and some essential IDE usage.

Initial Setup

(Windows) Disabling Annoying Screen Rotation

There is an annoying feature that mainly seems to be present on laptops where when you push ctrl+alt+left/right it results in the screen you are on being rotated. Having this mapping available is key to more productive use of IntelliJ/Android Studio as you will see later in this article. Besides this reason I can see no good use for this feature anyway other than pranking unwitting colleagues. Check out the answer here: http://superuser.com/questions/373832/disable-alt-arrow-display-flip-keyboard-shortcut for more details on disabling this annoying feature.

(Mac) Make Your Function Keys Standard Function Keys

Jetbrains' IDEs make heavy use of the function keys for various keyboard shortcuts. By default in Mac the function keys will do things like adjust brightness, volume etc. If you wanted to use the function key as a function key i.e. not a media key then you'd need to first push fn then the function key you wanted.

You may be thinking: "Why would I change this? If I did, it would be more of an effort to change the volume etc." How often do you change the volume vs actually using the function keys? Once you've become more accustomed with the IDE shortcuts the answer will be not too often. To enable normal function keys:

  • Click the little Apple Icon
  • Click "System Preferences"
  • Click "Keyboard"
  • Check the box that says "Use all F1, F2, etc. keys as standard function keys"
How to make make function keys normal Fn keys

(Mac) Use A Different Keymap

When you install IntelliJ or Android Studio on Mac and open it for the first time as part of the initial IDE setup you are asked to choose a keymap to use. The one option is described as being closer to Windows (which I believe is the Mac OS X bindings as apposed to the Mac OS X 10.5+ or other bindings)

I would strongly recommend using the Mac OS X bindings. The main reasons for this are that if you ever switch to using Windows or Linux you will need to learn many of the shortcuts again. Also if you work in an environment where some colleagues use Linux, some Windows and some Mac it becomes a pain when trying to pair program on one another's machines.

If you did not choose a different keymap when starting up the IDE for the first time don't stress. To change it:

  • Push cmd+,
  • Click Keymap
  • In the Keymaps dropdown choose Mac OS X (not Mac OS X 10.5+)
OS X Keybindings

Use The System Maven instead of bundled (Maven projects only)

By default the IDE uses a version of Maven that comes bundled with it. This causes problems if you have custom config for your Maven that specifies different repositories and proxy settings which the default maven settings will obviously not have.

From about version 14 of the IDE to 15 this was an issue, but luckily from version 16 the IDE seems to remember the setting you used on another project. It will still use its bundled Maven but point to your system's settings.xml. Nevertheless it is still better to point to your system's Maven so that if you ever run Maven from the command line it shares the same local repository as when it runs in the IDE.

Whichever version of the IDE you have if you want to change the version of Maven used by the IDE:

  • Go to the settings by pushing cmd+, / ctrl+alt+s
  • Search for Maven
  • In the drop down choose the option that points to your machines maven and not any of the bundled mavens
Use system Maven instead of bundled Maven

Key promoter

One of the issues with learning shortcuts is actually getting awareness of them: we do not even know what we do not know. To this end the IDE extension Keypromoter (https://plugins.jetbrains.com/plugin/4455) helps a fortune. You can also install this via the extensions section of the settings menu ( ctrl+alt+s [Windows/Linux], cmd+, [Mac]) just be sure to install the newer of the two options that shows up.

What Keypromoter does is alert you with a little pop up whenever you click a menu item, tab or other IDE action that has a keyboard shortcut assigned to it. It will also show you the number of times you've clicked that functionality instead of using a keyboard shortcut.

Keypromoter shortcut prompt

Keypromoter also takes it one step further by asking you if you'd like to assign a shortcut to functionality that does not have a keyboard shortcut assigned to it after clicking enough times and it helps you setup the shortcut if you click yes.

Keypromoter add shortcut dialogue
Keypromoter new keymap configuration

Some IDE Basics

Moving Around and Opening Files

Opening Files ctrl/cmd+shift+n

When working with any reasonably large project or even on small projects you often need to open a file that is linked to what you are currently working on.

There are 2 main ways to do this:

  1. The slow way: Push cmd/alt+1 then search through the project's directory structure to find the file you want and double click or push enter on the file to open it

  2. The recommended way: Files in a project are indexed so that they can easily be searched.

  3. By typing ctrl/cmd+shift+n the file search dialogue will show up (this searches all file types in the project) .

  4. Start typing the name of the file and a list of files matching your search should show up

  5. Scroll to the one you want and push enter to open it

This search feature also uses fuzzy matching to find a match for your search term so for example if you have a file called ListActionTest.java you can type LAT or LiAT or any other variants that have a subset of the file and it will find all files that have those capital letters in that order making up the file name.

File search no fuzzy matching
File search with fuzzy matching

Moving to the last place you were at ctrl/cmd+alt+left

When you are editing a file and move to another file you can easily switch back by pressing ctrl/cmd+alt+left. If you want to switch back to where you were before then press ctrl/cmd+alt+right. For both these shortcuts if you repeatedly press the given shortcut it will keep moving back or forward respectively until it cannot move forward/back as there is no history to move to.

Shortcut to move to previous cursor location
Move back to original cursor location

Switching Tabs

Go to the tab at the left or right of the current active tab (Windows Only) alt+left/right arrow

Windows/Linux only: Pushing alt+left/right arrow lets you switch to the tab thats to the left/right of the current one. If you reach the end of the strip it will go to the tab at the other end.

Switching to tab on left/right Windows

Flip Between Tabs (Windows and Mac) cmd/ctrl+tab

If you want to cycle through open tabs push cmd/ctrl+tab. If you keep pushing tab it will keep cycling.

Flip Tabs
Keep Tabbing Between Tabs

You can also push ctrl/cmd+tab then let go, then ctrl/cmd+tab to flip-flop between the file you are currently on and the previous file.

Flip flop between tabs

Go to declaration (ctrl/cmd+b) / implementation (ctrl/cmd+alt+b)

While looking through code you may want to see:

  • Where a variable was initially declared
  • The contents of a method that is being used
  • The interface that is being implemented

To do this easily:

  1. Put your cursor over the method/variable/interface declaration you'd like to go to
  2. Push ctrl/cmd+b
Go to variable declaration

Also if your cursor is over a method name were the method is declared (not where it is called) and you push ctrl/cmd+b the IDE will present you a list of places that the given method is called and you can then choose one you'd like to see and push enter to go there (this is generally much faster than doing a find usages on something but only if that item is not used in too many places).

Go to method usages

If you ever want to see the implementation of an interface method push ctrl/cmd+shift+b to get taken to it - if there are multiple then you are presented with a list to choose from. In these same instances you can also push cmd/ctrl+b but this will take you to the interface/abstract class declaration and not the implementation.

Assigning and Using Bookmarks ctrl+shift+1/2/3/4/5/6

Sometimes you need to bookmark a line you are on for future reference to do this press ctrl+shift+1/2/3/4/5/6 (it is ctrl on Mac also) where you can choose any of the numbers 1 to 6 for the numbered bookmark. After you have assigned it you should see the number of the bookmark in the margin. To then flip back to that line press ctrl+1/2/3/4/5/6 where the number must correspond to the number you assigned previously.

Assign and use a bookmark

If you ever forget what you bookmarked press cmd/alt+2 to bring up the favourites tab. To remove a bookmark go to the line that was bookmarked, ensure your current line is the bookmarked line then push ctrl+shift+the same number to remove it or alternatively you can delete the bookmark in the favourites tab.

Bookmarks pane

Accessing the IDE Menu Using Only a keyboard

If you have ever wanted to access the IDE menu without using a mouse there is a way. The approach you use differs depending on what operating system you are using.

Mac ctrl+F2
Mac Accessing menu without mouse
Windows alt+first letter of menu name
Windows Accessing menu without mouse

Code Assist ctrl+spacebar

By pushing the code assist shortcut the IDE will give you some completion suggestions in terms of the variable and method names that can be used in the context where the cursor is at.

Code assist basics

Intention Actions alt+enter

To better understand how useful this is I'll illustrate some nifty areas you can use it:

1. Method creation

Say you want to create a new method. Instead of going and typing the access level, method name, parameters and return type you can instead:

  1. type the method name where you want to use it
  2. Type the parameters in the method
  3. Assign the new method to the type you want
  4. The IDE will syntax highlight the method in red as it does not exist yet
  5. Place your cursor over the new method name
  6. Push alt+enter
  7. Choose create new method and push enter

The IDE will infer the return type, access level and parameters based on the way you have used the method and assigned it to a variable.

Creating a new method using an intention

Adding a new method parameters or removing a method parameter

  1. Go to a usage of the method
  2. Manually delete/add the desired parameters
  3. The IDE will syntax highlight the method in red as it detects that the method does not match any defined method signatures
  4. Place the cursor over the syntax highlighted method
  5. Push alt+enter
  6. Choose add/remove parameter and push enter
Add a new method parameter intention
Remove a method parameter intention

Quickly iterate through an iterable variable

This applies mainly to collections and arrays (anything you can use a loop to iterate through)

Iterate through collection intention

Switch loop type

There are times when you can use a for in loop to iterate through a collection but you may realize you need to use the index of the item for some reason, that is where this intention comes in.

Switch type of loop

The topics covered in this last post focused mainly on the initial setup of the IDE and key shortcuts and features that -once you have familiarized yourself with- you cannot live without. In future posts I will cover other features of the IDE as well as various tips and tricks.