xml file error in android - 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

Related

Android layout reference xml element later in file

How do I reference a later XML element?
Here's a specific use case. Let's say I have a form with a root LinearLayout, containing LinearLayouts for multiple rows, each row having one or more text input areas.
Here's a visual of what I'm going for. First pic is from Venmo's app, second is a rendering of the following XML.
Such a layout could look like this:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="#+id/row_card_number"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<EditText
android:id="#+id/card_number"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:nextFocusDown="#id/month"/>
</LinearLayout>
<LinearLayout
android:id="#+id/row_date"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<EditText
android:id="#+id/month"
android:layout_height="wrap_content"
android:layout_width="100dp"
android:nextFocusDown="#id/year"/>
<EditText
android:id="#+id/year"
android:layout_height="wrap_content"
android:layout_width="match_parent"/>
</LinearLayout>
</LinearLayout>
In this use case, forward referencing is necassary in order to set the next focus element. That way, when you press the next button on the keyboard, it'll go to the correct view. In this sample xml, without the nextFocusDowns, pressing next would go from name to month, and never go to year.
However, if you try to compile this, you'll get an error:
Error:(18, 36) No resource found that matches the given name (at 'nextFocusDown' with value '#id/month').
This is because the id month hasn't yet been initialized when I'm trying to reference it, since that's later in the file. How can I reference an id in xml that appears later in the file?
The simplest solution is just to replace
android:nextFocusDown="#id/month"
with
android:nextFocusDown="#+id/month"
When the compiler is parsing your XML to add the id's to R.java, it just reads top to bottom. When you have #id/month, it searches through the existing id's, and fails to find it.
However, if you do #+id/month, it creates a new id, and links to that. When it gets to android:id=#+id/month in the actual month view, it links it to the same id that we already created.
This brings up the question: If you can replace #id/ with #+id/, and #+id/ will work regardless of the order of elements, why even bother to use #id/?
The reason for this is if the id doesn't exist, #id/ will throw a compiler error, while #+id/ will log a warning at runtime.
Consider this XML:
<EditText
android:id="#+id/month"
android:layout_height="wrap_content"
android:layout_width="100dp"
android:nextFocusDown="#+id/SOME_RANDOM_ID"/>
<EditText
android:id="#+id/year"
android:layout_height="wrap_content"
android:layout_width="match_parent"/>
When this is parsed, a new id element SOME_RANDOM_ID is created. However, when Android tries to apply it at runtime, it can't find an element with that id. If you look at Logcat, you'll see this:
W/View﹕ couldn't find view with id 2131689604
This log message is both hard to find and hard to debug. One small typo in a #+id/ and you'll have a bug that could be incredibly difficult to debug. However, if we had done:
android:nextFocusDown="#id/SOME_RANDOM_ID"
Then we'd get a compiler error, something like:
Error:(18, 36) No resource found that matches the given name (at 'nextFocusDown' with value '#id/SOME_RANDOM_ID').
This is much easier to find and debug.
tl;dr: You can use #+id/ instead of #id/ and you'll be able to forward reference, but note that that can make small typos incredibly difficult to debug.
You might be able to use a RelativeLayout to make all the Views exist in reverse order in the xml, but that seems like overkill to me.
I had the same issue recently and I used #+id/my_new_id the first time I referenced the element and later in the XML in the element definition, I assigned #id/my_new_id to the android:id attribute. It seems it works fine and it's not necessary write #+id with the same id more than one time avoiding possible warnings.
For example:
<LinearLayout
...
android:layout_toLeftOf="#+id/my_new_id"
... >
...
</LinearLayout>
<ImageButton
android:id="#id/my_new_id"
... />

Android: How to mark sample string in layout file

I have many lists in my android app. I'm using strings as samples (android:text) for the layout preview.
Example:
<TextView
android:id="#+id/firstname"
android:layout_width="0dp"
style="#style/TextListStyle"
android:text="Peter"/>
Android studio is marking this as a warning but I don't want to put every sample string into the strings.xml file.
What is the best way to deal with this?
If you want it just for preview consider this:
tools:text="John Doe"
More Infos on Designtime Layout Attributes.
Add tools:ignore="HardcodedText" in your root layout, like:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:ignore="HardcodedText" >
However this is quite bad practice, for the reasons stated here:
Hardcoding text attributes directly in layout files is bad for several
reasons:
When creating configuration variations (for example for landscape or
portrait) you have to repeat the actual text (and keep it up to date when
making changes)
The application cannot be translated to other languages by just adding new
translations for existing string resources.

Android: ids.xml versus using #+id/id_name everywhere

So I'm trying to decide whether it would be worth it to refactor out my current use of id's in all of my android layouts to an ids.xml file, or to just leave my view code the way it is (with multiple views sharing ids and both views using "#+id/id_name).
Is there a significant compile/runtime performance benefit to refactoring out the ids to the ids.xml file? How about if the application gets bigger?
Related resources:
http://developer.android.com/guide/topics/resources/more-resources.html#Id
Thank you for your time.
I used <item type="id"> resources in my app because I have TextEdit views that serve a similar purpose in more than one Activity.
ids.xml has the following advantage: all ids were declared, so compiler can recognize them. If something like this:
<TextView
android:id="#+id/text1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignBelow="#id/text2"
android:text="...."/>
<TextView
android:id="#+id/text2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="...."/>
Can result in compiling error because text2 was refered before declared

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

Whether we should give string values in Strings.xml or in layput xml file itself

I have one ques that whether it is good to give all string text that we are going to use in our layout file in strings.xml file or should we give it in "string text" in layout file itself for attribute values like android:text="some text".
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:id="#+id/ll1"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button android:text="Button"
android:id="#+id/butShow"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Button>
</LinearLayout>
then is it fine to give android:text="Button" or we should use android:text="#string/btnlabel"
strings.xml
<resources>
<string name="btnLabel">Button</string>
</resources>
We should specify all string text in strings.xml or only specific ones.
check my answer hereOk
After looking into the source code of android.content.res.resources and some other classes, it is evident that using Resources and getting resources through getResources() is a bit costly.
However, using getResources() has its advantages:
It helps you externalize your resources.
For any type of resource, you can specify default and multiple alternative resources depending maybe on Locale, Screen Depth/Resolution...
You will do better to extract all your strings to strings.xml incase you ever decide to internationalize your app! then you only have to do a different strings.xml for each language rather than layouts too

Categories

Resources