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?

5
  • 1
    Make a class and define static/non static String values to access them. Commented Apr 1, 2016 at 8:07
  • 1
    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 Commented Apr 1, 2016 at 8:09
  • @ArjunGurung is there any other way? Commented Apr 1, 2016 at 8:13
  • 1
    @stackman if you dont want to pass context and use getResources which is best method to use, making a class is only method i know.. Commented Apr 1, 2016 at 8:18
  • @ArjunGurung thanks man Commented Apr 1, 2016 at 8:20

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

1 Comment

please see my Edit
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);

1 Comment

please see my Edit

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.