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.

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.