How do I align/format code in Android Studio?
Asked Answered
P

6

34

Is there a way/shortcut/built-in feature that can align code for operands, like '=' signs?

For example, there is a XAlign for Xcode (https://github.com/qfish/XAlign), allowing the user to select code that needs to be aligned and use a shortcut to align it automatically.

There is a Fields Group feature in Android Studio which can "Align in columns", but it does not work for code that is already written.

Pre answered 29/1, 2015 at 19:41 Comment(2)
possible duplicate of Code formatting shortcut in Android studioSubplot
I don't think there's a built in formatter for aligning by = in IntelliJ.Brisling
T
56

Indent code in Android Studio:


Windows

Ctrl + Alt + L


Mac:

Option + Command + L

Touchwood answered 16/12, 2015 at 16:0 Comment(6)
That changes the screen orientation on my laptop. VERY annoying.Georgiannegeorgic
@CoolMind, Thanks for the suggestion, but I no longer have that laptop. I am on a MacBook now.Georgiannegeorgic
Works good on a file level but I wonder if there is a way to do modification on entire project or even better to do it automatically when I open a project? UpvotedAdamandeve
@Adamandeve Use the re-format code shortcut in the project/android file view highlighting/selecting the folder thereby navigation to Code > ReformatNorthwestwards
Working on macbook Catalina. ThanksReservoir
It's ctrl+alt+shift+L on my WIndows machine. I wish I knew where the menu item was ... if there is one.Rhythm
H
6

In Eclipse which comes with Android Studio Bundle, the shortcut key is as follows to initiate Auto Formatter:

Ctrl + alt + L

If you want to change this combination, go to menu PreferencesKeymap and set to custom.

Hypothec answered 29/1, 2015 at 19:45 Comment(3)
I am on Mac. [Command + Option/Alt + L] does not work. i have the Field group "Align in column" turned on. but selecting code and reformatting does not seem to work.Pre
Yes. Keymap are default. using the shortcut bring up the Reformat Code but it does not reformat the code. the = sign does not align for variable declarations for the selected code.Pre
i think it's CTRL + SHIFT + FFundamentalism
P
6

The alignment for variable declarations works for only those that are outside of functions. The alignment feature in menu PreferenceCode StyleWrapping and BracesFields Groups"Align in Columns" does not work for those inside the brackets/braces.

Here is an issue ticket on the JetBrains website: https://youtrack.jetbrains.com/issue/IDEA-56947#

This feature is available, but not in the stable release yet. It is available from build: 140.1761

At the time of writing this answer my current build is "135.1653844, built on December 18, 2014" on Stable Release.

Pre answered 29/1, 2015 at 21:52 Comment(0)
U
4

Windows:

Ctrl+Alt+L

Linux:

Ctrl+Shift+Alt+L

macOS:

⌥ option+⌘ command+L

Underfur answered 4/12, 2018 at 7:25 Comment(0)
P
1

For Windows and Linux in Android Studio:

Ctrl + Alt + L

For Mac:

Option + Command + L

Purapurblind answered 22/3, 2017 at 12:22 Comment(0)
T
1

Ubuntu Linux: Ctrl-alt-L (suggested by other responses) is captured by the window manager and locks the screen. So you need to change it: file->settings->keymap->main menu->code->Reformat Code. I use a double tab since I don't want the tab to shift a selection right.

This formats the code as you've specified in your preferences. To handle your specific request (aligning equals statements) go to: (file->settings->editor->code style->(choose language)->Wrapping and braces->Group declarations->align variables in columns

These styles are language specific so this particular option might not exist in the language you chose, or may be named differently.

Turpin answered 7/6, 2019 at 15:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.