Android Studio has many keyboard shortcuts that you can use to speed up your programming. You can learn about them from the both the menus and Default Keymap reference that’s available on the Help menu. There's one version of this document for Windows and Linux, and another version for Mac. If you start on Mac, you'll see the file ends with "_maC". To get to the other version from here, just remove "_mac" from the end of the file name.
- [Voiceover] Android Studio has many keyboard shortcuts and other tools that you can use to speed up your programming. As I showed previously, you can learn about the keyboard shortcuts by going to the Default Keymap Reference. This isn't exhaustive. It's for Intellij IDEA, not for Android Studio, but it'll help you get started with your Java programming. I'm working on Mac, and so I went to the Mac version of this document, but to get to the Windows and Linux versions, just trim off the _mac in the file name and then you'll see what's available for the other operating systems.
Here are some of my favorite keyboard shortcuts that I find myself using a lot. First, here's how you can comment and commencement code. Place the cursor either on a single line or select multiple lines and then press COMMAND or CONTROL and the forward slash key. And that will add the double-slash comments in front of the selected lines. To remove those comments, select the lines again and press the same keyboard shortcut again. You'll be toggling the comments on and off.
Another incredibly useful trick is autocompleting your code. I'm working with the previous version of my app, where I created a class name MyTextUtil. And I'll create an instance of that class here in my onCreate method. I'll start with a declaration of the type. I'll type the beginning of the classname and then press either ENTER or TAB. That will autocomplete the classname and, if it's in a different package, will add an import statement for that class.
Then I'll give the object a name. I'll call it util. And then I'll instantiate it with new, and once again I can autocomplete the constructor method. Right now, this class only has a single constructor. But let's change it so it has more than one constructor. First, I'll declare a string variable called name. Then I'll use something called the Generate tool. I'll go to the menu and choose Code, Generate. And again, there's a keyboard shortcut associated with that.
Then I'll choose Constructor, and then I'll generate a constructor method that accepts the name variable. The constructor receives that as an argument and assigns its value to the private field. Then, I'll use the same tool again, once again creating a constructor, and I'll deselect the name argument and click OK and now I get a no arguments constructor. Now, I'll go back to my main activity class and I'll backspace over the parentheses, then I'll type in an opening parentheses and wait a moment, and I see a list of the possible arguments.
There's a version of the constructor tat receives a single argument, a string, and one that receives no parameters. I'll pass in a string, my name wrapped in quotes, and then I'll complete the statement by pressing the COMMAND + SHIFT + RETURN or CONTROL + SHIFT + ENTER on Windows. I'll backspace over this one more time and this time I'll type in My, and then press ESCAPE, and then press CONTROL + SPACEBAR and that will bring up a listing of all available classes and variables that match that string and some documentation over on the left side.
I'll press ENTER or RETURN again, and again I'll wait a moment, and this time, I won't pass in a value and then I'll complete the statement with the same keyboard shortcut COMMAND + SHIFT + RETURN or CONTROL + SHIFT + ENTER. Here's another variation on autocompletion. I'll create a new variable data-typed as the Intent class. This is a member of android.content and I'll name it intent and my goal here is to show you how you can use the autocompleting of a statement to complete a line and make a default assumption about what kind of constructor method you want to use.
I'll type in just the beginning of the classname and then press the complete statement keyboard shortcut, COMMAND + SHIFT + RETURN or CONTROL + SHIFT + SPACE. The constructor method is completed. Now, there's more than one constructor method available here. There's one with no parameters which is what was selected. There's one that receives an Intent object, one that receives a string, one with a string and URI, and so on. When you use that tool to complete the statement, it's called smart code completion and it typically chooses the simplest statement that's available.
If you want to see what's available in terms of constructor methods, place the cursor between the parentheses in a no arguments-version of the constructor and then press COMMAND + P or CONTROL + P on Windows. And that list will pop up. It isn't a selectable list. It's just documentation. But it should help you complete your code. As I've shown previously, you can easily move code around in a Java class. To move a single line, hold down the ALT or OPTION key and the SHIFT key and press UP or DOWN, and to move complete statements, add the COMMAND or CONTROL keys.
Here's what I mean by that. Let's say that you've broken a statement up into two lines. Now on Mac, I'll hold down OPTION + SHIFT and then push the UP key. Only that line of text moves, and that wasn't what I wanted. That broke up the statement. But now instead, I'll hold down COMMAND and SHIFT, and then press UP, and now I'm moving the entire statement even though it's on multiple lines of text. I'll bring that line back so that the statement is on a single line. To duplicate lines of code, press CONTROL or COMMAND and d and then to delete lines of code, press COMMAND + BACKSPACE on Mac or CONTROL + y on Windows, and as I showed previously, if you don't like the keyboard shortcuts that are part of the default map, you can go to your Preferences or Settings, and click on Keymap and make changes here.
To make changes, you'll need to make a copy of the keymap and change your copy. Android Studio's goal is to give you as many options as it can to make your programming fast and efficient. It comes with all these default keyboard shortcuts but also gives you a way to customize the environment so that the product works for you.
- Installing Android Studio on Mac and Windows
- Creating Android Studio projects
- Setting up the development environment, including HAXM and the new Android emulator
- Importing existing code into Android Studio projects
- Exploring the interface, including the editor and project windows
- Managing project builds and dependencies
- Creating new Java classes
- Refactoring code
- Using templates
- Using breakpoints and watch expressions
- Updating apps with Instant Run
- Using Git for version control