Comments in Android Layout xml

AndroidXml

Android Problem Overview


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

Android Solutions


Solution 1 - Android

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" />

Solution 2 - Android

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 "/>".

Solution 3 - Android

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

For example:

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

Solution 4 - Android

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 + /

Solution 5 - Android

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"/>-->

Solution 6 - Android

<!-- comment here -->

Solution 7 - Android

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" />

Solution 8 - Android

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.

Solution 9 - Android

click the

> ctrl+shift+/

and write anything you and evrything will be in comments

Solution 10 - Android

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

Solution 11 - Android

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

Solution 12 - Android

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>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser412317View Question on Stackoverflow
Solution 1 - AndroidFederico klez CullocaView Answer on Stackoverflow
Solution 2 - AndroidAniket ThakurView Answer on Stackoverflow
Solution 3 - AndroidDan DyerView Answer on Stackoverflow
Solution 4 - AndroideliView Answer on Stackoverflow
Solution 5 - AndroidYogesh SarvaiyaView Answer on Stackoverflow
Solution 6 - AndroidWesley WiserView Answer on Stackoverflow
Solution 7 - AndroidCJBSView Answer on Stackoverflow
Solution 8 - AndroidNabin KhatiwadaView Answer on Stackoverflow
Solution 9 - Androidvenu46View Answer on Stackoverflow
Solution 10 - Androiduser8919158View Answer on Stackoverflow
Solution 11 - AndroidSweet LouView Answer on Stackoverflow
Solution 12 - AndroidStanley KoView Answer on Stackoverflow