Android studio code formatting

Auto format code in Android Studio/Intellij IDEA

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.

Источник

Different Ways to Format Code in Android Studio

Code formatting is very important when you are building an Android application. This will help you to organize your code properly and to maintain your code to make it easily readable. Below is a list of purposes of having a standard coding format.

Читайте также:  Как установить supersu для андроид

Purposes of Having Coding Standards

  • A coding standard gives a uniform appearance to the codes written by different engineers.
  • It improves the readability, and maintainability of the code, and it reduces complexity also.
  • It helps in code reuse and helps to detect errors easily.
  • It promotes sound programming practices and increases the efficiency of the programmers.

In this article, we will take a look at different ways to format the code in Android Studio. We are going to discuss three different methods with the help of which we can format the code in our Android Studio.

  • Method 1: By using a shortcut key for formatting a code in any file in Android Studio
  • Method 2: Formatting a code from MenuBar
  • Method 3: By using different shortcut keys for formatting our code

Method 1: By using a shortcut key for formatting a code in any file in Android Studio

In this method, we simply have to navigate to the file in which we have to format our code. After opening the file we have to simply press the shortcut key as Ctrl+Alt+L to reformat the code in your file. With this key, the code in your file will be rearranged according to the code standards. You can get to see below image 1 in which our code is not formatted properly and after pressing this shortcut key our code will be formatted as shown in the second image.

Unformatted code which we have written.

Formatted code which we get after pressing Ctrl+Alt+L

Shortcut keys for different Operating Systems

Shortcut key

Windows Ctrl+Alt+L
Mac OS Option+Cmd+L
Linux Ctrl+Shift+Alt+L

Method 2: Formatting a code from MenuBar

In this method, we will rearrange our code from the Menu Bar. To format our code navigate to the file in which we have to format our code and inside that file. Navigate to the top bar and click on the Code option and inside that option click on Reformat code option which is shown in the below screenshot.

After clicking on this option your code will be formatted as shown in the above screenshot

Note: This method will be similar for all the operating systems

Method 3: By using different shortcut keys for formatting our code

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. The options present inside this dialog box is explained below.

After selecting the option on this dialog make sure to run it to format your code

Источник

How to create my own ‘Reformat Code’ style in Android Studio?

In Eclipse,(Window -> Preferences -> Java -> Code Style -> Formatter) there was a facility to create my own format style and apply it in the code. Is there a way to do that in Android Studio(for Java code) ?

3 Answers 3

To change formatter options go to File | Settings | Code Style | Java

To reformat your code manually, press Ctrl + Alt + L (or using the menu Code | Reformat code. ). To reformat the whole source code of your module, just select the module folder first.

More information about reformating source code can be found on the IntelliJ Web Help

In Android Studio 3.3.2 (running in Mac OS) «Android Studio» -> «Preferences» -> «Editor» -> «Code Style» -> «Java».

I would be useful to find a way to export/import only code format settings in order to share them between the team.

Open global settings/preferences via ⌘, or Android Studio > Prefences.

In the left hand side bar, go to Editor > Code Style.

open the scheme: drop down menu and select Default IDE .

Click the cog menu to the right of the drop down menu and select duplicate. .

Then rename your new profile, and change the general settings as well as the settings for each language in the sub menu of Code Style as you see fit.

Then with you new profile selected, reformat your code with ⌘ + ⌥ + L

These style are global and can be used in other projects.

Источник

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:

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

6 Answers 6

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

  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.

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.

Источник

How do I align/format code in Android Studio?

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.

6 Answers 6

Indent code in Android Studio:

Windows

Mac:

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

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

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.

Ctrl + Shift + Alt + L

⌥ option + ⌘ command + L

For Windows and Linux in Android Studio:

For Mac:

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.

Источник

Читайте также:  Android viewpager отключить прокрутку
Оцените статью