I am creating an android library and I want to know the best way to pass colors from the main app module to my library.
I have the below colors in my colors file.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="textColor1">#848484</color>
<color name="textColor2">#99848484</color>
<color name="textColor3">#676767</color>
<color name="textColor4">#e7e6e6</color>
</resources>
What I want to do is to enable the user of the library to pass values for these colors programmatically so I can use them in my library using the normal way:
In XML:
android:textColor="@color/textColor1"
or in Java:
setTextColor(ActivityCompat.getColor(context, R.color. textColor1));
There are many way to create this functionality. Below is one of them: In Libray create util class:
class Utility{
public static int myColor = 0;
public void setColor (@ColorInt int colorId){ // integer id
myColor = colorId;
}
}
Use this colorId in anywhere in your library
for ex.
textView.setTextColor(ContextCompact.getColor(context, Utility.myColor);
Apply color from your project like:
Utility.setColor(R.color.colorPrimary);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With