19

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?

Eran
  • 387,369
  • 54
  • 702
  • 768
  • 2
    I voted for your question, because it seems very useful, especially with your own answer, provided in comments. I think overriding drawables is as important approach as overriding methods, and makes things much easier and streamlined. There is no need in any excessive parameters and coding. – Stan Jun 19 '12 at 21:14

3 Answers3

6

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.

Zephyr
  • 6,123
  • 34
  • 33
  • Please, write, in what file should we place `drawable_base_style_app` style? Where `refs.xml` is kept? Compilation is failed because `drawable_state_selected_style` is duplicated. – CoolMind Sep 07 '18 at 14:06
1

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

Snicolas
  • 37,840
  • 15
  • 114
  • 173
-1

As I wrote in a comment above, I managed to solve my problem by cleaning the projects and re-building them.

Eran
  • 387,369
  • 54
  • 702
  • 768
  • 2
    Not sure what the downvote is for. This answer did solve my problem. Otherwise I wouldn't post it here. – Eran Sep 04 '14 at 16:53
  • 4
    Your "solution" not provide such details what you exactly do while you "clean the projects and rebuild them". Is you remove some resources by your hands? Is you perform gradle's clean task? – A. Petrov Dec 02 '16 at 09:33
  • @StAlex It has been a long time since I posted this, so I don't remember the details, but I believe I cleaned the projects in my Eclipse IDE and rebuilt them. "clean" is an operation you can perform on an Eclipse project. – Eran Dec 02 '16 at 14:22