Android String Resources Reference - android

i am using Eclipse 3.7.2.
I dont know why but the last line leads to an error highlightning
<string name="app_name">Test</string>
<string name="title">#string/app_name</string> <!-- works //-->
<string name="txt_text">Checkout #string/app_name this works</string> <!-- works //-->
<string name="txt_recommend">#string/app_name is not working</string> <!-- error //-->
is there a work around?

I believe you can't mix references and text in the XML. Use formatting placeholders instead.
http://developer.android.com/guide/topics/resources/string-resource.html#FormattingAndStyling
Instead of
<string name="app_name">Test</string>
<string name="txt_recommend">#string/app_name is not working</string>
this would look like
<string name="app_name">Test</string>
<string name="txt_recommend">%s is not working</string>
And in the code:
String text = String.format(res.getString(R.string.txt_recommend), res.getString(R.string.app_name));

The error is probably from the second last line rather than the last line.
<string name="txt_text">Checkout #string/app_name this works</string>
In this line you seem to be mixing text ("Checkout" and "this works" ) with a reference ("#string/app_name")
I dont think you can do that just in xml.

Related

How to include a Android values strings.xml file into another strings.xml

I need to put all codes inside values-zh/strings.xml into values-zh-rCN/strings.xml. How do i do this?
I've tried this <include layout="#values-zh/strings" /> but did not work, Intelij is telling me to declare the file first.
Assuming values-zh/strings.xml is your base string resources, you can put all your strings there, and only provide values for those you need to override in values-zh-rCN/strings.xml.
In values-zh/strings.xml:
<string name="title">default title</string>
<string name="content">default content</string>
In values-zh-rCN/strings.xml:
<string name="title">simplified title</string>
Now when you are on simplified Chinese config, content will be 'default content', and title will be 'simplified title'.

how to make #symbol in string.xml for android

I am trying to make #sancorporation in my android application. I have written strings in string.xml e.g "<"string name="cor_name">sancorporation"<"/string>, but I need something like this "<"string name="cor_name">#sancorporation"<"/string>. How to take special symbol in android?
Use ascii value of # symble is 0040 so try it
for #sancorporation
<string name="mytest">#sancorporation</string>
or
<string name="mytest">\u0040 sancorporation</string>
Maybe this will help
<string name="specialChar"> "#" </string>
You can also try this
<string name="specialChar"> \# </string>
Edit:
Now I notice that you want to write #sanCorporation.
so you can use this in string.xml file:
<string name="corp"> "#sanCorp" </string>

error : The element type "string" must be terminated by the matching end-tag "</string>"

ended the statement with and clean the the project still i am getting the same error ,here i am placing the code
<resources>
<string name="app_name">LiveWallpaper1</string>
<string name="hello_world">Hello world!</string>
<string name="menu_settings">Settings</string>
<string name="title_activity_main">MainActivity</string>
<string name="wallpaper_description">wallpaper descriptioning></string>
</resources>
I think the error is here
wallpaper descriptioning>
the '>' at the end is not allowed because it is a special character. try to replace it with \u003E, which is the unicode definition. for other unicode definitions refer here
http://jrgraphix.net/research/unicode_blocks.php?block=0
hope this works
On the last string line remove the '>' character before your
</string>
tag. That is causing your error.

HTML Formatting in TextView

All,
I have the following in my string resource xml file.
<?xml version="1.0" encoding="utf-8"?> <resources>
<string name="hello">Hello World, ProDroid_Android!</string>
<string name="app_name">ProDroid_Android-CH3-Resources</string>
<plurals name ="eggs_in_A_basket">
<item quantity ="one"> There is 1 egg</item>
<item quantity = "other"> There are %d eggs</item>
</plurals>
<string name="simple_string">simple string</string>
<string name="quoted_string">"Quoted String 'xyz' string"</string>
<string name="double_quoted_string">\"Double Quoted String\""\";</string>
<string name="java_format_string">Hello %2$s Java format String. %1$s again.</string>
<string name="tagged_string">Hello <b><i> Slanted Android.</i></b>, You are bold</string>
When I go to put the tagged_string into a TextView with the HTML formatting it does not display with it's formatting. However, if I cut and paste the Hello Slanted Android., You are bold and manually put it into .setText it works, or if I set it statically in my layout xml file. However using the Java code below it does not work (All you see is unformatted text):
//HTML
TextView t6 = (TextView) findViewById(R.id.textView6);
t6.setText(Html.fromHtml(ProDroid_Android.this.getString(R.string.tagged_string)));
Any help would be appreciated.
Thx
If you format your text in XML it isn't necessary to use Html.formatHtml(...)
Try this:
t6.setText(getString(R.string.tagged_string);
EDIT:
Hmm... This way worked for me fine and I tried exactly your String.
Maybe your Project needs a clean (Project > Clean ...)
If this doesn't help:
I found another question. Maybe the answer there can solve your problem

Internal string resource references

I wonder if it's possible to reference a XML string value in another XML String resource.
Things like the following are possible:
<string name="title">MyTitle</string>
<string name="activityTitle">#string/title</string>
But in case of an concated resource string, I found no solution yet, so what I'm searching for is the following:
<string name="title">MyTitle</string>
<string name="subTitle">#string/title - mySubTitle</string>
So far I was only able to solve it programmically via:
<string name="title">MyTitle</string>
<string name="subTitle">%1$s - mySubTitle</string>
getResources().getString(R.string.subTitle, getResources().getString(R.string.title));
I would like to keep the string references in the string.xml file itself.
What you want is not supported at this time, AFAIK.

Categories

Resources