Android Studio Tip #004: Finding concrete methods of Interfaces
Have you ever found yourself navigating through code and ending up at an Interface, instead of the concrete implementation you were hoping for? During Yun Cheng's talk - MVP The Life-Changing Magic of Tidying Up Your Presentation Layer (at 360 AndDev), her and her team uncovered that exact command.
Have you ever found yourself navigating through code and ending up at an Interface, instead of the concrete implementation you were hoping for? During Yun Cheng's talk - MVP The Life-Changing Magic of Tidying Up Your Presentation Layer (at 360 AnDev), her and her team uncovered that exact command.
What is it?
The Go To Implementation(s) action, which will take you to the concrete instance of and Interface. If more than one concrete class exists, a picker will be displayed that will let you choose which instance to go to.
How do I use it?
Position your cursor on an Interface method and press ⌘+⌥+B
. Note: you can do this from the Interface class itself or you can do this from a invocation of the method.
Links:
Android Studio Tip #003: Swap Intention Action
Today’s tip is going to be short and sweet. I was testing an internal library that I had little experience with a few days ago, when I noticed that for one method invocation I had reversed the inputs. This was partially to do with the lack of documentation and the fact that this legacy library lacked human readable parameter names, but I digress. The problem I had was that this method invocation was used enough that I didn’t want to swap all of the values by hand.
Today’s tip is going to be short and sweet. I was testing an internal library that I had little experience with a few days ago, when I noticed that for one method invocation I had reversed the inputs. This was partially to do with the lack of documentation and the fact that this legacy library lacked human readable parameter names, but I digress. The problem I had was that this method invocation was used enough that I didn’t want to swap all of the values by hand.
After searching Android Studio for all its actions (⌘+⇧+A) it turns out that what I was looking for does exist. For methods with two parameters only, you can swap the parameters. This action is aptly named swap
and though it doesn’t have a direct hot key, you are normally only a couple of keystrokes away from using it.
What is it?
The swap action is what the people at JetBrains consider an Intention Actions. You may be familiar with actions that show up as a light bulb icon next to your cursor, which happens under situations like missing imports, possible refactoring and potential bugs.
How do I use it?
Simply position your cursor anywhere inside your method invocation’s parentheses. And press (⌥+⏎), the Intention Actions drop down will appear and last on the list of the drop down should be swap ‘x’ and ‘y’
where x
and y
are the parameters you wish to swap.
I am not always swapping parameters so I can’t say this was a monumental find for me but as always knowing of its existence may save me more time in the future and hopefully it does you as well. What’s more important, or interesting, to note is that if you ever want to see what Intention Actions are available for your code you can always press (⌥+⏎) and find out.
P.S.
If you press (⌥+⏎) on string literals you can extract those strings directly to your Strings.xml
. Which is great for refactoring legacy code with hardcoded strings.
Links
Android Studio Tip #002: Re-enable Test Artifact Switching
It's no doubt that Android Studio gets better with every version. Who doesn't love Instant Run? There is one thing I am sad to see change without mention. Test Artifacts are merged together by default now.
It's no doubt that Android Studio gets better with every version. Who doesn't love Instant Run? There is one thing I am sad to see change without mention. Test Artifacts are merged together by default now.
If you aren't familiar with testing or test artifacts in Android, Google themselves have created a really nice starting point answering most of your questions. Also I love to note that I am a huge fan of the Arrange-Act-Assert pattern and TDD. I would definitely check them out if you are new to testing or programming in general.
I usually run my tests at the command line via the same build task that will be run on the server. This validate that what I am working on will in fact pass on the server. However, if I want to debug through my tests in IDE I usually like to isolate which test artifact I am running. The reason being is that tests run on the regular JVM are usually faster and it means I don’t have to have a device connected to run them.
So if you are like me chances are that you would like to not have your tests ran together out of the box. In order to get back the old Android Studio behavior navigate to the Experimental tab in Preferences.
Preferences>Build, Execution, Deployment>Build Tools>Gradle>Experimental
And simply toggle off the Enable all tests…
box and apply. Voila! You now can switch between your Unit Tests and Android Instrumentation Tests.
TL;DR:
Android Studio Tip #001: Bookmarks and Favorites
Chances are if you are an Android developer there are a bunch of really amazing features hiding in Android Studio that you have probably yet to discover. I am definitely not the exception to that and yesterday I stumbled upon (/took the time to understand) a few features that have so far made my life much easier. These features are Bookmarks and Favorites.
Chances are if you are an Android developer there are a bunch of really amazing features hiding in Android Studio that you have probably yet to discover. I am definitely not the exception to that and yesterday I stumbled upon (/took the time to understand) a few features that have so far made my life much easier. These features are Bookmarks and Favorites.
Conceptually these are about the most basic features that we could possibly ask for and are pretty self-explanatory. Besides the fact that it is painfully obvious what the features do, I was also frustratingly upset with myself because of where these features are located, which is in the Tool Buttons bar on the left side of Android Studio. These features weren't even hiding from me but staring me right in the face.
Favorites
So as I said before the Bookmarks and Favorites features are embarrassingly easy to understand. Favorites can be made of any file, from Java classes to XML resources and everything in between. To add the current file you are on to Favorites you can simply press (⌥⇧f) or you can right click on any tab and then click on Add to Favorites
. You will probably need to create a new Favorite list or if you have any other lists already created you can easily add to them. I have found that it is helpful to name Favorite lists after tickets I have worked on or keywords so that I can easily find them.
Speaking of finding Favorites there are numerous ways you can get to the Favorites menu. The easiest way by pressing (⌘2) otherwise you can look on the Tool buttons bar (on the left hand side of the screen) and find the vertical Favorites tab. Note: If you are in Distraction Free mode you can easily show the side bars by pressing (⌘) two times. Also this view is searchable so if you start typing while you have something selected in that view it will start searching all of your Favorites and Bookmarks.
Bookmarks
Bookmarks are a bit more specific than Favorites. Bookmarks are done per line and can be created by pressing (fn F3) which will visually put a check next to the line, similarly to debug break points. You can as well have mnemonic bookmarks, by pressing (fn ⌥ F3) which in essence allows you to swap the checkmark out for number 0-9 or character A-Z. It's important to note that you can't reuse mnemonics. As well another difference from Favorites is the ability to give Bookmarks descriptions. Simply navigate to the Favorites tab on the Tool Buttons bar and right click on any Bookmark and select Edit Description
.
There you have it a great way to create and organize Favorites and Bookmarks without cluttering up your IDE with tabs.
TL;DR
Favorites and Bookmarks are possible in AS and amazing!
Favorites
- Add (⌥⇧f) or right click on tab
- View/Edit/Remove Favorites (⌘2)
Bookmarks
- Add/Remove Bookmark (fnF3)
- Add/Remove mnemonic Bookmark (fn⌥F3)
- View/Edit/Remove Bookmarks (⌘2)