Settings variable in Android - android

Is there a way to have an global settings variable for an android application, which is accessable as well from any help java classes without giving them context?!
I try to explain what I mean.
I have an application version as string value in strings.xml
I can get its value from every android activity, but not from help java classes withought giving context
What I do now, is saving it in a static variable of my first activity, but it seems, that sometimes it will be erased and set to null.
May be I do something wrong?!
Sorry for newbie question.
And thank you in advance,
Mur
P.s.
I wrote a small tutorial for this topic, to show the solution.

A variable declared as public, static, and final will be visible to all of your classes and never get erased.
public static final String VERSION = "1.2.3.4";

You could make a public static variable in your application class that you fill with the value from strings.xml in the onCreate method. The application class is a singleton and will be the last thing that is killed as part of your app so it will always be there and if you make it public static there will be only one instance.

I'm guessing that you have a JAVA class for some common utility functions. You get the value of your string using a context in your Activity/Service and then pass in that value to the JAVA class function as a parameter.

Related

Android Studio cannot resolve symbol

I'm just starting with programming Android apps and was working through the developers guide on android.com. When trying to display a text on a second activity it says it cannot resolve the symbol EXTRA_MESSAGE as you can see here:
As far as I can tell I did every step like the guide says. I also tried copy and pasting everything but it still doesn't work. What am I missing?
You're doing a static import of extra message from some random class in MainActivity. That's wrong, don't do that. Define EXTRA_MESSAGE as a public final static String, with whatever value you want (I'd suggest "message") in your MainActivity.
There is two ways of solving it.
1) Use same static variable (Its a dirty way). At DisplayMessageActivity use android.provider.AlarmClock.EXTRA_MESSAGE.
2) This approach I recommend you. Create public static final String field at MainActivity, remove android.provider.AlarmClock.EXTRA_MESSAGE, and use MainAcitivity field at both classes. Content of this variable does not matter as long as it is unique extra key.

Is there anything in Android like the appSettings in Asp.net?

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

Setting field from resources on declaration on android

Is there any problem with code like this?
public class SomeClass extends View {
private final float someFieldVariable = getResources().getDimension(R.dimen.someVariableValue);
....
}
I think this is dangerous code.
The context gets wired the time on of the super-constructors is called. The initialization of someFieldVariable depends on the context and maybe is done before super is called.
So there is a chance the context is not wired because of the compiler not being smart enough and then your initialization will fail with an uncaught exeption. This will cause your app to crash.
Even if it works, I think it is bad style to rely on how the compiler does his work.
You should initialize it in your constructors instead to make sure the super-connstructor has been called before or just get the value from resources as you need it.
I also think there is no big advantage in defining a local variable for holding a resource value. It is like defining a variable to hold another variable, which is even final. It's just reasonable if you need the value very often and every processor-cycle counts.

Best way to use getString()

I'm having some problems on where and how to place and call the getString().
I tried at the beginning of my activity where I usually define it but always get an error. Should it be only after onCreate()?
Some of my strings I use it very often so I don't want to use getString() every time I use them.
Is this the correct way?
private String helloWorld = getString(R.string.hello_world);
Thank you very much! :)
at the top of your file do:
private String helloWorld;
And then, in onCreate(),
do:
helloWorld = getString(R.String.hello_world).
The problem is that getString needs a reference to the activity context, which has not been associated with your class until onCreate is called.

Eval function in Dalvik

If I know a variable's pattern such as R.id.edit_x where x (1..N), how can I get a reference to a given EditText, like findViewByID(R.id.edit_1). Is there something like an "eval" function in Dalvik ? Thanks.
Try Java reflection. Discussion on retrieving static final fields via reflection is here - Accessing Java static final ivar value through reflection
hoha's answer is good. Another thing you can do is create a look-up table that maps 1..N to the resource IDs. (Presumably you know all the resource IDs ahead of time.)
maybe, you can check roboguice. it is a ioc framework for android and it's realy easy to use. i copy some code from the sample from the project to show how to use it:
public class AstroboyMasterConsole extends RoboActivity {
#InjectView(R.id.self_destruct) Button selfDestructButton;
#InjectView(R.id.say_text) EditText sayText;
#InjectView(R.id.brush_teeth) Button brushTeethButton;
#InjectView(tag="fightevil") Button fightEvilButton; // we can also use tags if we want
}
then you can you these injected variables in your code!

Categories

Resources