In this series aims to be a practical guide to demonstrate how to boost your productivity when writing the code.
In the previous series, we introduced how to navigate your code in Android Studio efficiently, you can check it out first if you’re not familiar with it.
The keyboard shortcuts in this article are based on default keymaps on MacOS. Linux and Windows equivalents can be found here. And I use the following symbols to represent key.
- ⌘ = Command Key
- ⌥ = Option (Alt) Key
- ⇧ = Shift Key
- ⌃ = Control Key
Refactor This: ⌃ + T
You may want to change the method name and let IDE help you do everything else for you (find all the occurrences of this variable and replace to new one)? Or adding/removing a parameter of a method call? Just move your cursor to the code you’d like to refactor and use Refactor This to help you
Refactor -> Refactor This .
The following is the common refactor tasks (you can see in the menu):
Change the name of variable, method or class. You also can use
Refactor -> Rename to change resource ID or key, such as
R.string.this_is_new_key. Android Studio will search all the occurrences and change the name for you.
2. Change Signature
Refactor -> Change Signature. This is very useful when you want to change the return type or parameter of a specific method. **Reminder: just double check after performing this operation, especially when adding a new parameter, you may need to provide the default value or make sure that the values are expected.
Refactor -> Move. This is for moving a member / method to a specific class or module. You can change the visibility modifier as well.
Quick Fix: ⌥ + Enter
When you encounter something error, it could be compile error, missing return statement or need to implement the methods…etc., you can find the hint how to fix for any errors, issues or warnings by Quick Fix. (In Android Studio, this action is called “Show Intention Actions”) **Reminder: The quick fix results are not 100% correct though, but it’s still very helpful at the most of time.
Generate: ⌘ + N
Code -> Generate. Android Studio provides various ways to help you to generate common and recurring code such as getter/setter (in kotlin data class, you could get rid of this, finally!!)
Implement / Override
When inheriting a class, you may need to override some parent methods, here to use
Code -> Override.
Get tried of writing dummy getter/setter for fields in class? Try
Code -> Generate -> Getter/Setter to help you.
Code Completion: ⌃ + Space
When writing code, you may not remember the entire method name, you can use Code Completion to help you. Use
Code -> Completion or just type Use
. (a dot), then it will popup a list of methods for you to pick. **Small tips: You can use
Tab key to complete the statment and delete everything untile the next dot, semicolon or space.
Surrounding With: ⌘ + ⌥ + T
To wrap a block of code (such as if-else, try-catch…etc.) for some statements, Use
Code -> Surround With.
That’s it. Hope you write the code much more efficiently.