Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 649
Yoda Avatar asked Mar 14 '14 16:03

Yoda


People also ask

How do I format code in IntelliJ?

In the editor, select a code fragment you want to reformat. Before reformatting, you can take a look at the code style settings that are applied to the selected code: press Alt+Enter and click Adjust code style settings. From the main menu, select Code | Reformat Code or press Ctrl+Alt+L .

How do I change the format code in Android Studio?

Navigate to the file in which we have to format our code and then click on the shortcut key as Ctrl+Shift+Alt+L after clicking this key you will get to see the below dialog on your screen. Select the formatting options and your code will be formatted and rearranged according to selected options.

How do I organize my code in IntelliJ?

Rearrange codePress Ctrl+Alt+S to open the IDE settings and select Editor | Code Style. In the editor, select the code entries you want to rearrange and from the main menu, select Code | Rearrange Code.

How can I format Android studio?

For formatting code in Android Studio on Linux you could instead use Ctrl + Alt + Super + L . You could use this and avoid having to change the system shortcut.


3 Answers

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

like image 109
IgorGanapolsky Avatar answered Oct 13 '22 19:10

IgorGanapolsky


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.

like image 42
Victor Ruiz. Avatar answered Oct 13 '22 18:10

Victor Ruiz.


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.

like image 38
free3dom Avatar answered Oct 13 '22 20:10

free3dom