What is better approach of the following in designing an Android app
To place all the String constants in res/values/Strings.xml
or
Create a class like Constant.java where all Strings are public static final?
Choosing which one of them is more efficient?
I imagine a constants class would be more efficient.
However, speed shouldn't really be an issue in either case. I would recommend organizing based on what makes sense.
Constants class
Put strings constants that will be used internally, like database column names or other keys.
strings.xml
Put strings that are displayed for the user. This way you can take advantage of localization, etc.
Related
My app uses a lot of static text and I'm trying to find an optimal way to persist and display that text. For now, I don't need to focus on localizing the text so, all the text goes into the strings.xml and that presents a lot of formatting nightmares.
Of course, it is not 100% static content, I sometimes have dynamic values in there which in my case can stay within strings.xml so, what is the right way for persisting this static text?
Static text content is exactly what you want to use strings.xml for, and you automatically get the added bonus of easier localization as you can have different strings.xml for the different languages. No code changes required, just different XML files.
Dynamic content is going to be content which changes based on user input. You can still use strings.xml to store the static portion (if any) of that dynamic content. Like the "format" string you may pass to String.format() or something similar.
Use the resources support for this, it is exactly what it was intended to do (and do it efficiently.)
Your comment is contradictory; you say:
For now, I don't need to focus on localizing the text so, all the text goes into the strings.xml
if you don't need Localizing, then why are you using strings.xml?
Of course, the answer is because regardless of localizing, strings.xml is the perfect place for this.
I don't know what kind of nightmares you have with it, but it's not different from any other string:
E.g. of a strings.xml:
<string name="refresh">Refresh</string>
<string name="order_placed" formatted="false">Order Placed: %s</string>
You can later use the same formatter for them:
getString(R.string.order_placed, "3pm")
will output:
Order Placed: 3pm
If you need new lines…
<string name="error">Something bad happened.\nPlease try again.</string>
will output:
Something bad happened.
Please try again.
And so forth.
Additionally, if you have trouble naming your resources, I've been following more or less this idea and despite the shortcomings described at the bottom, they haven't been a big deal with Android Studio fancy refactoring tools.
You could create a class called StaticBuffer or something with a static String array as a data member.
class StaticBuffer
{
static String array[];
}
Then you could initialize it in your onCreate() or any other function and use it.As it is static it's values will reflect the changes that you make everywhere.
Eg :
//Initialization
StaticBuffer.array=new String[10];
//Usage
StaticBuffer.array[0]="Item1";
PS: I got this idea from a friend of mine. :)
I am wondering if you should use strings.xml instead of global constants. I learned that global variables should be avoided but then again strings.xml are probably not ment to be used like this?
Are there any advantages / disadvantages using one or the other?
I am pretty sure that hardcoded strings like the following is not a good way.
putExtra("extraKey", extra);
With strings.xml or Constants you have a spellcheck and autocompletion.
A typical line with R.string could look like this.
intent.putExtra(getString(R.string.first_player_for_intent), firstPlayer);
in comparison to the
intent.putExtra(MyClass.first_player_for_intent), firstPlayer);
If you should use constants, in what class should they be located?
I wouldn't use res/strings.xml to store constants. You might want to access their value even though you don't have a context. Also, your keys don't need to be localized. Regarding the place where you should store it, it is up to you, and imo, it is just a matter of taste. I usually avoid having a dedicated class just for constants, but I declare it where it imo belongs. For instance, if you have a class called Player, I would put all the constants Player related there.
strings.xml is there for a purpose and primary purpose of it is to support localization.
you should not be overloading this system with constants which are not relevant for localization.
As Blackbelt correctly said you may need to access your constants even without context so that's another reason.
I need to store a few values that are going to be use in several activities. I know could easily create a Constants class , a Interface or extend the Application class and put those constants there.
But not wanting to reinvent the wheel, I want to ask you if Android have something like appSettings in Asp.net or the Application Descriptor that we had in the old Java Me.
Thanks in advance.
Since you are using constants, there are two solutions:
Create an interface or class with public static final fields.
Create a XML resource file. See Resource Types for details about the resource types.
Personally I prefer 1 for constants that are used in my Java code because 2 requires a Context object and calls to getResources(). This just makes for more code than is really necessary.
Note that I don't give SharedPreference as a possible solution. This is because SharedPreferences should be used to store calculated or user data rather than constant values.
You could use SharedPreferences
Complete Docs
I have a lot of Intent to send message between activities and services in my APP.
Usually they will have some Object to pass by bundle.putString(key, someObject) and bundle.getString(key, someObject).
I want these keys to be well organized so I will be clear with messages being sent.As I think, they should be constants in Java, but for android, would it be better to put them in res/values/strings.xml named as key_xxx_xxx.
Thanks for any answers or comments.
Well, I thought about this matter a few days ago.
Use Java is better than strings.xml, like Android' s permissions are all Java public static final field. If you hava a lot of constants, you can also prepend a package name to be clear and identical, which strings.xml cannot supply.
If your xml file use the the key, I think is better to put the key in the strings.xml. For example, your settings xml file need a string key, and your java code also needs it, if you just put that key in java, you need to set text literal to the settings xml file, that may not good...
in short, it depends on the key used in java or xml or both.
hope it helpful!
It would be good to keep them as static constants. Make a java file as Utils.java and keep all the names there as Static Constants.
Something like this:
public static final String FIRST_KEY = "my_first_key";
I usually place my keys on an xml and access them with R.string.key_name but someone make me notice that I could have inline strings in the code.
I feel that I might use that key in different places and if I change its name I would just rename in the xml but perhaps that doesn't make too much sense with keys.
What do you think?
Your question conflates two different questions:
Does it make sense to have a single definition of your key?
Does it make sense for this single definition to be within an XML file?
The answer to point 1 is clearly "yes". Duplicating strings used as keys (which need to be the same everywhere for your code to function correctly) is a recipe for pain and heartache.
But what benefit does putting the key in an XML file give you? You're just adding "noise" to your code, and ensuring that whoever reads it has to find, understand and look in at least one additional file.
public static final is the way to go.
Inline literal strings will be a massive pain to change if they get scattered through the code. Localizing them in one place with either the strings.xml or a defining a public final static variable will probably save you a headache later.