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>
Related
I use latest Android Studio and SDK. In preview & real device i see this:
My code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.myappname.view.AboutActivity">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/listViewAbout" />
</RelativeLayout>
How i make subtitle text color is gray? Like this:
I'm going out on a limb and assume that you're using the row layout simple_list_item_2.xml (based on the screenshot) which gives you two rows. The problem, if you may call it that, is that depending on the SDK version, the styling for this layout has changed.
On SDK 23, it looks like this:
However, on say SDK 19, it looks like this:
Why?
To understand this we first need to take a look at the xml that generates the rows from simple_list_item_2.xml, you'll see it's a pretty simple layout that uses the now deprecated view TwoLineListItem but that's just a plus on why to use your custom layout.
<TwoLineListItem xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/listPreferredItemHeight"
android:mode="twoLine"
android:paddingStart="?attr/listPreferredItemPaddingStart"
android:paddingEnd="?attr/listPreferredItemPaddingEnd">
<TextView android:id="#id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:textAppearance="?attr/textAppearanceListItem" />
<TextView android:id="#id/text2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/text1"
android:layout_alignStart="#id/text1"
android:textAppearance="?attr/textAppearanceListItemSecondary" />
</TwoLineListItem>
The reason is because of the way the style textAppearanceListItemSecondary is resolved in each SDK version. The style is what gives the text the size, the color, etc. The evolution of the interface in Android has given birth to a huge ecosystem of themes and relying on the default styling will result in inconsistencies like the one you stumbled upon.
What to do about it?
You should use your own layout for this to allow for uniform styling across versions. To do so, please refer to any of the multiple questions covering this matter. But in short it just means creating a layout file, call it for example custom_row.xml and having the layout look exactly as you please. This also gives you total control over placement of the items, extra Views that you may need, and overhead in terms of coding is minimal compared to the SimpleAdapter or ArrayAdapter that perhaps you were using.
Note
You should consider moving your code towards RecyclerView instead of ListView if you haven't already.
You can set Textview property
android:textColor="#color/grey"
in you Adapter layout to change colour of your sub item
Hope this will help
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..
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 -->
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 "<".
Is there a way I can suppress individual warnings about hardcoded strings in layout files?
I often put placeholder text into TextViews so that I can see them in layout at design time. The downside of this is getting a ton of these warnings about hardcoded strings. But without them I wouldn't see the TextViews at all in the layout.
You can add the following to the text view element:
tools:ignore="HardcodedText"
Example:
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is a hardcoded text"
tools:ignore="HardcodedText" />
Note there is a shortcut in Eclipse for adding this easily: just press CTRL + 1 and select the relevant option.
Unfortunately, I couldn't find a way to do this for a whole layout, you will have to do it for each element.
Note that you must also add the xmlns:tools="http://schemas.android.com/tools attribute to the root element
The other way is to use tools:text instead of android:text:
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:text="This is a hardcoded text" />
Note that you must also add the xmlns:tools="http://schemas.android.com/tools attribute to the root element
In Eclipse, go to Window->Preferences->Android->Lint Error Checking.
Scroll down to and select Hardcoded Text (under Internationalization). On the Severity drop down box, select Ignore and click on Apply.
Use string.xml file to remove this warning....
You have to put your string in string.xml file and then give like android:text="#string/mytext"
And in res-->value->string.xml add <string name="mytext">Your Text</string>
http://tools.android.com/tips/lint