I was wondering if I could place XML within /res/values/strings.xml? I ask this because I am checking for the XML data file for my application, if it does not exist yet then it creates it from the default contents that will be contained as a string resource.
Eclipse tries to change the less than and greater than tags to their corresponding HTML entities when using the GUI to edit the strings. Is eclipse on the right track? Because I should think that it will be written out into my file as HTML entities too. Could I use getText() rather than getString() to convert the entities back into tags?
Yes you can, just use CDATA
<string name="stringName1"><![CDATA[<html>bla</html>]]></string>
It will obviously not work unless you escape characters in there such as < or > or &.
If you do encode the XML, it should work fine but probably not the best way to do it. I would prefer binary resource.
For putting in string.xml, you may encode using
String encoded = URLEncoder.encode(xml);
And decoding is the opposite.
For binary, you place it in RAW folder and you get a binary stream and turn it to string and load.
I have done this way:
Put your string in strings.xml
<string name="my_string"><![CDATA[Your long text here]]></string>
How to use:
<TextView
android:id="#+id/textView"
android:text="#string/my_string"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
Done
Related
[I18N] Hardcoded string "Happy Birthday Debashish", should use #string resource less... (Ctrl+F1)
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. In Android Studio
and Eclipse there are quickfixes to automatically extract this hardcoded string
into a resource lookup.
Ths is not an error but a warning. As a general rule, you should never use hard-coded strings in your layout but always use string resources instead (which means that all the strings are stored in one separate file where they are easily changeable for different languages and so on).
To convert a hard-coded string into a string resource:
Put the curser on the hard coded string;
Press ALT + Enter;
Enter a name for your ressource;
Click OK.
After doing this the warning will be gone.
This is just a warning.
Define your string in string.xml file
Happy Birthday Debashish
and in textView use this string as
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/string_name"
/>
This is only a warning. The function will still work as intended. It is just recommended to place your text in the strings.xml file. This makes future changes much simpler and is easier to reference across multiple pages.
First, place the <string> element in values/strings.xml like this:
<string name="your_string_name">Happy Birthday Debashish</string>
Then, you can reference the string in the .xml file as follows:
<TextView
android:text="#strings/your_string_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
Again, it is not required to do it in this method. It just makes things simpler to manage and change in the future if needed.
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/title"
tools:text="Happy Birthday Debashish" />
When you are in the 2019 version. Go to the strings.xml and Add this in to it
<string name="Your text">Your Text</string>
Or
In the warning it has the Fix button you can click it for fix
I have a problem that I want to show a bulleted list contents which is resided in strings.xml file as an array elements. Then the problem is that how to convert the array elements in Html List format? Can any one suggest any solution regarding the same.
Thanks in advance
I just put the symbol directly into the strings.xml without any codes or anything:
<string name="msg_sms_no_note">• Notes and attachments will not be sent.</string>
There's a problem with the approach suggested by some of the answers in this thread of prepending the bullet unicode character (i.e. \u2022) to each of the Strings in the String array: You don't get proper indentation when one or more Strings in the String array span multiple lines. What you get is formatting as follows:
In order to get proper indentation, you're better using BulletSpan. In doing so, you'll get formatting as follows:
To use BulletSpan, you need to create a SpannableStringBuilder instance and append each String in your String array to this SpannableStringBuilder instance. As you append each String, call the setSpan(what:start:end:flags:) method on the SpannableStringBuilder instance passing in a BulletSpan instance for the what parameter. You can find an example of this in the appendBulletSpan(...) Kotlin extension function located here.
I think, the most elegant way of doing this is to load a WebView and put your string in it. this way, you use the common ul/li convention and you can style it at your leisure with CSS.
Use the unicode escape sequence "\u2022" in strings.xml
like so:
<string name="menu_new_trip_desc">View them in: \n\u2022 Table
I wrote a big app with thousands of string in the code.... very bad idea, because now I want to translate each string.... big problem.
Copying all strings to the strings.xml takes a long time.
Eclipse has an option to take all selected strings and put them into messages.properties.
Does this work similiar like strings.xml? When, why all people use strings.xml.
Or should is use eclipse to seperate each string and than I should copy them to string.xml?
All people are using strings.xml because this is the normal way to do it on Android. You don't have to manage the load of the strings, to call any locale function in your script.
You can see the documentation here : http://developer.android.com/guide/topics/resources/index.html
BTW, you can easily transform your eclipse generated file to an strings.xml file after the extraction.
In Eclipse you can use the shortcut keys Alt + Shift A, S to extract an inline string in to the strings.xml file via a popup dialog - might be a bit easier than doing it by hand. And as the others say, yes you should ALWAYS use the strings.xml file so that you only have to look in one place when you want to change a string, instead of having to search through all your code.
Hey, I have a lot of Strings that I use into my app, the .txt file that I use has ~14000 lines.. and each 3-10 lines are divided into sections like <String="Chapter I"> ... </String> ..
Speaking of performance/speed, should I put the sections into a Database, Or read line by line through the .txt file and check if the section number is the current one? Will this affect speed/performance?
I could also divide each ~2000 lines into a different .txt file so there would be less lines to go through. Is this a bad way of storing data? Thanks
I think sqlite would do the trick. It will probably be way faster than parsing a text file, plus you wont have to maintain the headache of your own ad hoc text database, or build a parser in the first place. Basically, use it, its way easier.
The standard way to deal with Strings in Android is to put them into res/values/strings.xml (I'm pretty sure you can have multiple String files in that directory if you like). If you are developing in Eclipse it will automatically populate the R class (the resource class) with constants that you can use to reference these Strings in your code:
R.string.mystring
Or in XML layouts:
#string/mystring
Or if you're doing something more custom you can use:
String string = getString(R.string.hello);
I would definitely choose this over a .txt file. It's much easier. All the work is done for you! Have a read of this Android article about it.
This is what a database is for. Use it.
since the beginning of my programmation, I used some special character like "<-", ""<<" im my string.xml in Eclipse while developping for Android.
All worked fine for one year, but today, i just wanted to make some minor changes and began to edit my xml files.
I get now compilation error on these characters because eclipse believe it's part of the xml blocks.
Any idea on how I could add this symbol "<" in my xml files?
Thank a lot.
Use
< for <
> for >
& for &
Another way to insert special character follow Moss guide: How can I write character & in android strings.xml by used Unicode definition:
Example:
<string name="item_unknown">\u003c Item Unknown \u003e</string>
which present in string :
< Item Unknown >
I stumbled upon this question, as I use HTML markup in my strings.
If you refer to the Android String Resources documentation,
in the section:
"Styling with HTML markup"
you will see that in strings.xml, you need to use &lt; for an opening bracket, but you can safely use backslash and a closing bracket. For example:
<b>Text</b>
can be written as:
<b>Text</b>
in your strings.xml.
If using this HTML string in your code, you can use:
Html.fromHtml( getResources().getString(R.string.yourHTMLString )
and the output will be your bold string!