Tips and Tricks of Android Studio

Being an Android Developer, we use a number of IDE for writing the code for our Application. For example, Android Studio, Eclipse, Visual Studio (in some cases), etc. But the most famous and the recommended one among these IDEs is the Android Studio. Nowadays, everyone uses Android Studio to write codes for their Application because Android Studio gives us a lot of functionalities and features, that make our speed of writing codes fast. Also, we get an organized way to manage our project in Android Studio.

No prizes for guessing! In this blog, we will look upon some of the Tips and Tricks of Android Studio. It is rightly said that:

A sword never kills anybody; it is a tool in the killer’s hand.

No no, we are not going to kill anyone :)) I mean to say that we all know that Android Studio provides a number of features that can be used for faster Application development but do we use all those features? The answer is NO. But if you want to write codes in a faster and convenient way, then you must have to use all the features that Android Studio provides. So, let’s look upon some of the Tips and Tricks of Android Studio.

1. Setting the Theme

The theme of the IDE plays an important role in determining the time that we can spend on Android Studio to write code. So, a good and eye soothing theme must be there. To change the Theme and color of the Android Studio, go to File > Settings > Appearance & Behavior >Appearance > Theme and then choose the desired Theme. To change the color of the editor text, you can go to File > Settings > Editor and change the desired property. (Note: In MAC, instead of going to Settings, you can find the options in Preferences)

2. Changing Color of Logcat

Logcat is the best way to find any bug present in the application. On the Logcat, we can see each and every event going on with a particular Android Application. You can change the color of various options available in Logcat by going to File > Settings > Editor >Color Scheme > Android Logcat and change the default color by unchecking the “Inherit values from” option.

3. Profiler tip

You can check the CPU, Memory, Network, and Energy usage of your application in the Android Studio. All you need to do is run your application on your device or emulator and then click on the Profiler button.

After pressing the button you will get a window at the bottom of the Android Studio that will show a graph which describes the usage of CPU, Memory, Network, and Energy by your application. The window is super small. So, you can increase the size of the window by pressing the cmd + shift + up arrow (in mac) and ctrl + shift + up arrow (in windows/linux).

Also, you can open any of the four options available to you i.e. CPU, Memory, Network, and Energy to have a piece of detailed information about the same. For example, you can check if there is some memory leakage by analysing the Memory graph.

4. Opening Tool window

In Android Studio, there are various tool windows for different tasks like Project, Logcat, Run, Todo, etc. You can open these windows by simply clicking on the small buttons corresponding to these windows. But to open it with the help of keyboard you need to note the underlined number that is present on the small button to open that window.

In the above image we can see that to open Logcat we have to use the number 6. So, to open Logcat press alt + 6. Similarly, you can use alt + underlined number combination to open the window.

In many cases, we don't have any underlined number for a window. For example, to open Gradle, we don't have any underlined number. So, in that case, you can take the help of switcher. Press ctrl + tab and then release the tab and keep on pressing the ctrl button. You will see the below image:

Here, you can see that G is underlined in case of gradle. So, now you can press G to open the Gradle(the switcher is open here).

You can close the recently opened tool window by pressing the shift + esc button. To get back the window again, you can press f12 anytime.

To close or hide all the opened tool window and to get the maximum space available for the editor, you can use ctrl + shift + F12.

5. Find everything

While developing Android application, we make many files and in those files, there are many classes and symbols. So, it becomes a harder task to find which file is located where. Don't worry, by pressing shift (two times) you will get a window to search anything you want to find.

6. Finding Actions

Android Studio contains a number of features and it is impossible to remember the place from where a particular feature can be used or activated. So, in order to search these features in a very fast manner, you can use Ctrl + Shift + A (in Windows and Linux) or by using Cmd + Shift + A (in Mac).

7. Rename file/variable

There are many cases when you want to change the name of a file or a variable, but in order to do so you have to change the name at all places where these files or variables are used in the project. Don’t worry, Android Studio provides a better way of doing this. All you need to do is select a file and then right click on it and then click on Refractor > Rename or simply Shift + F6 and then enter the new name Refract the changes. This will change the file name throughout the project.

8. Camel Hump Navigation

We know that by pressing the ctrl key with a left arrow we directly move to the next word in our code and by using shift with the left arrow we select a particular word. So, by using ctrl + shift + left arrow, we can directly select the next word. But what if we want to change some part of a variable name that is following the camel case notation. For example, if our variable name is myVarName and we want to change it to myVariableName. Should we traverse one by one character to reach to Var and change it to Variable? No.

Press shift two times and search for "camel Humps". Then, set the "Use Camel Humps words" option to ON.

Now, by pressing ctrl + left arrow, we will directly move to the next capital letter i.e. if the variable name is myVarName and the cursor is at m then after pressing the ctrl + left arrow the cursor will move to V. You can select the Var by pressing ctrl + shift + left arrow and change it to Variable.

9. Split Screen

Split Screen is a new feature that is provided by almost every modern day’s IDE. By using Split Screen, you can use two screens at a time i.e. the same window will be split into two part, either vertically or horizontally. In order to split the window, right click on the tab of the file that you want to split and then choose Split Vertically/Horizontally.

10. Distraction Free Mode

You can use the Distraction Free Mode of Android Studio to remove the editor tabs and tool-windows buttons. By doing so, your focus will be on the coding part and nothing else. To enable the Distraction Free Mode, go to View > Enter Distraction Free Mode.

11. Code Completion

You can use the Postfix Code Completion to write the code in a faster way. All you need to do is press Ctrl + J or Cmd + J(in MAC) and Android Studio will give you a number of possible codes. For example, if you want to write the code for a Toast, then just write “T” and press Ctrl + J, you will be given a list of possible items. Select Toast and click Enter.

12. Navigate through methods

In an Android file, there are various methods and it becomes a harder task to navigate from one method to other as a method can be very large and you need to scroll to move on to the next method. So, to do it quickly, we can press alt + down arrow to move to the next method in the downward direction and alt + up arrow to move to the next method in the upward direction.

13. Find information about methods and parameters

You can find the information related to a method by putting the cursor on the required method and then pressing the ctrl + Q keys.

To get the information of the parameters of the methods, you can press ctrl + P.

14. Extended Selection

There are various blocks present in a file. For example, in a method block, there can be one or more if block, for block, while block, etc. So, to select some lines together we can use the approach of extended selection. Press ctrl + w and the desired lines will be selected. To select more lines, keep on pressing ctrl + w. (Try this in Android Studio for better understanding).

15. Move Statements up/ down

Inside a function, you can select one or move lines of code and collectively move all the selected statements to up or down by pressing ctrl + shift + up arrow (to move up) and ctrl + shift + down arrow (to move down).

16. Find all occurrences

Sometimes we use a variable many times in our code and at a certain time we may need to change the variable name. So, firstly, we have to find all the occurrences of the variable name and then we can rename it. To find all the occurrences of something, press ctrl + f and enter the name of the variable and then press alt + j to get all the occurrences of the same.

17. Working Offline

If you want your Android Studio to run faster, then you can use the Offline mode of Android Studio. To start Offline mode, Open the Preferences window by clicking File > Settings (on Mac, Android Studio > Preferences) and in the left pane, click Build, Execution, Deployment > Gradle. After that check the Offline work check box and click Apply or OK.

18. Line Number and Method Separator

You can put line numbers to your code and method separator also. This will make your code more readable. All you need to do is Click on File > Settings > Editor > General > Appearance > Select Line Number/Select Method Separators.

19. Multicursor Feature

You can edit to more than one lines at a particular instant of time. For doing so, go to a particular word and click ALT + J (Windows and Linux) or CTRL + G (for Mac). This will select the next appearance of that word in the file. Now you have to write only once.

20. Last Copy & Paste

You can choose from your last 5 Copy/Paste operations by clicking ctrl + shift + v (in Windows or Linux) or cmd + shift + v (in Mac).

21. Open Class

In order to open a class in Android Studio, press ctrl + n (in Windows and Linux) or cmd + o (in Mac).

22. Open File

To open file press ctrl + shift + n (in Windows and Linux) or cmd + shift + o (in Mac).

23. Find Declaration

You can find the declaration of any class and its method by clicking on that class and pressing ctrl + b (in windows and Linux) or cmd + b (in Mac).

24. Move between tabs

You can move between tabs by pressing alt + left/right arrow (in Windows and Linux) or cmd + shift + [ (left) and cmd + shift + ] (right) in Mac.

25. Last tab

You can switch between last tabs by using ctrl + tab (in windows and Linux) and cmd + tab (in Mac).

26. Format your code

You can format your code by using ctrl + alt + l (in windows and Linux) or cmd + option + l (in Mac). Formatting means rearranging the declaration of attributes. For example, “id” of a TextView should be written before the width and height property. This makes the code more readable.

27. Auto-indent line

You can apply indentation by using ctrl + alt + i (in Windows and Linux) or control + option + i (in Mac).

28. Find

You can find something in a file by using ctrl + f (in Windows and Linux) or cmd + f (in Mac).

29. Find and Replace

You can use find and replace feature by pressing ctrl + r (in Windows and Linux) or cmd + r (in Mac).

30. Hardcoded String to resources

You can move your hardcoded string to string.xml file or any other resources file by just clicking on the string and pressing alt + enter (in Windows and Linux) or option + return (in Mac) and then Extract String Resource.

31. Build and Run

To build and run your app, press shift + F10 (in Windows and Linux) or control + r (in Mac).

32. Last edited location

You can directly move on to the last edited location in a particular file by pressing ctrl + shift + backspace (in Windows and Linux) or cmd + shift + backspace (in Mac).

33. Close active editor tab

You can close the active editor tab by pressing ctrl + F4 (in Windows and Linux) or cmd + W (in Mac).

34. Resource Manager

Resource Manager is the newest tool in the layout editor of Android Studio. It shows every single resource that you have in your application i.e. you will see drawable, colors, layout, etc.

It's a very nice way to navigate through all the resources very quickly.

35. Using Plugins

You can use a number of plugins available in Android studio to improve the functionality of Android Studio. Some of these may be:

  • ADB Idea: It helps to execute adb commands to uninstall, kill, start, restart or clear an application.
  • Android Material Design Icon Generator: This plugin will help you to generate Material Design Icons for your project.
  • Git flow integration: This helps you to have a defined flow of version control of your project.
  • Genymotion plugin: It is used to provide a smooth flow for Genymotion virtual devices.
  • Key Promoter: It is used to make your own shortcut keys for a particular task.

Conclusion

Android Studio has a number of features and by using these features we can improve various things in our Android Project like Time Management, Code Formatting, Better Understanding of Code, etc. We looked upon some of these features. If you want to know more about your project in Android Studio then go to Help > Productivity Guide (Bonus tip :))

That’s it for this blog. Please let us know your favorite tips & tricks of Android Studio.

Keep Learning :)

Team MindOrks!