XML attributes order in Android Studio
Asked Answered
B

6

89

I'm facing problem with keeping proper order of XML attributes in Android Studio. As you can see below, the style attribute is between layout_* attributes, but I want it to be ordered by name (like in Eclipse). I'm using standard Intellij code formatter and the Android Studio gives users ability to set your own rules regarding XML ordering. The settings are located in Code Style -> XML -> Arrangement, but it seems not to work or I'm using it wrong. Any ideas how to order XML attributes by name using default code formatter?

<TestView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    style="@style/BackgroundLight"
    android:layout_height="wrap_content">

</TestView>
Bluestone answered 13/8, 2013 at 9:56 Comment(1)
Note that unless there is a bug in the layout preview in Android Studio, then for ConstraintLayouts, the layout is dependent on the order of attributes. For instance if I put layout_height and layout_width after constraintX_toYof, then I get a different result than if I don't.Resistance
B
199

Finally got it.

  1. Choose File > Settings > Code Style > XML > Set from > Predefined Style > Android
  2. Set File > Settings > Editor > Formatting > Show "Reformat Code" dialog
  3. Run formatting on a XML file (CTRL+ALT+L by default)
  4. You will see a popup window, set the Rearrange entries flag
  5. 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.)

Bluestone answered 20/8, 2013 at 22:4 Comment(4)
It rearranges attributes, but it doesn't delete empty lines between themAuxochrome
for ubuntu its CTRL+Shift+ALT+LFibster
The keyboard shortcut may vary depending on platform and configuration. To check or change it go to Settings -> Keymap -> Reformat CodeRubeola
This answer is outdated, 2nd number changed. Use Ctrl + Alt + Shift + L.Mosier
B
29

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:

  1. Go to Settings (CMD + , ) on Mac.
  2. Type format in Search box and click on Editor -> XML setting.
  3. Click on the Android tab and make the same settings here as it's on a picture:

Android tab in XML formatting Setting Settings on a picture quite normal, but of course you can tune this tab setting up to you.

  1. Go to the Arrangement tab and you will see something like this:

Arrangement tab in XML formattings Setting The reason why attributes are not getting sorted - sorting rules are empty.

  1. To fix this:

    1. Choose Scheme and play with Default IDE and Project options. Switching between them may help.
    2. Or choose Scheme which suites your needs and then click on Set from... blue text in right upper corner -> Predefined Style -> Android. You will see restored rules in the window:

Finally you will see restored rules here 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

Bridgettebridgewater answered 12/8, 2019 at 16:7 Comment(0)
C
10

Go to your layout XML
Try Ctrl + alt + shift + L then find checkbox label is Rearrange code and checked its.

Cootch answered 12/12, 2019 at 3:45 Comment(0)
S
5

To delete empty lines between them:

Preferences > Code Style > XML > Other > Keep blank lines: 0

And then just reformat your XML files.

Scratch answered 3/2, 2015 at 2:53 Comment(2)
works for me very good. There are blank lines between widgets but not inside widget code.Epistasis
How to format xml files in android studio? ctrl+alt+L doesn't work for me in macCrate
F
0

There is a plugin for IntelliJ which might do what you're after...

http://plugins.jetbrains.com/plugin/?idea&pluginId=6546

Forby answered 20/8, 2013 at 0:19 Comment(1)
I'm using this plugin to format Java source code but it doesn't format XML files.Bluestone
S
0

File > Settings > Editor > XML > Arrangement Select Scheme and choose Default

Sump answered 20/5, 2019 at 16:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.