Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 349
Daksharma Avatar asked Jan 29 '15 19:01

Daksharma


People also ask

How to align in Android Studio?

To center align text in TextView in Kotlin Android, set android:textAlignment attribute with the value “center” in layout file, or programmatically set the textAlignment property of the TextView object with View. TEXT_ALIGNMENT_CENTER in activity file.

How to set auto format in Android Studio?

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 ).

How to indent code in Android?

File-> Settings-> IDE Settings-> Keymap So if you are have the same problem just go to the Settings - Keyboard - Shortcuts - System and change the default shortcut for the "Lock screen". Show activity on this post. Shift + Tab to decrease the indent.


2 Answers

Indent code in Android Studio:


Windows

Ctrl + Alt + L


Mac:

Option + Command + L

like image 186
Jorgesys Avatar answered Sep 21 '22 23:09

Jorgesys


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.

like image 39
Daksharma Avatar answered Sep 20 '22 23:09

Daksharma