user3013767
user3013767

Reputation: 137

Getting Resources from another package

Inside src/ I have the following packages:com.app.animals, com.app.animals.threads, com.app.animals.games

When I try to get my proyect resources (drawable folder) from a class called "CarreraJuego.java" inside com.app.animals.games, it says that 'R cannot be resolved to a variable'

How can I access resources folder from that package?The class is a surfaceview

Upvotes: 4

Views: 8350

Answers (2)

Smittey
Smittey

Reputation: 2490

All you need to do is import the R.java file inside the classes of packages outside of the main package (the one with the generated R file)

Upvotes: 0

Manitoba
Manitoba

Reputation: 8702

Here is a simple and quick way to achieve it:

try
{
    PackageManager manager = getPackageManager();
    Resources resources = manager.getResourcesForApplication("com.example.packagename");
    int resId = resources.getIdentifier("drawable_id", "drawable", "com.example.packagename");

    Drawable myDrawable = resources.getDrawable(resId);

    // Do something
}
catch (Exception e)
{
    e.printStackTrace();
}

Change com.example.packagename width the package name of the app you need the resources. Change drawable_id with the resource Id. Change drawable to the folder name you want (drawable, layout, values, ...)

Upvotes: 11

Related Questions