Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to override drawables defined in an Android Library Project?

I have two Android applications with a similar functionality but different drawables and layouts.

I'd like to put all the code and resources in a library project and override some of the drawables and layouts in the two applications that would reference that library.

I created an application that references the library. I copied all the resources from the library to the application and changed some of the drawables. I also changed some of the string resources. When I view one of the changed layouts in the layout editor in Eclipse, I see the correct overridden images.

When I launch the application I see that the string resources I changed in the application are displayed correctly (the library strings are overridden by the resources of the application), but the drawables of my ImageViews are all taken from the resources of the library.

I also made changes in some of the layout resources (moved and resized some images). When I launch an activity that uses the changed layout (an activity whose source code is in the library), I see the new (application) layout with the old (library) drawables.

I tried defining the drawables of the ImageViews in two ways :

  1. in the layout xml : android:src="@drawable/image_id"

  2. in the code of the activity :

    ImageView view = (ImageView)findViewById(R.id.viewId);
    view.setImageResource(R.drawable.image_id);
    

In both cases the image I see is taken from the resources of the library project and not the application.

If I don't manage to solve this problem, I'll have to duplicate all the code in both applications, which would be a shame.

Am I doing something wrong?

like image 927
Eran Avatar asked May 21 '12 21:05

Eran


People also ask

What are Android Drawables?

A drawable resource is a general concept for a graphic that can be drawn to the screen and which you can retrieve with APIs such as getDrawable(int) or apply to another XML resource with attributes such as android:drawable and android:icon . There are several different types of drawables: Bitmap File.

How do you add Drawables?

Drag and drop your images directly onto the Resource Manager window in Android Studio. Alternatively, you can click the plus icon (+), choose Import Drawables, as shown in figure 3, and then select the files and folders that you want to import. Figure 3: Select Import Drawables from the dropdown menu.

What is a drawable resource in Android?

A drawable resource is a general concept for a graphic that can be drawn to the screen and which you can retrieve with APIs such as getDrawable (int) or apply to another XML resource with attributes such as android:drawable and android:icon . There are several different types of drawables: A bitmap graphic file ( .png, .jpg, or .gif ).

What is drawable shape in Android?

Shape drawable. A drawable resource is a general concept for a graphic that can be drawn to the screen and which you can retrieve with APIs such as getDrawable (int) or apply to another XML resource with attributes such as android:drawable and android:icon . There are several different types of drawables:

What is a layerdrawable object?

A LayerDrawable is a drawable object that manages an array of other drawables. Each drawable in the list is drawn in the order of the list—the last drawable in the list is drawn on top. Each drawable is represented by an <item> element inside a single <layer-list> element. The filename is used as the resource ID.

What is drawable class in Android Studio?

Android Drawable Class extends to define a variety of specific kinds of drawable graphics which includes BitmapDrawable, ShapeDrawable, PictureDrawable, etc. These classes can be extended to define customized drawables.


2 Answers

Based on my experience, you can replace the drawable resource in the library module by using style and refs.xml.

First you need to declare the drawable resource in a style, like

<style name="drawable_base_style">
    <item name="android:src">@drawable/base</item>
</style>

then in the app (module or project, depending on your IDE), redefine a style

<style name="drawable_base_style_app">
    <item name="android:src">@drawable/app</item>
</style>

and use refs.xml to point the new style.

<item name="drawable_base_style" type="style">@style/drawable_base_style_app</item>

then the last step : cross your fingers.

like image 60
Zephyr Avatar answered Sep 19 '22 09:09

Zephyr


Don't copy-paste things, review your design in order to give the Drawable's resourceId as a parameter to your object.

like image 38
Snicolas Avatar answered Sep 19 '22 09:09

Snicolas