XML attributes order in Android Studio
Finally got it.
- Choose
File > Settings > Code Style > XML > Set from > Predefined Style > Android
- Set
File > Settings > Editor > Formatting > Show "Reformat Code" dialog
- Run formatting on a XML file (
CTRL+ALT+L
by default) - You will see a popup window, set the
Rearrange entries
flag - Disable
Reformat Code
dialog in the settings
This way every XML file formatting will set the attributes in a proper order.
Edit:
Starting with Android Studio 0.2.6 release the XML Android style formatting is set by default, but you still have to set the Rearrange Entries
flag manually.
http://tools.android.com/recent/androidstudio026released
Automatically apply the Android XML code style if a code style hasn't already been customized. This will make it possible to automatically order XML attributes (check the "Rearrange Entries" checkbox in the "Code > Reformat Code..." dialog.)
UPDATE: the method I described below is an official way to fix this known issue, see: https://developer.android.com/studio/releases
After updating to the newest Android Studio v3.4.2
I found that it doesn't format XML code properly anymore. For instance, attributes ordering didn't work in layouts
. It did indentation and namespaces ordering only.
I don't know the reason why it was broken after an update but was lucky to fix this:
- Go to
Settings
(CMD
+
,
) on Mac. - Type
format
in Search box and click onEditor -> XML
setting. - Click on the
Android
tab and make the same settings here as it's on a picture:
Settings on a picture quite normal, but of course you can tune this tab setting up to you.
- Go to the
Arrangement
tab and you will see something like this:
The reason why attributes are not getting sorted - sorting rules are empty.
To fix this:
- Choose
Scheme
and play withDefault IDE
andProject
options. Switching between them may help. - Or choose
Scheme
which suites your needs and then click onSet from...
blue text in right upper corner-> Predefined Style -> Android
. You will see restored rules in the window:
- Choose
I also chose
Force rearrange - Always
To save result – click Apply
.
Also you can click on a gear icon near Scheme
and copy settings or even restore defaults.
P.S. To me standard rules are comfortable, but it can be customized further. Here is an article which may help doing this: https://medium.com/@VeraKern/formatting-xml-layout-files-for-android-47aec62722fc
Go to your layout XML
Try Ctrl + alt + shift + L then find checkbox label is Rearrange code and checked its.