• 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
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

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: 50
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: 4465
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)

 
Hey, I'm supposed to be the guide! Wait up! No fair! You have the tiny ad!
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic