15

Is there a way to use the getString method from a seperate class?

I have a string stored in my strings xml, I'd like to use that string in an object... but the method isn't even available in the object...

any tips?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Mars
  • 4,197
  • 11
  • 39
  • 63
  • 1
    Does this answer your question? [getString Outside of a Context or Activity](https://stackoverflow.com/questions/4253328/getstring-outside-of-a-context-or-activity) – lcnicolau May 01 '20 at 04:25

7 Answers7

21

getString() is a method of the Context class¹. If you need it inside a seperate class (that does not extend Context), it's usually best to provide it as a seperate argument to the method that needs it.

Example:

public void logString(Context c, int stringId) {
    Log.d("TAG", c.getString(stringId));
}

One thing is important: Never store the context inside the separate class.
Provide an argument. Otherwise you will leak memory and disrupt the whole android lifecycle if the object that stores the context lives longer than the object where the context originally belongs to (e.g. an activity).

¹ getString() can also be used from the Resources class - which you can get via Context.getResources()

Community
  • 1
  • 1
  • 1
    thanks I ended up passing the context to the object because I might be expanding it to use more strings, so I don't want to be passing to much parameters since the object gets it's data from a webserver... – Mars Sep 17 '11 at 12:23
  • Getting data from the web and using context: Depending how your class works, I'd suggest you might rather build a [`Service`](http://developer.android.com/reference/android/app/Service.html) (which extends Context). This works great with the android lifecycle and provides what you need. –  Sep 17 '11 at 12:27
12

the solution here is to make sure your object has a reference to the application context

Class Swag{
    private Context ctx;
    public Swag(Context ctx){
       this.ctx = ctx;
    }
    public void doSomething(){
        String something = ctx.getResources().getString(R.string.somestring);
        ...
    }
    // or like this
    public void makeUpperCase(Context appContext){
        appContext.getResources().getString(R.string.super_string_swag_yolo);
    }
}

obviously you'd have to supply the context when creating an object or when caling the method

Mars
  • 4,197
  • 11
  • 39
  • 63
10

resouce file: values/strings.xml

<resources>
   <string name="app_name">App name</string>
<resources>

java

import android.content.res.Resources;
Resources.getSystem().getString(R.string.app_name);//result : App name
iCrazybest
  • 2,935
  • 2
  • 24
  • 24
2

edit: The below will NOT work. I read this on another site and assumed it worked, but I just tried it in my app and kept getting an error. Problem is, it will compile but you will get a runtime exception.

This will work from any java class:

import android.content.res.Resources

Resources.getSystem().getString(R.string.blah);
devo
  • 1,290
  • 1
  • 15
  • 28
1

if you cannot pass a context as parameter, create another class, where you put all your static data.

example :

public class StaticData {
    public static String BASE_URL = "https://stackoverflowrocks.com";
}

and get that string from your other class by calling directly StaticData.BASE_URL

nice and clean.

JPhi Denis
  • 335
  • 5
  • 5
-1

This works, but for SYSTEM resources only:

import android.content.res.Resources
Resources.getSystem().getString(R.string.blah);

Reference: https://stackoverflow.com/a/40917607/8994882

Samuel Liew
  • 76,741
  • 107
  • 159
  • 260
nebykoff
  • 1
  • 1
-3

Try this in your java file:

String myString = getResources().getString(R.string.MY_STRING)

Now use this string object.

Shafi
  • 1,368
  • 10
  • 24
  • getResources() is a method of Context class. You cannot use it without a valid Context reference – Jose_GD Nov 06 '12 at 14:08