2

First of all, please note I´ve been trying to search and existing answer to this question. I found this question, but is not helping.

I am simply trying to get strings from regular Java (domain) classes within an Android app, but I am not able to get them by using getResources().

Is this possible? (I´d like to avoid creating a new strings.xml alike file in order to centralize all the strings within the app)

Thanks in advance.

EDIT: Am I forced to pass Context to every single Java class? Is there any way to avoiding it?

Community
  • 1
  • 1
stack man
  • 2,303
  • 9
  • 34
  • 54

2 Answers2

5

Pass context to the Java classes in their constructors. Assign this context to class variable. Then you can do mContext.getResources().getString(---)

You will have to also import the R.java in this class

crashOveride
  • 839
  • 6
  • 12
3

You need to have access to a Context object (passing it so a non android class e.g.).

Using this Context you can call context.getResources().getString(R.string.mystring);

Andreas Brunnet
  • 722
  • 6
  • 19