androidandroid-xmlandroid-resourcesandroid-library

How to reference a color in a android xml resource that is defined in a lib?


I have an android fragment app/src/main/res/layout/fragment_main.xml that references a color bg_line defined in app/src/main/res/values/colors.xml

res/values/colors.xml :

<resources> <color name="bg_line">#E7E7E7</color> ...

fragment_main.xml :

<RelativeLayout android:background="@color/bg_line" ... >...

I want to refactor my existing working code and move color bg_line into a different android library module mylib/src/main/res/values/colors.xml that uses

mylib/src/main/manifest.xml :

<manifest package="de.k3b.mylib" ...>

My Question: how can i reference the color bg_line in fragment_main.xml if bg_line is defined in modul :mylib

in https://developer.android.com/guide/topics/resources/color-list-resource i found

> resource reference:
>    In Java: R.color.filename
>    In XML: @[package:]color/filename 

i tried

I am using Android Studio (Electric Eel | 2022.1.1 Patch 2)


Solution

  • the module that contains the layout fragment main.xml must implement (gradle) the module (:mylib) where the color definition is found. So it's just referencing the color as "before" (android:background="@color/bg_line").