Mars Mars - 1 year ago 81
Android Question

call getString(R.strings....) from class?

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?

Answer Source

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){

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download