Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comments in Android Layout xml

Tags:

android

xml

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

like image 379
user412317 Avatar asked Oct 12 '22 09:10

user412317


People also ask

How do you put comments in XML?

Syntax. A comment starts with <! -- and ends with -->. You can add textual notes as comments between the characters.

How do I comment in Androidmanifest XML?

ctrl+shift+/ You can comment the code.

Does XML allow commenting elements?

Allows notes and other human readable comments to be included within an XML file. XML Parsers should ignore XML comments. Some constrains govern where comments may be placed with an XML file.

What are comments in Android?

This interface inherits from CharacterData and represents the content of a comment, i.e., all the characters between the starting ' <! -- ' and ending ' --> '. Note that this is the definition of a comment in XML, and, in practice, HTML, although some HTML tools may implement the full SGML comment structure.


2 Answers

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" />
like image 270
Federico klez Culloca Avatar answered Oct 21 '22 06:10

Federico klez Culloca


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 "/>".
like image 44
Aniket Thakur Avatar answered Oct 21 '22 05:10

Aniket Thakur