2

I have MyAppProject as android application project and MyLibProject as android library project. I have strings.xml(2 strings) and one xyz.java class in MyLibProject. xyz.java class has 2 get methods to access the strings.xml values.

I have added reference library project "MyLibProject" in "MyAppProject" application project.

For some usecase, I will check whether Library project xyz.java class is in classpath then get the values from xyz.class.

How to access the strings.xml texts in xyz.java class get method?

2 Answers 2

1

As far as I'm aware, you should be able to access any references via a fully qualified name for the R file you're attempting to access, via the Context. Something like (untested):

Context.getString(com.YOUR.LIB.R.string.YOURSTRING));

Failing that, you could possibly import the com.YOUR.LIB.R file directly into your activity.

Hope this helps! And I hope that I understand what you're trying to achieve. If not, please provide more information and possibly some code you've already tried, may help explain your problem :)

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

4 Comments

I have simple class in my library project.I dont have any activity class and i dont have context.Is there anyway to achive the above issue without context?
You have no activity at all? Not even in your main application? If so, what is the purpose of using strings.xml, over just in code references with getters/setters? I'm a little confused as to where you're actually attempting to reference these values from.
i dont have activity in library project.In main application i have.I am using strings.xml for internalization(Globalization) purpose.
I tried creating one App class which extends android Application class.And tried to get the context in App class and it is null.
0

This question has been very old, but creating Android libraries will never get old. I have been working on creating Android libraries for a few days now, and myself encountered this issue of not been able to use res directory resources in library's java files due to some reason. After 2 days of searching, I found that I was missing package declaration in my library's manifest file.

So, to make it working, check if you have declared package in manifest.

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.yourpackage">

</manifest>

It worked for me after this change, now I can use resources in library's java files. I hope it works for you too.

Comments

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.