Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Override resources in library android

I'm using my own library. In this library I defined some resources:

colors.xml (library)

<color name="colorPrimary">#000000</color> 

In my app (that uses the above library). I want to override that color primary.

colors.xml (app)

<color name="colorPrimary">#ffffff</color> 

I actually want to override that attribute so that the library (and my app) use the overrided one, not the declared one from library. It works fine but Android Studio keeps yelling out that:

Overriding @color/colorPrimary which is marked as private in my_lib. If deliberate, use tools:override="true", otherwise pick a different name". That makes me think that this is not a good approach.

I also try to add tools:override="true" to the resource:

<resources xmlns:tools="http://schemas.android.com/tools"        tools:override="true"> 

but the warning still there.

Is there any wrong with what I'm doing?. I can not pick another name because I want the library to use the overrided values.

I'm using Android Studio 2.1.1 and gradle 2.1.0 version.

Thanks.

like image 786
Paul Avatar asked May 18 '16 03:05

Paul


People also ask

How do I override resources in Android?

If you press alt-enter on the line with the warning, AS will offer to insert the override attribute for you and will put it in the correct place. It works as expected now. Thanks.

How to open resource file in Android Studio?

Click the target app module in the Project window (while in either the Android or Project view), and then select File > New > Android resource file. Fill in the details in the dialog: File name: Type the name for the XML file (does not require the . xml suffix).

What are resource files in Android?

Resources are the additional files and static content that your code uses, such as bitmaps, layout definitions, user interface strings, animation instructions, and more. You should always externalize app resources such as images and strings from your code, so that you can maintain them independently.

What is resource value in Android Studio?

In Android, almost everything is a resource. Defining resources that you can then access in your app is an essential part of Android development. Resources are used for anything from defining colors, images, layouts, menus, and string values. The value of this is that nothing is hardcoded.


1 Answers

Move the tools:override to the color tag.

There's nothing wrong with this. Android Studio is just warning you, incase it wasn't deliberate.

If you press alt-enter on the line with the warning, AS will offer to insert the override attribute for you and will put it in the correct place.

like image 59
alex Avatar answered Sep 17 '22 13:09

alex