• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Rob Spoor
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Holloway
  • Piet Souris
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Himai Minh

Java: Using string resource in external class

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everyone, during development I needed to use string resources. I can fetch them from strings.xml for the layout xml file and for the MainActivity.java file (using getString(R.string.name)). But I can't get to use them in an external class file (which is being used to handle all the functions of the MainActivity file).

I have gone through StackOverflow forum and tried many answers but in vain. I tried using:

1. Resources.getSystem().getString(R.string.name)
2. getResources.getString(R.string.name)

I have a orderProcessing button in mainActivity.java which gets processed in orderSummary function in mathCalculator.java. Everything works out fine when I use hardcoded strings. I just can't get to use the string resources from strings.xml to reference them.

Please help out in this regard. Many thanks
 
Ranch Hand
Posts: 39
Android Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The library you use from inside the activity is not dependent on the activities nor on the resource definitions. The only way to share a string between the activity and an external library is to provide the string as a parameter in a method call.
 
Master Rancher
Posts: 4460
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also posted here:  https://www.javaprogrammingforums.com/android-development/43936-java-using-string-resource-external-class.html

Note: This is an Android question
Staff note (Paul Clapham) :

Moved to Android forum (thanks Norm)

 
A lot of people cry when they cut onions. The trick is not to form an emotional bond. This tiny ad told me:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic