Commenting in activity_main.xml - android

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

Related

the markup in the document following the root element must be well formed,error parsing xml not well formed (invalid token)

when i try to run my app it keeps giving me the same message above
<EditText android:id="#+id/edit_message"
android:layout_weight="1"
android:layout_width=`"0dp"`
android:layout_height="wrap_content"
android:hint="#string/edit_message"/>
I tried putting android:id under but it still gave me the same message.
im making this app for a competition, please repond as soon as you can its in 5 days.
the `s is the stackoverflow code thing
I just copy pasted your code and the error is where you have the line android:layout_width="0dp" - you have single quotes here surrounding the value. Take them out.
Remove the `s (single quotes) in this line:
android:layout_width=`"0dp"`
It should be
android:layout_width="0dp"
If this is the only element in your layout file then android name space is missing. try following:
<EditText xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/edit_message"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="#string/edit_message"/>
And as other people are pointing, remove apostrophes surrounding the weight value.

Element type "Button" must be followed by either attribute specifications, ">" or "/>"

I am creating a button in my XML and here is the creation parameters
<Button android:id="#+id/btn2"
---> android:layout_width="wrap_content" <----
android:layout_height="wrap_content"
android:text="#string/PNR"
/>
I am getting and error in the line indicated saying :
" Element type "Button" must be followed by either attribute specifications, ">" or "/>" "
Not only in button id I try to create TextView or so then also same error comes and at same place.
I have checked earlier posts but they said that the tags were not closed and did not worked for me.
Please suggest me, what to do? Here is the full code :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button android:id="#+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/PNR"
/>
</LinearLayout>
Try cleaning your project
Project --> Clean... then choose your project
Sometimes Eclipse doesn't pick up changes to your xml. When you get goofy errors like this always try cleaning first. Sometimes you will get ClassCastException in Java code when running right after changing something in xml like
cannot cast Button to EditText
or something similar that won't make sense. This is also a good time to clean your project.
I would also recommend getting rid of whitespace within elements because I have had trouble with that as well (especially on older versions of Eclipse) plus I think it looks cleaner. So I would change your <Button... to
<Button android:id="#+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/PNR"/> <!-- just moved your end tag to this line -->

This text field does not specify an inputType or a hint

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.

Commenting in android layout file

How can I comment in xml file of android layout. I have seen Comments in Android Layout xml - but I wanted to comment inside a parent.
When I try to do that I get an error in eclipse:
<Button android:text="Button"
<!-- this is a comment -->
android:id="#+id/Discon" >
</Button>
From the background knowledge of xml, I came to know that we cannot add comments in attributes.
Is there any way of removing the id as attribute and specifying it as element?
Or is there any way to add comments inside attributes of elements?
you cannot embed a comment inside a tag.
between tags it's not a problem
<!--
this is a comment
-->
<Button android:text="Button"
android:id="#+id/Discon" >
</Button>
if you want to temporary comment out an attribute (if that's what you want to know) you will have to move it ouside the tag and comment it there.
<Button android:text="Button"
android:id="#+id/Discon"
>
</Button>
==>
<!-- android:id="#+id/Discon" -->
<Button android:text="Button"
>
</Button>
Well not technically a comment, buy you can do
<Button comment="write whatever you want here" android:text="Button" ...
Or if you want to temporarily remove an attribute, remove the android: from the name.
Basically Android ignores everything without the android: namespace.
I use Android Studio, So i can tell u XML comment for A.S only.
Simply select the part of code you want to make it comment and
press (Ctrl+shift+?) It will add <1-- your Code -->, Here 1 is (Shift+1)( exclamation mark)
Remember one more thing , Here is one exception : Example
<TextView
android:id="#+id/T_HomeworkMax1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FF1E0101"
android:textAppearance="?android:textAppearanceSmall"
android:layout_marginStart="95dp"
android:text="Max" />
<TextView
android:id="#+id/T_HomeworkMin1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FF1E0101"
android:layout_marginStart="140dp"
android:textAppearance="?android:textAppearanceSmall"
android:text="Min" />
Here,You can not select just one or two lines from the code, You have to select full Node(TextView).
<!-- <TextView
android:id="#+id/T_HomeworkMax1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FF1E0101"
android:textAppearance="?android:textAppearanceSmall"
android:layout_marginStart="95dp"
android:text="Max" />-->
<TextView
android:id="#+id/T_HomeworkMin1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FF1E0101"
android:layout_marginStart="140dp"
android:textAppearance="?android:textAppearanceSmall"
android:text="Min" />
Now, First TextView is Comment.
Use Below Code for comment in Android Studio:
Comment / uncomment a block of code.
Ctrl + Shift + "/"
Comment / uncomment a line of code.
Ctrl + "/"
XML does not allow to comment between tags () which means you can't place comments to attributes as they'd be inside its tag.
You can't remove an attribute and place it outside its tag, because then it is no longer an attribute.
In short, what you're trying to do, in the way you try to do it, it simply cannot be done.

Comments in Android Layout xml

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>

Categories

Resources