Display the value '??' in strings.xml - android

I have a little problem but I have no answer. I use the strings.xml to store all texts from my application. If i want save the next value : '??' so I have an error when I execute my app.
<string name="unknown_text">??</string>
error: Error: No resource found that matches the given name (at 'unknown_text' with value '??').
Is there a solution to save this value in xml or I must use setText() for all widgets which need it ?
Thx

It's good habit to always quote whole string:
<string name="unknown_text">"??"</string>

use <string name="questions">\??</string>
check FormattingAndStyling and this link for more detail.

Related

How to maintain unique error phrase and strings document for review every time in android

I want to maintain a single document for all error codes and strings.And i need this document keep on updating when any error code or string is changed in my android application.Is there any better way to maintain a document like this for android applications.I want to link this error code(int) with string in strings.xml in resource file.Please help me guys.
Thanks
AnkiReddy
The strings.xml docs is your friends i guess ?
you declare a phrase like this
<string name="delete_store">Delete a store</string>
and use it like this
android:hint="#string/delete_store"
Then if you change your value in the file string.xml, the value change all over your app.

android email address logcat shows only numbers

in my android app i have into my strings.xml the following value:
<string name="Email">mail#domain.com</string>
in my main.class i have this log.e:
Log.e("-->", ""+R.string.Email);
The output is:
2131099691
Why?
R.string.email is a string resource id. In order to get the value associated with it, you need to call getResources().getString() & pass in the resource's id as an argument:
Log.e("-->", ""+getResources().getString(R.string.Email));
R.string.Email is actually a resource id of an string in xml resource.
That's why it is printing it inside log.
If you want to log actual strung value , you have to use:
context.getString(R.string.Email);
Hope it helps.
Try this:
Log.e("-->", ""+getResources().getString(R.string.Email));

How to add a long text with many paragraphs in string.xml in android

When I added many paragraphs between
<string name="bbb"> ...... </string>
it showed me following error.
[2014-12-22 14:54:55 - Inspiration] Her father rescued her from the heartless husband and she was back her to the
[2014-12-22 14:54:55 - Inspiration] G:\adt-bundle-windows-x86_64-20140702\workspace\Inspiration\res\layout\activity_women.xml:11: error: Error: No resource found that matches the given name (at 'text' with value '#string/Women1').
Try this
<string name="Your string name" > This is your string.
This is the second line of your string.\n\n Third line of your string.</string>
This will result in the following on your TextView:
This is your string.
This is the second line of your string.
Third line of your string.
You can add the long string to /res/values/strings.xml, as it appears you've done.
You can separate paragraphs with the \n.
You will need to use Unicode codes for special characters like backslash, etc.. See this answer.
Yes, was just going to add what's in the other answer: The other trick is to use the CDATA trick:
<![CDATA[Foo Bar baz is cool]]>
See this answer for more on that.

Use a reference to a String resource as a name attribute for other resources

Is it possible to create an resource in XML where the name attribute references to a String resource?
Let me show an example:
In my strings.xml I have:
<string name="some_string">I am a string</string>
Can I (in some way) use the String resource as a name for another resource? As in:
<integer name="#string/some_string">1</integer>
To clarify some things:
I know you can reference the value of an resource to a reference:
<string name="another_string">#string/some_string</string>
I already tried doing it without quotes:
<integer name=#string/some_string>1</integer>
I don't think this will ever work.
Also this would be a very bad idea, because the value of your String can change by translations to other languages. Following this the name (ID) of your integer would change by using your App in different countries. This could break your code by changing the Phones default language.
As mentiond in the documentation of the More Resource Types
:
name : String. A name for the string. This name will be used as the resource ID.
so you cant do this .
hope that helps

Dynamically referencing a String in Strings.xml in Android

Say I have the following my strings.xml file:
<string name="string0">Lorem</string>
<string name="string1">ipsum</string>
<string name="string2">dolor</string>
In my activity an ID is set based on the clicking of a button. If the top button is clicked the id is 0, middle is 1 and bottom button is 2.
What would the syntax look like for referencing one of the three strings?
I know R.string.string0 works but I want to do something equivalent to:
R.string["string"+currentID]
where I derive the string to use based on the ID.
Just not sure what the syntax would look like in Java/Android.
Thanks in advance,
Tony
Could you not just use a string array in your resources instead of separate string entries?
That's a bad approach. It's slow. It'd be better to have an internal integer array with all the R.string IDs.
If you really insist on using a string-based approach, use Resources.getIdentifier().

Categories

Resources