I know this is a simple question, but I can't figure out how to reference strings in android.
for example
on this EditText, I don't want to hardcode the string hi, I want it to reference a string in the res folder. How do I do this?
Suppose you have following string in xml.
<string name="string_one">My string</string>
You have to access this in code :
String str = resources.getString(R.string.string_one);
You can also used in xml where you have take EditText :
android:text="#string/string_one"
Create a file strings.xml in res/values folder, and add a text like
<string name="card">Card</string>
then reference the text in layout as android:text="#string/card"
Figured it out. All you need to do is create a string in the res value folder and then reference it
Create a file strings.xml in res/values/ folder, and add a text in it like
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="mystring">MyStringFromXML</string>
</resources>
then reference the text in layout as :
android:text="#string/mystring"
And in Java file do it like:
String string = getString(R.string.mystring);
Related
In my little app, I have a long list of data. Before, I used an editor to let the user enter the data - but now I would like to put the data static in program code/string list value Folder.
What is the best way to achieve this?
If you already know the values inside array you can crate string array inside strings.xml file which is present under values.
example:
XML file saved at res/values/strings.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="sports_array">
<item>Football</item>
<item>Cricket</item>
<item>Hockey</item>
<item>Tennis</item>
</string-array>
</resources>
This application code retrieves a string array:
Resources res = getResources();
String[] sportlist = res.getStringArray(R.array.sports_array);
so now your sportlist will contain all the items specified in sports_array which is declared inside strings.xml
for more information pls see
http://developer.android.com/guide/topics/resources/string-resource.html
I have updated some string values in strings.xml and my application now shows not the new text but something like #234545201. I have cleaned the projected and rebuilded, there are no import android.R anywhere, just R related to my package. What went wrong?
To obtain a string from your strings.xml file, you can do a few things.
If you need it as a String object, you can use getString(R.string.string_id) to fetch the string, given an ID.
If you're trying to set the text of, say, a TextView, you can actually simply use setText(R.string.string_id) and the OS will obtain the correct string for you.
In other words, the TextView class has a method called setText(int resid), and that's also the reason why you can't write something like the following:
TextView.setText(12345680);
Are you trying to read it directly as R.string.my_string_resource?
Try passing it to getString() as getString(R.string.my_string_resource).
You can put your stings.xml file in the follwing format.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Yamba</string>
<string name="titleYamba">Yamba</string>
<string name="titleStatus">Status Update</string>
<string name="hintText">Please enter your 140-character status</string>
<string name="buttonUpdate">Update</string>
</resources>
and use the name as reference of your textbox ids like.
android:text="#string/app_name"
In my android application, i have to write a string into a file. Before that I want to format the string to make the font bold and have a new text size. So I modify the string.xml by
"<string name="file_title" ><b>#title*</b></string>",
for bold the text and wrote to text file. But it is not showing as bold in the file.
Is that right method i used to implement the string bold?
Also i want to know how to change the text size of a string direct
from string.xml
how to add that boldness in file... i tried
"<string name="note"><![CDATA[<b>title</b>]]></string>"
in java code get your string
yourtextview.setText(Html.fromHtml(getString(R.string.note)));
by using texview it"s coming bold , exaclty i want to write to file, using the below code
"bufferWritter.write(String.valueOf(Html.fromHtml(getString(R.string.note))));"
but i can"t see the boldness in my print.txt file...
Thanks & Regards
Anoop
You Should try to use dimens.xml for dimension values
then reference it #dimen/yourDimensionName
Refer this link for
Or other way is using HTML from java code
I tried this is working.
In your string.xml file try this
<string name="test"><![CDATA[<b>test</b>]]></string>
In your java code settext this string value from string resource like this
textview.setText(Html.fromHtml(getString(R.string.test)));
Its working fine....
Try this
in string.xml write like this
<string name="note"><![CDATA[<b>title</b>]]></string>
in java code get your string
yourtextview.setText(Html.fromHtml(getString(R.string.note)));
This is not the right way. Just change its size from xml directly. use.
android:text="Date"
android:textStyle="bold"
android:textSize="xdp"
In order to do that, you should have your xml resource as:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="file_title"><b>title*</b>!</string>
</resources>
And then use:
Resources res = getResources();
String text = res.getString(R.string.file_title);
CharSequence styledText = Html.fromHtml(text);
In order to change the text or other parameters, you can add a <span></span> with specific css style around your text.
For more information please check here: http://developer.android.com/guide/topics/resources/string-resource.html
Hope this helps!
I have two xml files in values folder for strings:
new_strings.xml
strings.xml
From strings.xml I can access string as follows:
String str = getString(R.string.app_name);
How can I directly access from new_strings.xml?
By the same way you're accessing the values in strings.xml file.
Example :
strings.xml :
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name_1">First app name</string>
</resources>
new_strings.xml :
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name_2">Second app_name</string>
</resources>
In your java code you can do :
R.string.app_name_1
R.string.app_name_2
and you can access both values which are in two different xml files.
As the doc said:
file location:
res/values/filename.xml
The filename is arbitrary.
The <string> element's name will be used as the resource ID.
compiled resource datatype:
Resource pointer to a String.
To give you a really short answer:
Yes, you can access to all strings in both files regardless of the filename. Although it's generally a good practice to keep all strings in one file, I do personally find it useful to separate strings into multiple files, especially for localization purposes, which brings me to the next point:
Yes you can access to the localized strings in other locale folders, e.g. values-es, as long as it contains a translated copy of all string files in the values folder.
For example, if you have strings.xml and new_strings.xml in your values folder, make sure you also have both of those files in your values-es folder in order for your app to display the localized strings in Spanish.
Hope this clarifies things up for you =)
I want to associate URLs with items in a string array so that when an item in a ListView is selected data is scraped from the URL. I was wondering could the URL be added as metadata in the strings.xml file? Does anyone have any suggestions as to how to proceed?
Strings.xml isn't the right place. You COULD define the URLs, one at a time, in strings.xml, like this:
<string name="url1">http://foo1.com</string>
<string name="url2">http://foo2.com</string>
// etc
And then create an array at runtime, dumping all these values in one-by-one. But that would suck.
Instead, create an xml file in res/values/, name it whatever you want (for instance, urlvals.xml), and populate it like this:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<array name="urls">
<item>http://foo1.com</item>
<item>http://foo2.com</item>
</array>
</resources>
Then, in code, reference it in the following way:
String[] myUrls = getResources().getStringArray(R.array.urls);
If you want an array of string resources, you can either:
1) add the strings in strings.xml (as per #Alexander's answer), and reference them by name in an array in values/yourresourcename.xml as per here.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<array name="myLinks">
<item>#string/link1</item>
<item>#string/link2</item>
</array>
2) add them directly the string array (verbatim) like #Alexander suggests
How you associate them with the elements in the ListView is up to you. I would suggest (if the content is static) creating a matching string array for (e.g.) the string you want to actually display (as opposed to the link itself).