yes, I looked it up!
<!-- comment -->
seems to be the right choice,
but I get an error in android-studio
Gradle: Error parsing XML: not well-formed (invalid token)
when I do this
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
<!-- android:background="?android:attr/selectableItemBackground" -->
android:id="#+id/imageButton"
android:layout_alignParentTop="true"
android:src="#drawable/gfx_select_medium"
android:layout_marginRight="22dp"/>
any help is appreciated, thx!
XML comments cannot be placed inside tag markup. Move the comment for example above or below your ImageButton tag.
Here's the spec reference, with emphasis added:
Comments may appear anywhere in a document outside other markup
Where markup is defined as:
Markup takes the form of start-tags, end-tags, empty-element tags, entity references, character references, comments, CDATA section delimiters, document type declarations, processing instructions, XML declarations, text declarations, and any white space that is at the top level of the document entity (that is, outside the document element and not inside any other markup)
Its not just AS. You need to place your comments outside of your end tag </> so do something like
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageButton"
android:layout_alignParentTop="true"
android:src="#drawable/gfx_select_medium"
android:layout_marginRight="22dp"/>
<!-- android:background="?android:attr/selectableItemBackground" -->
See this link and this one from W3 about xml comments. In a nutshell, it says
[Definition: Comments may appear anywhere in a document outside other markup;
notice
outside other markup
from the first link and
[Definition: Markup takes the form of start-tags, end-tags, ...
from the second link
You should know that comments within an xml file are considered nodes of XmlComment type, so if you load the xml file those nodes are going to get loaded and it is up to you to avoid them or filter them when parsing the loaded content.
So, This will be the correct format,
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageButton"
android:layout_alignParentTop="true"
android:src="#drawable/gfx_select_medium"
android:layout_marginRight="22dp"/>
<!-- android:background="?android:attr/selectableItemBackground" -->
Or you can see this link..
Related
Just wanted to ask a quick question. Here is an example XML tag. Which is the proper way to end it and what are the differences between both?
<TextView **code** />
<TextView> **code** </TextView>
XML in general
Attributes may appear <TextView here /> or <TextView here >...</TextView>— typically used for scalar values.
Elements (or text) may appear <TextView> here </TextView>— typically used for values with substructure.
For empty elements, <TextView/> and <TextView></TextView> are equivalent.
Android Layout XML
Android XML uses both elements and attributes and generally follows the common guidance that elements be used when further substructure is required and attributes be used for scalar values. See the documentation for details.
See also
XML Element vs XML Attribute
Both are acceptable. The one you use depends on what you are doing. For example, a <LinearLayout> tag will contain other tags nested inside, so you use the 2nd version. For example, if you include two TextViews inside, it looks like this:
<LinearLayout>
<TextView />
<TextView />
</LinearLayout>
On the other hand, a <TextView> never contains other tags inside it, but will have attributes:
<TextView
android:id="#+id/text_view_id"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="#string/hello" />
As you work more with Android's XML layouts, you will start to see patterns and get the hang of which of these you will use in a given situation.
So XML is a markup language for android. But I don't understand why the code is written in opening tag of the element but not in between the element opening and closing tags just like HTML.
I am a totally noob with XML, help would be much appreciated. Thanks.
For example this is normal XML, where code is written between tags.
<?xml version="1.0" encoding="UTF-8"?>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
And this is XML for android where code written in the tag.
<TextView android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is a TextView" />
Why its different.
So XML is a markup language for android
XML is a general-purpose markup language that Android happens to use. XML existed before Android did.
But I don't understand why the code is written in opening tag of the element but not in between the element opening and closing tags just like HTML.
HTML and XML both use nested elements (or "tags" in HTML terms) with attributes. Android layout resources use nested elements with attributes.
For example this is normal XML, where code is written between tags.
That is not "normal" XML. That is simply XML. There is no requirement for any XML file to look like that, and there is no requirement for that data to be represented in XML that way. Here is another representation of that data in XML:
<note to="Tove" from="Jani" heading="Reminder" body="Don't forget me this weekend!" />
Here is another:
<note to="Tove" from="Jani">
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
Here is one more:
<note>
<to>Tove<from>Jani<heading>Reminder<body>Don't forget me this weekend!</body></heading></from></to>
</note>
All of those are perfectly valid XML.
And this is XML for android where code written in the tag
In Android layout resources, elements (mostly) represent View and ViewGroup subclasses, with attributes defining what those widgets and containers look like. The developers who created Android chose this particular XML representation for the data. They could have done something else.
In XML, we can put some attributes for an element in it's tag.
For example, if the note from your example was a "top-secret" note, the top-secret could be "in" the tag as an attribute (It is up to you to decide whether the "top-secret" is an attribute for the note or an inner element of it):
<note top-secret="true">
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
The TextView is an element with no inner elements (id, width and ... are decided to be the attributes of the TextView). When XML tags don't have any inner elements, we can merge the closing tag with the opening tag by putting a slash before the '>' character. So the TextView is an element with it's closing tag merged with its opening tag. It can be written as follows:
<TextView android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is a TextView"></TextView>
In Android, view containers like ScrollView, have inner elements:
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is a TextView"/>
</ScrollView>
I get the warning, "This text field does not specify an inputType or a hint" When I modify a copy of a tutorial code (below)
<EditText android:id="#+id/edit_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="#string/edit_message"
android:layout_weight="1" />
This works fine, and the warning only comes up if a new blank line is created
I've modified it for a friend with several comments lines explaining what each part of it does however, whenever i add in an extra line in the above (Even a blank line, in this case it's a comment line) I receive the above error
<!--edit text creates a text input box-->
<EditText android:id="#+id/edit_message"
<!-- edit_message is a variable, defined in strings.xml-->
<!-- determines the width of the textField, in this case 0dp means "however long the text is" IE: it will grow to fit however many characters the user types -->
android:layout_width="0dp"
<!-- determines the height of the Text Field -->
android:layout_height="wrap_content"
<!-- The hint is the default value for the text field, it calls on the variable edit_message defined in strings.xml-->
android:hint="#string/edit_message"
<!-- Weight means how much the screen the text field can take up, in this case, the ratio is 1:1 so it can take up however much room is needed, However if a different variable also had the weight of 1, the ratio would be 1:2 and each could take up half the screen -->
android:layout_weight="1" />
Without the comments, the warning is not there
The Reason for this warning is that you haven't set the inputType for this editText. Add the following line:
android:inputType="text"
So it should look like this:
<EditText android:id="#+id/edit_message"
android:inputType="text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="#string/edit_message"
android:layout_weight="1" />
And the warning is gone.
Logically you should get warning in the line
<EditText android:id="#+id/edit_message"
and an error in the next line.
Reason:
When you are placing comment tags, then the closing tag of comment is considered as the illegal closing tag for EditText. So you should even get the following error
Element type "EditText" must be followed by either attribute specifications, ">" or "/>".
and because of the above error the remaining code is not executed and thus you get a warning
This text field does not specify an inputType or a hint
even though android:hint attribute exists in your code.
I just run your code and got my app running properly, when i added the comment it crashes, then i realized that you should not comment inside your XML , it is a principle of XML.
I suggest you to read this article explaning what is a well formed XML and how to comment xml in the right way http://webdesign.about.com/cs/xmlinformation/ht/htcommentxml.htm
Also here has been a discussion about this particularly subject and has been solved as well.
https://stackoverflow.com/a/2073162/2069737
Hope it helps you.
I am new to Android. I understand that commenting in XML works the same as it does in HTML, using the
<!-- comment here -->
form. I would like to write some comments in my activity_main.xml configuration file for an Android project, but it's giving me errors. It's worth noting that I'm using Eclipse, but for the moment, I'm editing the XML file directly as opposed to graphically because I'd rather force myself to understand the attributes. I am trying to comment in and out conflicting attributes but it's giving me red.
Is there a way for Eclipse to allow me to comment that particular XML file?
For those that asked, here's an example:
<EditText android:id="#+id/edit_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="#string/edit_message"
android:layout_weight="1" />
Say I'd like to simply comment out that second line. I want to comment out the layout_width tag because I'm later using layout_weight. When I try to comment it out, I get:
Element type "EditText" must be followed by either attribute specifications, ">" or "/>".
One person responded that a comment can't break up a tag, which is what I intended to do. I thought I had done that before in HTML, so I assumed that XML abided by the same rules. I guess not, or maybe I just need to brush up on my XML.
The problem with commenting attributes on xml is that you can't have a comment break the xml tag. So if you have:
<View
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
You can't comment layout_width, your comment must be outside of the view tag.
There are two things forbidden in terms of commenting in XML:
double -- inside comments
comments inside tags
So this code would generate an error.
<EditText android:id="#+id/edit_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="#string/edit_message"
<!-- That would use all empty space -->
android:layout_weight="1" />
And this too:
<!-- This is for -- your name -->
<EditText android:id="#+id/edit_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="#string/edit_message"
android:layout_weight="1" />
You could read more about comments in Android: http://android4beginners.com/2013/07/appendix-d-comments-in-xml-and-java-its-crucial-to-create-a-documentation-of-android-app/
It is giving you the error because you are closing your EditText tag in the second line itself.
That's because for commenting out some code, you'd use <!-- -->, so ">" has been taken. and from next line onward, it's not having the starting "<".
For example,
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
If you comment out the second line, i.e. layout_width, it will be considered as though you were closing the TextView tag, and next line onward it would not have any starting "<".
I would like to enter some comments into the layout XML files, how would I do that?
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" />
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 "/>".
XML comments start with <!-- and end with -->.
For example:
<!-- This is a comment. -->
There are two ways you can do that
Start Your comment with "<!--" then end your comment with "-->"
Example <!-- my comment goes here -->
Highlight the part you want to comment and press CTRL + SHIFT + /
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"/>-->
<!-- comment here -->
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" />
click the
ctrl+shift+/ on windows
command + control+/ on Mac
and write anything you and evrything will be in comments
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.
you can also add comment by pressing Ctrl+shift+/ and shift+ / for one line.
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
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>