stack man
stack man

Reputation: 2413

Android: Getting string from strings.xml in Java code

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?

Upvotes: 2

Views: 3415

Answers (2)

crashOveride
crashOveride

Reputation: 837

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

Upvotes: 5

Andreas Brunnet
Andreas Brunnet

Reputation: 732

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

Upvotes: 3

Related Questions