Eclipse/ADT xml string to strings.xml automatically? - android

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.

Related

text/hint in XML changed back from #string to hardcode

After some time my xml files declaration are changing. sometimes when I open it my textvalues are changing from my #string values to regular texts. In this case:
android:id="MAC Adress" Can someone explain why?
<EditText
android:layout_width="330dp"
android:layout_height="wrap_content"
android:hint="#string/mac" <!-- <-- THIS -->
android:id="#+id/editText_mac"
android:maxLength="17"
android:paddingLeft="15dp"
android:paddingRight="15dp" />
it is a nice feature of AndroidStudio. It shows the content pointed by the id. But you are still using the reference to the localized value. As matter of fact, if you click on the text, you will see again #string/ instead of the value pointed by it
try clicking on it, you'll see the previous string path. It just hides to make it clear and easy to access the string , instead of the path to string. It's the same with every one...

Error message 'no label views point to this text field'

I've just been doing the tutorial on how to make a temperature converter, but the tutorial does not explain how to get rid of the exclamation mark on my edit text input. The error is:
No label views point to this text field with an android:labelFor="+id/#+id/editText1"attribute
The tutorial is http://www.vogella.com/articles/Android/article.html.
Take another look at your XML layout file. You want to have #+id/editText1 as the id. When you reference it, you want to call:
EditText editText1 = (EditText)findViewById(R.id.editText1)
Note that you don't need to write the #+id/ when referencing, only when defining an attribute. Maybe that could be the problem.
Also, if you want to change the text, I have not read the tutorial, but it should be defined inside strings.xml.
Add this to your TextView in the layout xml:
android:labelFor="#+id/editText1"
I don't know why, but it works.
If you prefer to suppress the error using lint, I'll describe below how to figure out the name of the lint warning to suppress.
Full warning message to suppress:
Missing labelFor attribute
No label views point to this text field with an
android:labelFor="#+id/searchTextView" attribute
Text fields should be labelled with a labelFor attribute, provided
your minSdkVersion is at least 17.
If your view is labeled but by a label in a different layout which
includes this one, just suppress this warning from lint.
To suppress it, add the attribute tools:ignore="LabelFor" to the component declaration, for example:
<EditText
android:id="#+id/searchTextView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerVertical="true"
android:layout_marginStart="62dp"
android:layout_toStartOf="#id/clearTextButton"
android:ellipsize="end"
android:inputType="textLongMessage|textNoSuggestions"
android:maxLines="1"
tools:hint="Search STATICDCA"
tools:ignore="LabelFor" />
To determine that the value was LabelFor, I did as follows in Android Studio:
Open the .xml layout file containing the component
Go to menu option Analyze > Inspect Code
Go to the inspection results at the bottom of the screen
Drill all the way down into the warning
Click the warning
On the right side of the inspection results is a detail view, click the drop-down box labeled "Suppress" and choose "Suppress with #SupressLina (Java) or tools:ignore (XML) or lint.xml"
This automatically added tools:ignore="LabelFor" to my component definition in my xml file

xml file error in android

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

any way to suppress warnings about "hardcoded strings" in layout files?

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

How to format Android xml in Eclipse?

The GUI designer for Android in Eclipse spits out this type of XML (into a layout):
<Button android:layout_height="wrap_content" android:id="#+id/btnSaveMessage" android:layout_width="wrap_content" android:layout_gravity="right" android:text="#string/text_save" android:width="125dp"></Button>
Is there a refactoring in Eclipse to turn this goo into pretty formatted XML like this:
<Button android:layout_height="wrap_content"
android:id="#+id/btnSaveMessage"
android:layout_width="wrap_content"
android:layout_gravity="right"
android:text="#string/text_save"
android:width="125dp"/>
Press ctrl + shift + F
or
Press ctrl + I
Yes you can do this.
For doing so follow the steps
Select the source code in xml file.
Right click on selected source code.
Now go the Source->cleanup document.
Now a cleanup window is popup, mark check for appropriate options and click ok.
you can also choose source->format to do some formatting in xml. cleanup provide some other fuctionalites besides format.

Categories

Resources