How to override drawables defined in an Android Library Project?

10,074

Solution 1

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

Solution 2

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.

Solution 3

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

Share:
10,074
Eran
Author by

Eran

Here are some links to deleted posts that only 10k rep users can view, unless they are undeleted: Bitwise left shift on long Why does this output 5? "Missing return statement" Cannot Catch ArithmeticException when dividing by zero difference between (double)(1/2) and (double)1/2 Why adding one else statement is changing whole scenario? Pangram detector not recognizing every pangram Unable to resolve return type on generic factory method I get the error else without if Array size in default constructor I am having problems with this program cannot make a static reference to the non-static method TreeMap&lt;Object,Object&gt; issue Using ((Base)this).f(); instead of super.f() in Derived.f() compiles fine but causes StackOverflowError in Runtime Is Double.POSITIVE_INFINITY a true representation of infinity? Why do these "else if" statements not work? Java Arrays pass by reference How to write Fibonacci in one line? Simple calculation on for loop Exception in thread "main" ArrayIndexOutOfBoundsException: 8 Java program for multiply two matrix (with user input Bukkit ArrayIndexOutOfBoundsException on Checking for an Argument how this code works a basic Java question input.read errors in conversion Is there any way to avoid the execution of a method inside a "if" check if that check has already been "true"? Java naming convention: plural form method name - best practice - links? On which basis does SortedSet sort

Updated on June 05, 2022

Comments

  • Eran
    Eran about 2 years

    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
    Eran almost 10 years
    Not sure what the downvote is for. This answer did solve my problem. Otherwise I wouldn't post it here.
  • A. Petrov
    A. Petrov over 7 years
    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?
  • Eran
    Eran over 7 years
    @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.
  • CoolMind
    CoolMind almost 6 years
    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.