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.
Related
I want to use a particular string found in the Values folder into normal classes. This is for my localization process with different languages.
How would I do this?
Use the getString() method on a Context (such as an Activity), specifying the key in this form: R.string.you_key
If you need to get a String outside of an Activity, like in a helper class or something, just make sure you pass in a reference to a context.
Access the value by calling getString(R.string.your_string);
Ex.
String myValue = getString(R.string.your_string);
obviously replace "your_string" with the actual name you gave the string
I have this in a normal activity:
tv.setText(this.getString(R.string.week1));
I think
remoteViews.setTextViewText(R.id.widget_textview,"something");
is the equivalent to
tv.setText("something");
How do I getString from strings.xml in AppWidgetProvider?
There is probably a really easy solution but I tried using google and I'm new to android...
So, when you get a hold of a Context you can just call
context.getString(resIdHere);
onEnable(), onDisable(), onReceive() and onUpdate() methods all pass context reference in to their definitions by default.
Hope this helps,
-serkan
I do not know how to "How do I getString from strings.xml in AppWidgetProvider?" but I think you try to get string from strings.xml
and that could be done like this.
String s = getResources().getString(R.string.-yourString);
i found this problem some time ago, but i solve it using this: getString(), or this: getResources().getString()
but now, for this case, it doesn't works, i think it's because i need to get the string values on a NON ANDROID ACTIVITY CLASS. I need the resource values on a remote connection class, that doesn't extends any kind of activity or service.
how i can acces to the variables from my strings.xml on this normal class?
this is the code where i get the error (it gets an integer, and not the string value)
String a =R.string.totalpermission;
Take a look at these two answers (are the same XD):
How to obtain AssetManager without reference to Context?
How can I get a resource content from a static context?
Just an advice: try to read some basic concepts... it seems you don't understand what the R class is and how to use it. Trust me, you waste less time studying than trying to figure out how things work.
I'll add something to existing answers since I found it very useful.
To get your strings you have to use a Context. Your activity will work just great.
String string = getString(R.string.myString);
But if you have something more complex... for exemple
R.string.result -> "You %1$s %2$d cats"
String result = getString(R.string.result, killed ? "killed": "saved", count);
That would give you a result like that:
You saved 10 cats or You killed 2 cats... and so on. You can pass parameters and positional arguments in strings will get replaced by your arguments in getString.
All Android resources are referenced via a resource ID, like R.string.totalpermission. You can see those numbers in R.java (although there's no reason to ever do that).
In cases of strings, you can easily get those using Context.getString. Bonus: You can even pass optional arguments and add dynamic strings that way. You always have a context - how are you getting called? If you really don't have a context, you can create one for the package your resources are in.
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.
I need to get the context to be able to get a resource. Like this:
getApplicationContext().getResources().openRawResource( R.raw.texture );
I've seen the getApplicationContext() in the android documentation but when I try to use it in the above code it doesn't work - it doesn't exist.
I can send the context through functions to get it to where it's needed and it works. However, I find it cumbersome to send a variable through many functions that doesn't need or use it. Then I would rather just try to get it in the function that do. But the getApplicationContext(), as in the android documentation, doesn't work - http://developer.android.com/reference/android/content/Context.html
So how do I get the context so I can read resources? Or are my only option to send it through all my functions?
getApplicationContext() is a method of a Context. You have to have a context to get the resources. That's just how it works.
Just makes sure you're not storing a reference to your context anywhere or you could cause a memory leak.
In your activity you can use getResources() method immediately. For example
getResources().getDrawable(R.drawable.logo);
If you want to get some resource in some other class not in activity, you should pass context link in other class from your activity. For example
Util.convertLogo(this)
or
Util.convertLogo(getApplicationContext())