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>
An <attr> element has two xml attributes name and format . name lets you call it something and this is how you end up referring to it in code, e.g., R. attr.
eXtensible Markup Language, or XML: A markup language created as a standard way to encode data in internet-based applications. Android applications use XML to create layout files. Unlike HTML, XML is case-sensitive, requires each tag be closed, and preserves whitespace.
Android Studio supports a variety of XML attributes in the tools namespace that enable design-time features (such as which layout to show in a fragment) or compile-time behaviors (such as which shrinking mode to apply to your XML resources).
Finally got it.
File > Settings > Code Style > XML > Set from > Predefined Style > Android
File > Settings > Editor > Formatting > Show "Reformat Code" dialog
CTRL+ALT+L
by default)Rearrange entries
flagReformat Code
dialog in the settingsThis 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.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With