12

In the following XML, what is the alternative way to place a comment over the attributes of element?

  <!-- I can comment here -->

  <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textView_ben"

        <!-- Cannot place a comment here  -->

        android:layout_centerHorizontal="true"/>
user103214
  • 3,478
  • 6
  • 26
  • 37
  • 1
    See http://stackoverflow.com/questions/26461869/how-to-comment-attributes-inside-xml-tag – DAB Jul 30 '16 at 13:35

2 Answers2

16

No, this isn't possible. Comments are not allowed in an XML open tag.

See How do I comment attributes inside an XML tag?

DAB
  • 1,631
  • 19
  • 25
1

A comment can only appear before or after the start tag (regardless of whether it is an empty tag).

A comment may not appear within the start-tag of an XML element:

[40] STag ::= '<' Name (S Attribute)* S? '>'

Nor may a comment appear within an end-tag:

[44] EmptyElemTag ::= '<' Name (S Attribute)* S? '/>'

In general, per section 2.5 Comments:

[Definition: Comments may appear anywhere in a document outside other markup; in addition, they may appear within the document type declaration at places allowed by the grammar. ...]


Here is one way to document element attributes:

  <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textView_ben"
        android:layout_centerHorizontal="true"/>
  <!--  
        android:id="@+id/textView_ben"
            addresses defect #123
        android:layout_centerHorizontal="true"
            blah blah blah -->

Alternatively, such a comment may appear before the tag; it just cannot appear within a tag.

kjhughes
  • 106,133
  • 27
  • 181
  • 240