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
Related
If I have the following in a layout XML:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="string Test" />
it flags the 4th line "Hardcoded string should use #string resource"
Fine, I know that, and would like to. Is there an option to select this line and have the IDE automatically add a <string></string> to the strings.xml and replace the android:text= to refer to that resource?
Yes, there is an option to do this by selecting the text and have the IDE automatically do the job, just thank to JAL :
How to add string resources in Eclipse?
By the way, in JAL's answer, when he says GO TO REFACTOR, he means REFACTOR in the menu bar, not right click's context menu.
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 "<".
hi have problem when i view graphical.layout of this file
<LinearLayout 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:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="this is main text" />
</LinearLayout>
when i view graphical layout of this view this xml file then this warning show
[I18N] Hardcoded string "this is main text", should use #string resource
but i dont want to declare string valiable i want to show this "this is main text" without declaring string
The warning about 'should use #string resource' is just a reminder that locale-specific information should be placed in 'resource bundles' (eg /res/values/strings.xml) so that labels and other text can be provided in different languages without having to re-code and recompile the entire application for each language.
If you're just testing things out, you can safely ignore that warning, but it's a good idea to get in the habit of referencing strings as resources in external files rather than hardcoding them directly in the application itself.
It is not good practice to hard code strings into your layout files. You should add them to a string resource file and then reference them from your layout.
refer this answer and this one also.
You must declare your text using `#string` resource that
is a good programming format.You should prefer using
`string.xml` in value folder in res folder if you want
to use texts.
If you want to suppress the specific warning use
tools:ignore="HardcodedText"
more details
http://tools.android.com/recent/ignoringlintwarnings
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>