Yoda
Yoda

Reputation: 18068

How to format code in Android Studio (IntelliJ IDEA)

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.

Upvotes: 49

Views: 87158

Answers (6)

mahesh chandran
mahesh chandran

Reputation: 21

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

Upvotes: 2

bairprogramming
bairprogramming

Reputation: 86

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

Upvotes: 6

Someone Somewhere
Someone Somewhere

Reputation: 23787

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.

Upvotes: 12

Victor Ruiz.
Victor Ruiz.

Reputation: 1722

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.

Upvotes: 43

IgorGanapolsky
IgorGanapolsky

Reputation: 26814

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

Upvotes: 54

free3dom
free3dom

Reputation: 18978

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.

Upvotes: 38

Related Questions