Issue
In my Java class I want to use a string resource from strings.xml.
For that I want to use something like this:
getString(R.string.address)
If my class is an activity then it's working.
But how can I use it if my class is a simple Java class?
Solution
A class does not have a context and to use a string resource a context is needed. So just call the class from an activity and give a parameter context
and within your class constructor just use that context to get the string resource.
In your custom class you need to import the R namespace for the project to get the resource Id.
import com.myrandomapp.R;
Then to get the actual string
context.getString(R.string.COOL_STRING)
Answered By - Jyosna
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.