How to format code in Android Studio (IntelliJ IDEA)
Asked Answered
I

6

49

I want to format code in a freshly created Android application.

I messed up the code in this manner:

 private static final
    int AUTO_HIDE_DELAY_MILLIS
            = 3000;

I want to format it with Ctrl + Alt + L, but it is not repairing the code. I use Windows 8.1.

Iulus answered 14/3, 2014 at 16:58 Comment(4)
When you press ctrl+alt+l do you get a reformat prompt? just to make sure that something else is not reacting to this shortcut. sometimes it helps to highlight the code first.Hysterogenic
ctrl+alt+l works fine with me. I'm currently using windows 8.1 too.Ananias
Ctrl+Alt+L or Ctrl+Shift+Alt+L for dialogStier
But Android Studio is not the same as IntelliJ IDEA(?). Two different IDEs(?). Can you elaborate?Lighthouse
Y
54

Just right-click on the project or directory, and select Reformat Code Reformat Code

Yokefellow answered 10/2, 2015 at 16:10 Comment(2)
A nice way to do it for multiple files at once, thanks.Tildie
make sure you check rearrange entries if you want same results as "Order/FormatCode" i.e. ctrl+alt+LAphotic
A
42

If you want to:

  1. Order/FormatCode: Ctrl + Alt + L
  2. Implement Methods: Ctrl + I
  3. Override Methods: Ctrl + O

There isn't any need for them to be capital letters. This is just for the example.

Array answered 11/7, 2014 at 19:33 Comment(1)
its not working for android studio 2.2. Please update answerBlurt
L
38

First you will need to configure how your code is formatted. This can be done by going to

[Main Menu] -> File -> Settings
then go to
(Project Settings) -> Code Style -> Java

In this area you can define all the formatting rules that you would like to apply to your code.

Now you should check (or change) the keyboard shortcut for reformatting code by going to

[Main Menu] -> File -> Settings
then go to
(IDE Settings) -> Keymap

Just type Reformat Code into the search box at the top right to find it.

Once this is done, highlight the code you want to reformat and use the keyboard shortcut to reformat your code based on the rules you defined.

Logician answered 15/3, 2014 at 16:14 Comment(3)
For some reason it still doesnt work for me. I want to use the automatic formatting to remove the double spaced lines in the code snippets i copy to the Editor. The code formatting refuses to do that for me.Garnet
Somewhere is a setting that is something along the lines of "format after pasting" (I'm unsure of the exact wording right now) which should be enabled. If that still does not work, then perhaps your rules aren't setup correctly to remove double spaced lines - I'm not sure that there are any rules to remove single line spaces (only two empty lines).Logician
@Logician Is there any way to do this with bash script? Reformat code from a bash script? Or run a shortcut command from bash script?Admissive
H
12

In Android Studio (on the Mac), I select the code chunk, and then select the Code menu item and click Auto-Indent Lines.

See screenshot:

Enter image description here

Shortcut key (on Mac) is Ctrl + Alt + I.

Hereinafter answered 20/1, 2015 at 1:8 Comment(0)
I
6

At least on my Android Studio version, "keep when formatting - line breaks" was enabled by default. So you need to go to

PreferencesEditorCode StyleJava and disable that checkbox (see the below screenshot).

Android Studio Preferences

Ironwood answered 12/5, 2016 at 11:43 Comment(1)
Where is "Preferences"? Is it a menu?Lighthouse
T
2

Alt + Ctrl + Shift + L is the shortcut key for arranging the code in Ubuntu (Android Studio).

Tilda answered 2/8, 2016 at 6:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.