Could somebody tell me what is better in terms of performance?
Is it better to save 2 strings at string.xml, like 'abc' and 'abc:'
Or should I save only the first one and concatenate ':' when needed at Java coding ???
Very difficult to answer depending on what your strings will represent and what you need to append. Localization is also an issue, for example...
Dog // English
Chien // French
Hund // German
Using string resources allows you to create different resource files depending on the locale of the device and Android will automatically use the right localized string resource file. If all you need to do is append a single character such as : then you'll double every string for every language.
If you choose to only save the basic strings and append the character using code, then the code will be universal and you'll simply need to append the character to whatever localized word - potentially a lot more efficient.
Both from storage perspective and performance you should save only "abc";
getting extra data from disk takes far longer as some quick in-memory actions.
storing the same data twice is bad practice in general
If you have to concatenate multiple strings you should use StringBuilder - http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/StringBuilder.html
It's much faster then using '+' or '.concat()'
Related
In android, we can store string values either in strings.xml file or in some constants class as static final variable.Is there some reason for selecting one over another in some circumstances?
In a nutshell:
value for use in code: use always constants class.Advantage: codes remain integrated and your package can be utilized in other projects/contexts. You can not do that with string.xml as it is not transported with your package.
value for display in UI: use string.xml. Advantage: you can use localization to display translated texts.
Some situation may arise when both option appears viable. You will have to then decide where are its related values are stored.
As a general rule, use the strings.xml because android uses that XML to enable translating your app into different languages, which it can't do with strings that are hardcoded.
The official android guide on localization say the following;
Move all strings into strings.xml
As you build your apps, remember not to hard code any string. Instead
declare all of your strings as resources in a default strings.xml file
which makes it easy to update and localize. Strings in strings.xml
file can be extracted, translated and integrated back into your app
(with appropriate qualifiers) without any changes to compiled code.
If you generate images with text, put those strings in strings.xml as
well, and regenerate the images after translation.
Strings that are not going to be displayed to the user in any way needn't be stored in the XML, because they will never need translating, and you probably don't want the android system tampering with them in ways you might not know about during runtime.
If the string value is used to display in UI store in Strings.xml Otherwise keep it in code. There can be JSONTags, Key for different api/Thirdparty libraries.These kind of things should be kept in code itself.
strings.xml it is used for localization and needs a context to retrieve the content of a String. If you need a java constant to be accessed in different classes, you a public static final String member. If the string is a message for the user you should use strings.xml
If strings represent text readable by user, and which could potentially be translated to other languages (names of buttons, labels, notification/error messages, etc.) then they should be in strings.xml (actually, it can be any file name you like, not just "strings").
If string is some constant which is used in the app internally (bundle/intent keys, fragments tags, etc.) they should be declared in class
It depends, if it is a text string that will be translated or displayed to the user then for 118n sake, you will want to put in into strings.xml.
However, if the string is something like a server url or api code then you'll want to store those in code as a public static final String
can any one know about how to add/insert emotions/smiles to text(which ever i typed in my edit text for my notes). i have little confusion about if i want to add these smiles of type .png in to edit text type of string, is it possible? and also i have to save these input into sqlite database. normally i know to store string data taken from edit text.
but along with that text i want to also add smiles symbols where ever my cursor placed and to be store in sqlite data base. and get it back to read.
so guys any ideas, most welcome!
Try to use java (i.e. android) spannable method to implement smiley (i.e.) images for that. You will surely get it, search in google for "how to add images/smiley with java spannable method in android?" you will get good idea.
Reading your question the first thing I can think of is Mapping each image to a sequence of letters, for example :) is smiley.png etc. Now your database also has these smaller representation However while reading from the database you can convert those special character sequences to appropriate image.
For simplicity in seraching those Strings You can wrap them in some less used characters i.e. [ or { or <.
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.
How can I change the font on android to allow to show special characters like "'" or "à"?
Actually the strings that contains these characters are stored in the sqlite database.
When you load the text into your TextView, will this work for you?
textView.setText(new String(textFromDatabase, "UTF-8"));
This uses the String constructor to set the charset name. You can change "UTF-8" to a different Character encoding -- Also, look at the javadoc for String.
String(byte[] bytes, String charsetName) -
Constructs a new String by decoding the specified array of bytes using the specified charset.
The Droid font supports the "'", "à" and many others characters. I use them all the time (pt language).
Actually, I'm quite sure they support all the Basic Latin, Latin 1 supplement and the first extended latin range. They also support many others like hebrew etc., although I'm not sure if that changed between SDK versions.
You can also download the Unicode Map app in the Market to check which characters are available in your particular device. I also store unicode text in sqlite all the time, and still I don't have any problems.
One thing to consider: check that the encoding you are setting match the encoding of your source. It may be a text or a URL... an example:
BufferedReader b = new BufferedReader(new InputStreamReader(url.openStream(), MY_ENCODING));
Are you sure it's not a problem somewhere?
You should use '' instead of ' to store it into Sqlite database.
For example if you want to store 5 o'clock into database then you have to write this as 5 O''clock. Take a look here, for more information about it.
By default Android SQLite uses UTF-8.
I had this problem because when I populated the database on the first launch I used a txt file with another charset.
I have some reference data in a text file (~5MB) that I want to use with might android application.
The file is of the format:
1|a|This is line 1a
1|b|This is line 1b
2|a|This is line 2a
2|b|This is line 2b
2|c|This is line 2c
What I want to know is the most efficient way (less memory, fast, size etc.) to use this file within my application.
a.) Should I save the file as a raw resource and open and read the whole file whenever I need a certain line.
b.) Should I convert the file to XML and use XPath to query the file when ever I need to look up a value
<!--sample XML -->
<data>
<line number="1">
<entry name="a">This is line 1 a</entry>
</line>
</data>
c.) Should I just copy & paste the whole file as a static string array in the application and use that.
... any other suggestions are welcome.
[EDIT]
I will also need to search this file and jump to arbitrary keywords e.g. "line 1a".
XML will always take longer to read than simple text or CSV files. What XML gives you in the tradeoff is a highly structured and reliable way of storing and retrieving data. XML files are, as you can see in the examples above, a good 2-3x larger than the data they actually contain.
If you're sure that you're never going to run into the "delimiter" character in your simple text file, then that would probably work just fine, purely from a file speed perspective.
You have not provided enough information to answer this question. However, if I were a betting man, the answer is probably "none of the above".
I will also need to search this file
What does this mean? You are searching by some string key? By some regular expression? By a SQL-style query string where certain portions of a line are interpreted as integers versus strings versus something else? By a Google search-style string?
Each of those answers probably dictates a different technology for storing this information.
I will also need to...jump to arbitrary lines.
Why? How are you determining which "arbitrary lines" you are "jump"ing to: key? line number? byte offset? search results? something else?
And, of course, there are other questions, like:
How often is this data updated?
How is this data updated: new version of the app? download the whole file? download deltas/diffs? something else?
Is the data ASCII? UTF-8? Something else?
and so on.
Something that size that must be searched upon suggests "use a SQLite database", but some of the other answers might steer away from that solution.
If you are talking about very small amounts of data, the Android XML compiler can produce very efficient binary representations for you that you can access just like XML. On the other hand if the data is very large at all, and you need arbitrary queries, I would expect SQLlite to win out on performance (as well as flexibility). A small benchmark should be easy to write and would give you a good idea as to the basic tradeoffs involved.
Flat-files would be a last option, imo, but could work if the file isn't very large.
If you define efficiency as (less memory, fast, size etc.), a flat or delimited file will be faster to load and save.
However, people use XML because they are willing to trade some of that speed for XML's greater flexibility and ease of use.