167

I would like to enter some comments into the layout XML files, how would I do that?

Barskey
  • 423
  • 2
  • 5
  • 18
user412317
  • 1,767
  • 2
  • 12
  • 9

12 Answers12

282

As other said, the comment in XML are like this

<!-- this is a comment -->

Notice that they can span on multiple lines

<!--
    This is a comment
    on multiple lines
-->

But they cannot be nested

<!-- This <!-- is a comment --> This is not -->

Also you cannot use them inside tags

<EditText <!--This is not valid--> android:layout_width="fill_parent" />
Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95
  • 3
    Also you cannot have a double dash within a comment or the XML parser will complain – Martin Belcher - AtWrk Oct 24 '11 at 17:25
  • If you are using Eclipse, you can open the XML file, place the cursor where you want the comment, the choose from the top menu Source -> Add Block Comment. Also, "ctrl + shft + /" (that is, hold control and the shift key then press the forward slash key). The comment code will be created with your cursor in the middle, so you can just start typing. – LeBeau Mar 30 '14 at 06:21
  • 12
    > Also you cannot use them inside tags. Quite unfortunate really. – linuxjava Sep 03 '15 at 15:39
44

The World Wide Web Consortium (W3C) actually defined a comment interface. The definition says all the characters between the starting ' <!--' and ending '-->' form a part of comment content and no lexical check is done on the content of a comment.

More details are available on developer.android.com site.

So you can simply add your comment in between any starting and ending tag. In Eclipse IDE simply typing <!-- would auto complete the comment for you. You can then add your comment text in between.

For example:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    tools:context=".TicTacToe" >

 <!-- This is a comment -->

</LinearLayout>

Purpose of specifically mentioning in between is because you cannot use it inside a tag.

For example:

<TextView 
    android:text="@string/game_title"
    <!-- This is a comment -->
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"/>

is wrong and will give following error

 Element type "TextView" must be followed by either attribute specifications, ">" or "/>".
Kurt Wagner
  • 3,295
  • 13
  • 44
  • 71
Aniket Thakur
  • 66,731
  • 38
  • 279
  • 289
  • 2
    Note: No comments inside tags. This should be the selected answer – Eslam Sameh Ahmed Feb 14 '15 at 18:02
  • 1
    Submited an enhachment to Android Studio team. If I use data binding and want to comment a line in XML where I wrote some data binding logic, I have to comment somewhere else and it doesnt help with visibility or to what part does the comment refer to. This is not something that should be impossible to do, and should be enabled for us (devs) to use. – Chapz Mar 03 '17 at 10:08
19

XML comments start with <!-- and end with -->.

For example:

<!-- This is a comment. -->
Dan Dyer
  • 53,737
  • 19
  • 129
  • 165
14

There are two ways you can do that

  1. Start Your comment with "<!--" then end your comment with "-->"

    Example <!-- my comment goes here -->

  2. Highlight the part you want to comment and press CTRL + SHIFT + /

eli
  • 8,571
  • 4
  • 30
  • 40
14

ctrl+shift+/ You can comment the code.

<!--    
     <View
          android:layout_marginTop="@dimen/d10dp"
          android:id="@+id/view1"
          android:layout_below="@+id/tv_change_password"
          android:layout_width="fill_parent"
          android:layout_height="1dp"
          android:background="#c0c0c0"/>-->
Yogesh Sarvaiya
  • 551
  • 6
  • 16
7
<!-- comment here -->
adamk
  • 45,184
  • 7
  • 50
  • 57
Wesley Wiser
  • 9,491
  • 4
  • 50
  • 69
5

Comments INSIDE tags possible

It's possible to create custom attributes that can be used for commenting/documentation purposes.

In the example below, a documentation:info attribute is defined, with an example comment value:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:documentation="documentation.mycompany.com"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/relLayoutID"
    documentation:info="This is an example comment" >

    <TextView
        documentation:purpose="Instructions label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Click here to begin."
        android:id="@+id/tvMyLabel"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"
        documentation:info="Another example comment"
        documentation:translation_notes="This control should use the fewest characters possible, as space is limited"
        />

</RelativeLayout>

Note that in this case, documentation.mycompany.com is just a definition for the new custom XML namespace (of documentation), and is thus just a unique URI string - it can be anything as long as it's unique. The documentation to the right of the xmlns: can also be anything - this works the same way that the android: XML namespace is defined and used.

Using this format, any number of attributes can be created, such as documentation:info, documentation:translation_notes etc., along with a description value, the format being the same as any XML attribute.

In summary:

  • Add a xmls:my_new_namespace attribute to the root (top-level) XML element in the XML layout file. Set its value to a unique string
  • Under any child XML element within the file, use the new namespace, and any word following to define comment tags that are ignored when compiled, e.g. <TextView my_new_namespace:my_new_doc_property="description" />
CJBS
  • 15,147
  • 6
  • 86
  • 135
  • 2
    Note that these attributes will not be discarded during the build process but will instead be stored inside the resulting APK. Consider using the special `tools:` namespace instead, which does get discarded. (It probably didn't exist when this answer was posted, but this page continues to get new viewers.) – j__m Jan 04 '19 at 01:48
  • @j__m This is a good point. I haven't looked into whether ProGuard can remove this either automatically, or with a bit of configuration... – CJBS Jan 04 '19 at 19:09
5

click the

ctrl+shift+/ on windows

command + control+/ on Mac

and write anything you and evrything will be in comments

venu46
  • 419
  • 5
  • 10
4

If you want to comment in Android Studio simply press:

Ctrl + / on Windows/Linux

Cmd + / on Mac.

This works in XML files such as strings.xml as well as in code files like MainActivity.java.

David Rawson
  • 20,912
  • 7
  • 88
  • 124
Nabin Khatiwada
  • 478
  • 5
  • 15
0

you can also add comment by pressing Ctrl+shift+/ and shift+ / for one line.

-2

Unbelievably, in 2019 with Android studio 3.3 (I don't know exact version, at least 3.3), it is possible to use double slash comment to xml.

But if you use double slash comment in xml, IDE shows warning.

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    // this works

    /* this works too */

    /*
    multi line comment
    multi line comment
    */

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World! yeah"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>
Stanley Ko
  • 3,383
  • 3
  • 34
  • 60
-2

From Federico Culloca's note:

Also you cannot use them inside tags

Means; you have to put the comment at the top or bottom of the file - all the places you really want to add comments are at least inside the top level layout tag

Niki van Stein
  • 10,564
  • 3
  • 29
  • 62
  • 9
    It does not mean this. You can perfectly put a comment somewhere in the middle of the file. It just needs to be between other tags. – Alex Che Dec 07 '11 at 05:28
  • More specifically, they must be in this order: closing tag of element n, comment, opening tag of element n+1. – ban-geoengineering Jun 14 '16 at 11:02