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.
- [Instructor] 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 I'll help you get started with your Java programming. Here are some of my favorite keyboard shortcuts that I find myself using a lot. First, here's how you can comment and uncomment code.
Place the cursor either on a single line or select multiple lines, and then press cmd or ctrl and the / key, and that will add the double slashed 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 auto-completing your code. I'm working with a previous version of my app where I created a class named MyTextUtil and I'll create an instance of that class here in my onCreate method.
I'll start with the declaration of the type. I'll type the beginning of the class name and then press either Enter or Tab. That will auto-complete the class name 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 auto-complete 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 MainActivity 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 that 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 cmd + shift + return on Mac, or ctrl + 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 ctrl + spacebar, and that'll 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 auto-completion. I'll create a new variable data type 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 auto-completing 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 class name and then press the complete statement keyboard shortcut, cmd + shift + return, or ctrl + shift + space.
The constructor method is completed. Now there's more than one constructor method available here, there's one with no parameters, which was 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 create 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 cmd + p, or ctrl + 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 opt key and the shift key and press up or down, and to move complete statements add the cmd or ctrl keys. Here's what I mean by that. Let's say that you've broken a statement up into two lines, now on the Mac I'll hold down Option + Shift and then press 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 ctrl or cmd and d. And then to delete lines of code press cmd + Backspace on Mac, or ctrl + y on Windows.
And as I showed previously, if you don't like the keyboard shortcuts that are a 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 of 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
Skill Level Beginner
Q: This course was updated on 04/27/2017. What changed?
A: New videos were added that highlight the new features introduced in Android Studio 2.3. In addition, the following topic was updated: update apps with Instant Run.