Auto format code in Android Studio/Intellij IDEA

Ankit Goyal
Nerd For Tech
Published in
2 min readJun 9, 2021

Every organization/company has certain rules for code formatting and many times developer forget about these rules and face lot of issues like merge conflicts, Jenkins builds getting failed etc. to solve this you can use a utility MACROS available in all Android Studio and all other IntelliJ products.

Record the macro

  • Open any file in the editor.
  • From the main menu, select Edit | Macros | Start Macro Recording.
  • Press ⌥⌘L to reformat code (Code | Reformat Code ). Then press ⌘S to save all changes (File | Save All ). Android Studio/IntelliJ IDEA will show the performed actions in the status bar.
  • Stop recording by selecting Edit | Macros | Stop Macro Recording.
  • In the Enter Macro Name dialog, specify the name for the new macro and click OK.
  • If the macro is intended for temporary use only, you can leave the name blank.

Play back a macro

  • To play back a temporary macro, open the Edit menu, point to Macros, and click Play Back Last Macro.
  • To play back a named macro, open the Edit menu, point to Macros, and click the necessary macro name.

Edit macros

  • Open the Edit menu, point to Macros, and click Edit Macros.

Bind a keyboard shortcut to a macro

  • In the Settings/Preferences dialog ⌘,, select Keymap.
  • Expand the Macros node and select the created Reformat and Save macro.
  • Right-click the macro and choose Add Keyboard Shortcut in the context menu
  • In the Enter Keyboard Shortcut dialog, press ⌘S to be used as the shortcut and click OK.
  • Android Studio/IntelliJ IDEA will warn you that the shortcut is assigned to another action. Click Remove to remove the ⌘S shortcut for the File | Save All action. You can always reassign it later if necessary.
  • Click OK to apply the changes.

Now, when you press ⌘S, Android Studio/IntelliJ IDEA will invoke the new macro: reformat the current file and save your project.

--

--

Ankit Goyal
Nerd For Tech

Android Platform team member at Walmart Global Tech