Do you find yourself repeating the same tasks over and over again during development cycles? Ever wish that you could automate some of them to make things run more efficiently? Today is your lucky day. Whether you’re a veteran developer or you’ve just started out on your app development journey, our Android developers are going to give you some tips and tricks that they’ve learned along the way to ensure that you’re making the most out of Android Studio.
Grab a pen and paper and make note!
Struggling to make sense of your Android Logcat? You might want to consider switching up the colour scheme to help make your logs more legible.
Simply head to Preferences > Editor > Color Scheme > Android Logcat.
You can configure text colour, background style and even apply some effects such as underline or strikeout. The screenshot above displays our colour scheme but feel free to play around and define your own.
Wondering how you can translate this tip for iOS app development? You can check out Pere’s post all about how he implements this simple hack in Xcode to help optimize developer productivity!
IIf you’re struggling to find a class between all of your packages, classes and interfaces, double shift is your new best friend.
In order to avoid scrolling through all of the project hierarchy every time you’re looking for something specific, use double shift. It should open up a pop-up window. Type the name of the class you’re looking for directly in the search box and it should find what you’re looking for instantaneously, as demonstrated below!
As you can see, it’s an easy and efficient way to navigate through files in Android Studio.
If you’re tired of writing the same snippets of code over and over again, create some live templates!
Once you’ve set some up (under Preferences > Editor > Live Template) you’ll be able to find them again by simply typing in their abbreviation and pressing the ‘Tab’ key.
Almost all of the Android Studio live templates are designed to be used with Java. You can, however, create your very own templates to improve your logging.
Firstly, you’ll need to create a new ‘Template Group’ which you can rename AndroidLog. This will include all the things needed to write your logs more easily.
The abbreviation defines the shortcut that will generate the template text in the box underneath so make sure it’s clear and concise.
Once you’ve picked your abbreviation, you need to edit the template variables in order for it to work. As you can see in the following screenshot…
Once everything is defined, we’ll be able to do the following:
Defining live templates through external files looks a little different. Using the last example, the file would look like:
<p> CODE: "https://gist.github.com/kyloner/ae3fff44bc57e1c0a6e63bf5d4f74a50.js"</p>
The file named ‘AndroidLog.xml’ should be located in the following path:
Windows:<your_user_home_directory>\.AndroidStudio<version_number>\config\templates
Linux: ~AndroidStudio<version>/config/templates
macOS: ~/Library/Preferences/AndroidStudio<version>/templates
* We’re fans of vigilant automation here at We Are Mobile First so we urge you to always make sure that you’re diligently reviewing everything you create, even if you’ve used these handy tips and tricks to make the process easier.
If you’re interested in automating the code review process, check out our post on how to automate the Android code review process using Danger.
Distraction Free mode hides all of the existing toolbars so that you’re just left with the editor screen. You’ll still be able to navigate it in the same way, however.
To enter/exit this mode you’ll need to go to View > Enter/Exit Distraction Free Mode.
If you’re someone that could benefit from having a Distraction Free mode for every aspect of your life, you should check out our post on 9 time management tips for developers. You can thank us later!
Presentation mode does what it says on the tin!
To enter/exit this mode you’ll need to go to View > Enter/Exit Presentation Mode.
If you’re looking for a particular location in your code, to adjust the variables and such, you’ll want to use Cmd + Shift + F.
Once you’ve pressed said keys, a new window will open allowing you to search all of the project’s existing code for the desired expression. This window also offers you a preview of the place it appears in.
This combination works in a similar way to the last point but Cmd+Shift+R lets you search for the occurrences in order to replace them with a new expression.
If you’re working on code for a prolonged period of time and happen to find code that you’d like to swap to a different method, in order to make it more readable and easy to edit, you can simply use smart extraction to avoid having to go overboard with the copy and paste function.
By pressing Cmd+Option+M, with the piece of code that you want to move to a new method, you’ll be taken to a pop-up window where you just have to fill in the method name and, if applicable, change any attributes names. This will create a new method with your code selection. Easy peasy, right?!
But there’s more! If the IDE found that this particular piece of code was the same in another part of your file, it will ask you if you want to replace the code occurrence with the method that you just created.
A handy Android Studio hack that has got your back!
Scouring code and hardcoded strings for magic numbers can often be extremely tedious and time-consuming, we’ll be the first to admit.
Great news! Android Studio can help you with that! If you need to move a hardcoded string or number to a variable inside your method, you can move it quickly by using Cmd+Option+V. This combination will check all of the existing occurrences too and replace this number inside your method with the brand new variable. Saving you lots of time and energy!
If you want to extract a string or number to a class constant, you can do that by using Cmd+Option+C and, like before, it will check for all the occurrences in the class and replace them with the new constant name.
So, picture this. You’re making an app that uses coordinates. You’ve written all of your code but wait you’ve forgotten to set X and Y to a lot of variables with different names that would help you read the code in the future. You’ve now got to refactor all of them by going through each variable and adding either X or Y. Snore!
Once again, Android Studio has a solution for that! Enter multiple cursors.
Press Alt+Shift, guide the cursor to each place you would like to refactor and click each. Magical new cursors will appear before your very eyes. When you’ve finished setting all the cursors you desire, write whatever you need to and press ‘Esc’ to finish.
You should now see the added text in each location you set your multiple cursors.
In order to optimise productivity, it’s not uncommon for developers to work using the split vertically view. When this view is applied, we end up using half the screen for one file and the other half for another. Whilst this is certainly useful, it’s going to make code much harder to read if you aren’t following a pattern to define the max line length.
If you’d like to avoid this scenario, you can set a line separator to work as a guideline.
By default, the tabs are displayed on the top and as a single row. It’s quite common to end up with a lot of tabs opened at the same time which makes it really difficult to find the files already opened.
By right clicking on the tabs, you’ll be able to see the following menu that lets users define the tabs placement.
Try a few orientations out and see which works best for you!
When we run a selected configuration to build and install our application, the build can unfortunately sometimes fail – often giving us zero information about said fail on a build that was working perfectly fine just a minute ago!
We faced this issue several times as newbie app developers and can appreciate just how frustrating it is! In order to avoid finding yourself in this situation when your build fails, navigate to the right ‘Gradle’ tab where you’ll be able to find all of its tasks in an expandable menu.
Select the task that has failed and execute it directly from the menu.
It will obviously still fail but at least this time you’ll get more information about the problem which should help you to solve it much more efficiently.
This plugin will make your life much easier when it comes to creating all of the data models you need – a task that no developer likes.
Firstly, we need to install the ‘Generate Kotlin data classes from JSON’ plugin that can be found in the Android Studio ‘Plugins’ repository. Once installed, restart Android Studio.
Get a sinking feeling in your stomach every time you see a hardcoded string? We know the feeling!
That’s the reason why we use this useful shortcut to move the hardcoded strings to our beautiful string resources file. Just select a string and use Option + RETURN to name it!
In some instances, we may need to copy and paste more than one line or block of code to another class.
Before you go copying one line after another and go switching from one class to another to paste it, Android Studio has equipped us with a wonderful multi-copying tool! It allows you to copy all of the lines/blocks you want and then paste them wherever you want. Just use CMD + SHIFT + V.
We don’t like to use a lot of copy and paste on our code but this tool helps us greatly when it’s needed.
---
What are your top tips and tricks for getting the most out of Android Studio? Were you unaware of some of these pro tips for Android Studio? Tweet us at @WeAreMobile1st so we can retweet your tips and share your godly wisdom with fellow developers!
We Are Mobile First is a digital product agency based in Barcelona. Follow us on Twitter, LinkedIn or Medium to be notified of our future posts. We share weekly posts on everything from how to efficiently manage your Android app’s memory to tips on how to create Android apps that are both well-designed and functional.
(Hero image credit: Android Developer's blog)