No resource found when using '#string/value' in layout.xml - android

just started programming android and I am trying to make a simple xml file but it seems it got some problem with the android:text=#string/..
I have got this error:
error: Error: No resource found that matches the given name (at 'text'
with value '#string/ false_button').
For each of the strings..
The code is:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width = "match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding= "24dp"
android:text="#string/question_text"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/true_button"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/false_button"/>
</LinearLayout>
</LinearLayout>

You need to have the resources that you want to reference in your layout in strings.xml under res/values/
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="true_button">True Button</string>
<string name="false_button">False Button</string>
</resources>
#string/true_button refers to a resource in strings.xml
You can also have hardcoded string but not recommended
android:text="True Button"/>

add following to your string.xml file under values folder
<string name="true_button">true</string>
<string name="false_button">false</string>

Make sure that you are created string value with the name of false_button in the string.xml
<string name="false_button">false</string>
or just give name within the quotes "" in the android:text attribute
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="true_button"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="false_button"/>

Click on res folder then click on values and then open strings.xml and add following content.
<string name="true_button">whatever text you want to write to your button</string>
<string name="false_button">whatever text you want to write to your button</string>
and save your file. Then clean your project by clicking Project -> clean and then run it again.
Hope this will help you.

make sure that in res/values/
you have all the resources present there as follows
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="true_button">True</string>
<string name="false_button">False</string>
</resources>
otherwisely you can hardcode values at yours layout where you specified button as
android:text="True "/> or android:text="False"/>

In your android project, look for the res directory. Open it and inside it look for values directory. In values, open strings.xml and check if it has a flase_button value stored in it . If not you can add a new value using name as false_button and value as what you want to display on the button.
When you open the strings.xml in IDE like eclipse, you can switch views of the .xml file by selecting proper tab given at the bottom of your eclipse screen. Select strings.xml view and
modify the current strings.xml to
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">app</string>
<string name="false_button">FALSE</string>
</resources>
Hope this was helpful.

You just need to add this in your Strings.xml class
just add <string name="false_button">False Button</string> line in your strings.xml
so that it will look like :
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="false_button">False Button</string>
</resources>

All of these responses that refer to adding the string to the string.xml file make sense and work, but fail to address the original problem: this book ("Big Nerd Ranch: Android Programming") goes through these steps one at a time, mentions referencing the string in the layout XML file, but fails to mention adding to the strings.xml file. Fail.

Related

Why is it telling me to use #string/ where ever i am using android:text="' ? And that it's hardcoded

The error says that for hardcoded you must use #string/.. what is wrong with just using "android:text= "price" ??
<LinearLayout
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"
android:orientation="vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:layout_marginTop="16dp"
android:text="PRICE" />
A string resource provides text strings for your application with optional text styling and formatting. There are three types of resources that can provide your application with strings:
String
XML resource that provides a single string.
String Array
XML resource that provides an array of strings.
Quantity Strings (Plurals)
XML resource that carries different strings for pluralization.
All strings are capable of applying some styling markup and formatting arguments.
for more information, read it here
Also it's better to use a string resource, that way if you have to change the text you change only one variable
So in your res folder there will be another folder called values, and then access the strings.xml file, inside that file you'll put a string resource to use in your app, like so:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">yourAppName</string>
<string name="hello_world">Hello world!</string>
<string name="action_settings">Settings</string>
<string name="price">PRICE</string>
</resources>
and then in your xml file, you will change android:text="PRICE" to android:text = "#string/price"

how to fix resource not found error in android layout

This error shows up when I was writing my code. error:
Error: No resource found that matches the given name (at 'hint' with value '#string/edit_message').
The code is:
<LinearLayout 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"
android:orientation="horizontal" >
<EditText android:id="#+id/edit_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="#string/edit_message" />
</LinearLayout>
That is easy.Just add a item like this in your res/values/strings.xml file:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="edit_message">YOUR MESSAGE TEXT</string>
</resources>
The default string resources folder android generates called strings, did you write the XML manually or it is a generated code?
if manually, try to fix it:
android:hint="#strings/edit_message"
Set hint on edittext like this from programmatically if resource is not found in your application:
editMessage.setHint("hint message"):
or you can simply
Call setHint() on the EditText.
Refer: http://developer.android.com/reference/android/widget/TextView.html#setHint%28int%29

TextView textSize and #string in Android

I have the following xml code:
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Press Button" <!--Warning -->
android:textSize="45dp" <!--Warning -->
android:layout_gravity="center"
android:gravity="center"
android:id="#+id/tvDisplay" />
In the xml code i found two warning first that dp contains that which i got the waring to use sp indeed. What is the reason it showing so?
Second warning and may be error is that i am using android:text="Press Button" it tell me to use #string indeed. If i uses the same #string is displayed in text which look awkward. What is the reason for it!
Hardcoded String value in View is not recommeded by developer.android.com as making of Android Application compatible with different languages is twisted up to.
Referenced from
To add support for more languages, create additional values directories inside res/ that include a hyphen and the ISO country code at the end of the directory name. For example, values-es/ is the directory containing simple resourcess for the Locales with the language code "es". Android loads the appropriate resources according to the locale settings of the device at run time.
Once you’ve decided on the languages you will support, create the resource subdirectories and string resource files. For example:
MyProject/
res/
values/
strings.xml
values-es/
strings.xml
values-fr/
strings.xml
Add the string values for each locale into the appropriate file.
At runtime, the Android system uses the appropriate set of string resources based on the locale currently set for the user's device.
For example, the following are some different string resource files for different languages.
English (default locale), /values/strings.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="title">My Application</string>
<string name="hello_world">Hello World!</string>
</resources>
Spanish, /values-es/strings.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="title">Mi Aplicación</string>
<string name="hello_world">Hola Mundo!</string>
</resources>
Referring to your OP:
XML file saved at res/values/strings.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="press">Press Button</string>
</resources>
This layout XML applies a string to a View:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/press"
android:textSize="45dp" <!--Warning -->
android:layout_gravity="center"
android:gravity="center"
android:id="#+id/tvDisplay" />
This application code retrieves a string:
String string = getString(R.string.hello);
Use sp for setting size as suggested by developer.android.com
sp : Scale-independent Pixels - This is like the dp unit, but it is also scaled by the user's font size preference. It is recommend you use this unit when specifying font sizes, so they will be adjusted for both the screen density and the user's preference.
XML file saved at res/values/dimens.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="font_size">16sp</dimen>
</resources>
This application code retrieves a dimension
Resources res = getResources();
float fontSize = res.getDimension(R.dimen.font_size);
This layout XML applies dimensions to attributes:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Press Button" <!--Warning -->
android:textSize="#dimen/font_size"
android:layout_gravity="center"
android:gravity="center"
android:id="#+id/tvDisplay"
/>
Recommended dimension type for text is "sp" for scaled-pixels (example: 15sp)
from the developer.android
Android stander TextView size is use SP and you are hardcode String that i give warning.
Please User your String.xml in value folder and select this String then it do not give any error.
Thanks
It's better to use SP instead of DP.
It's recommend to use always string resources file, because if you need to change a single #String used in multiples xml files, you have to change only one time. Vice-versa, if you write your strings inside the xml layout files, if you need to change a string you need to search for the string, search for the xml file and then change as many occurrances you need.
In conclusion, it is not a good practice to hard code strings. You should specifies them to a string resource file and then reference them in your layout.This allows you to update every occurrence of a single word in all layouts at the same time by just editing your strings.xml file.
It is also necessary for supporting multiple languages definitions as a separate strings.xml One file for one language!

Android TextView

I have an example in android that I am trying to run.
There were 2 ways to do it..
// Get a drawable
ColorDrawble redDrawable = (ColorDrawable).getResources().getDrawable(R.drawable.red_rectangle);
//Set it as a background to a text view
textView.setBackground(redDrawable);
When I put this in the Eclipse IDE I get an error ColorDrawble cannot be resolved to a type
I have the textview in the main XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView android:layout_width="fill_parent"
android:id="#+id/texter"
android:layout_height="wrap_content"
android:text="this string"/>
</LinearLayout>
and the resources in the strings xml file
<resources>
<string name="hello">Hello World, ResourceTesterActivity!</string>
<string name="app_name">ResourceTester</string>
<drawable name="red_rectangle" >#f00</drawable>
<drawable name="blue_rectangle">#0000ff</drawable>
<drawable name="green_rectangle" >#f0f0</drawable>
</resources>
**
you can simply use a drawable like this :
// Get a drawable
Drawable redDrawable = YourActivity.this.getResources().getDrawable(R.drawable.red_rectangle);
//Set it as a background to a text view
textView.setBackgroundDrawable(redDrawable);//i've changed setBackground with setBackgroundDrawable.
or you can directly use :
textView.setBackgroundResources(R.drawable.red_rectangle);
NOTE : clean and rebuild your project , and run it to test
and for the drawable, you dont need to declare it in strings.xml , just add your drawable on the folder drawables , and it will work
It should be:
ColorDrawble redDrawable = (ColorDrawable)
getResources().getDrawable(R.drawable.red_rectangle);
You don't need the dot (perhaps it's a typo).
Then press Ctrl+Shift+O (Organize Imports) to import ColorDrawble class.

setting textColor in TextView in layout/main.xml main layout file not referencing colors.xml file. (It wants a #RRGGBB instead of #color/text_color)

I'm trying to set some general colors for a program I'm writing. I created a colors.xml file and am trying to directly reference the colors from the layout.xml file. I believe I'm am doing this correctly however it's giving me the following error:
Color value '#colors/text_color' must start with #
Here is my res/values/colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="background_color">#888888</color>
<color name="text_color">#00FFFF</color>
</resources>
Here is my res/layout/main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TextView
android:layout_width="fill_parent"
android:text="#string/hello"
android:layout_height="wrap_content"
android:id="#+id/TextView01"
android:textColor="#colors/text_color"/>
</LinearLayout>
I looked at some references on the android developers site: More Resource Types : Color and found this code:
Example:XML file saved at res/values/colors.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="opaque_red">#f00</color>
<color name="translucent_red">#80ff0000</color>
</resources>
This application code retrieves the color resource:
Resources res = getResources();
int color = res.getColor(R.color.opaque_red);
This layout XML applies the color to an attribute:
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#color/translucent_red"
android:text="Hello"/>
I think my two xml files follow this example pretty close - however the only difference is that I haven't used any application code to retrieve the color resource. I don't believe this is necessary (but it is a difference.) I thought I'd see if anyone else had similar problems or a solution? or is this a bug?
I did update all my android sdk (and Eclipse plugin) files last week so I believe them to be the latest.
A variation using just standard color code:
android:textColor="#ff0000"
After experimenting on that case:
android:textColor="#colors/text_color" is wrong since #color is not filename dependant. You can name your resource file foobar.xml, it doesn't matter but if you have defined some colors in it you can access them using #color/some_color.
Update:
file location:
res/values/colors.xml
The filename is arbitrary. The element's name will be used as the resource ID. (Source)
You have a typo in your xml; it should be:
android:textColor="#color/text_color"
that's "#color" without the 's'.
You should write textcolor in xml as
android:textColor="#color/text_color"
or
android:textColor="#FFFFFF"

Categories

Resources