Commenting in android layout file - android

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.

Related

What is right and less error-prone approach that the community follows for the use of IDs in Android's Activity xml

<RelativeLayout ..>
...
...
<Space
android:id="#+id/space4"
android:layout_width="match_parent"
android:layout_height="5dp"
android:layout_below="#+id/numberOfPersons" />
<TextView
android:id="#+id/txtCostPerPerson"
android:labelFor="#+id/costPerPerson"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/space4"
android:layout_alignBottom="#+id/costPerPerson"
android:layout_alignTop="#+id/costPerPerson"
android:gravity="center_vertical"
android:text="#string/costPerPerson"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#id/costPerPerson"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_toEndOf="#+id/txtCostPerPerson"
android:layout_below="#+id/space4"
android:inputType="number" />
</RelativeLayout>
The above xml code snippet has TextView having attribute labelFor="#+id/costPerPerson" whose value is the id of the EditText that follows below.
I'm confused on the convention of weather I should use the way it is now, or should the #+id be used in EditText and it is referenced other way here, if so what is it? What is right and less error-prone approach that the community follows? Thanks in advance!
If you are using an IDE and its graphical layout editor, you're probably letting it handle these things.
In cases where you are doing this work more by hand, the long-standing guidance has been to put the + on the first occurrence of the ID, top-down, in the layout file. In your case, costPerPerson appears first in android:labelFor of the txtCostPerPerson TextView, and so your code is following this convention.

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

Commenting in activity_main.xml

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

Code changes in Android's layout XML

I have a problem where my code automatically changes while I'm clicking or moving the cursor somewhere. This happens in a layout XML.
I use Eclipse Juno and I inserted an ExpandableList. My min sdk is 14 and max is 16.
It seems though that only the .xml part changes and not the layout. The .xml part, if opened again, is ok, but it does the same thing.
Helpz?
EDIT
I also noticed that the expandable list does not show up when I run the code(it just calls the layout)
The XML file:
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
<ExpandableListView
android:id="#+id/expandableListView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/textView1" >
</ExpandableListView>
Do you mean the code visually changes to other lines of code already in your XML file? Eclipse does this to me every now and then. Not sure why (I'm guessing it might be a bug with Lint) but usually closing and re-opening Eclipse fixes it for me.
From what I remember the code actually isn't changing, just what's being displayed.
try to align the code using ctrl + shift + f, and if the problem stod, reopen the xml file, and at last try to close eclipse and open it again.

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