I am writing a plugin for a third-party app. The plugin app loads a library through maven that executes majority of the tasks. Imagine it as the plugin app handles everything UI related (clicks, drawing, etc), while the library handles the logic.
The thirdparty app gives me a context and resources variables. If I execute this in the plugin:
resources.getString(id) // Returns String
context.getString(id) // Resources$NotFoundException: String resource ID #0x...
but if I pass context and resources as variables into the library:
resources.getString(id) // Resources$NotFoundException: String resource ID #0x...
context.getString(id) // Resources$NotFoundException: String resource ID #0x...
Now, the id is from the library package - com.my.lib.R.string.some_string
Why cant I access the resources of my own library?
How do access the resources without any other context or resource variable?
Related
Are int value of R.id.view is always same?
I tried printed it in two devices and it was same.
I also changed name of id but still the value was same.
But will it stay the same in all scenarios?
For any particular app resource, the particular R class associated resource ID will be the same in all scenarios during runtime as this resource ID is generated by aapt during compile time.
From Android Documentation:
Once you provide a resource in your application, you can apply it by referencing its resource ID. All resource IDs are defined in your project's R class, which the aapt tool automatically generates. When your application is compiled, aapt generates the R class, which contains resource IDs for all the resources in your res/ directory.
Thus printing it in two devices will result the same int value, provided that the same apk is used. Also note that the resource id is final in the respective R subclass.
From this answer:
Note that aapt by default makes no attempt to keep these identifiers
the same between builds. Each time the resources change, they can all
get new identifiers. Each time they are built, a new R.java is created
with the current identifiers so your code gets the correct values.
Because of this, you must never persist resource identifiers anywhere
where they can be used across different builds of your app.
Any changes to the resource might make the resource get an new R class resource ID.
Based on the way in which the R class Resource ID are calculated, as described in that answer, I think since you only changed the name of the XML resource ID, and didn't change the type of the resource nor the placement of the respective View object declaration in the respective XML file, the same R class Resource ID is being calculated.
A View ID is generated during Compilation, so Yes the ID will be the same in each Device if the APK is the same. Pay attention that "different compilations could generate different IDs".
I am trying to search for drawable (pictures) in a resource file in android from my library project Is it possible without the context of the of the actual android project or do I need to add that in? Also how would I be able to find an ID through my library project by using a string as the name of the ID and then convert it to the appropriate integer to set the background resource.
This is what I have tried so far: This works but only looks at the rescources in the library project, I need to look at the resources in the current application project
try
{
Class res = R.drawable.class;
Field field =
res.getField("string_ID_I_want");
drawableId = field.getInt(null);
}
The actual drawable resources are compiled into your app, so accessing them is no different than accessing normal app resources.
Supposing the ID of the drawable in the library is big_picture. Then R.drawable.big_picture is the ID of the drawable you want to load. You don't have to use introspection. If R.drawable.big_picture is not available, there's something wrong with your project setup (or a compile error).
Context context = getApplicationContext();
Resources r = context.getResources();
r.getDrawable(id); //pass your drawable id in it, could be R.id.whatever
I have a question about inheriting resources from android libraries.
Assume you have an android library project and in that library you put some resources (let's say strings for the moment) under the package name com.libexample
Now in an android project under the package name com.example, I reference the library I created earlier.
If I want to use one of the strings of the library I can get it by using
getString(com.libexample.R.string.test_string);
My question is, is it possible for a string resource in my main project to get assigned the same integer ID as a string in my library? Cause if it is, then the above code statement would in fact be equal to :
getString(R.string.new_string);
assuming that the new_string recourse was assigned the same ID as the test_string resource.
Apparently the compiler adds the resources under the same generated R.java file automatically which prevents any conflicts.
I am trying to separate an application into an app project and a library project (besides moving it from Netbeans to Eclipse). The app will contain resources that are used by the library - for this, I had read on Stackoverflow that we can bundle the resources in the library project and then override them in the app project.
But when I did this, I am getting the error:
...\res\values\attrs.xml:5: error: Attribute "pageBackground" has already been defined
Am I doing something wrong here? Any of my assumptions is faulty?
Thanks,
Rajath
I think I had similar problem when I tried to create a kind of 'configuration file' which was placed in application's resources and was meant to alter behavior of library it used. What I found working was using getIdentifier method from Resources instead of refering directly to R class:
final int resId = getResources().getIdentifier("my_resource", "raw", getPackageName());
You can then use the identifier as normal resource ID, e.g.:
if (resId > 0) {
final InputStream is = getResources().openRawResource(resId);
// ...
}
The idea was to handle both situations: when the file was present in app's resources or when it was not. But I think it should also work in your case of "overriding" the resources from library in application, thanks to getPackageName providing appropriate package name for resources' identifiers' resolving.
I am building an Android app, and I have a problem to get resource from string.xml.
I need to have a URL in String.xml that will be used several times through the app.
I tried Resources.getText("my_url"), but this does not work. Eclipse complains when I use this.
Do you think this is the best way to do ?
What you probably want is:
String myUrl = getString(R.string.my_url);
The getString() method is in Context which means that it's available directly in your Activity class since Activity is a subclass of Context. (The getString() method is also in Resources but it's easier to call on it directly on your Activity.)
What happens with your XML resources is that each is given a unique integer ID and this is added to the automatically generated R class as a public static final int. You then use these IDs to reference the resources. Have a look in the gen folder in your Eclipse project and you'll find the R class in there.
Do you ever refer this page: https://developer.android.com/guide/topics/resources/available-resources.html ?
If you want to retrieve the String represented by a resource ID, you can call the Context.getString() method.
Or, you have to post Eclipse's complains.