I have a library which has its own Activities with colorPrimary and colorPrimaryDark attributes. In the application which is using this library, there are different values for these color attributes.
Is there a way to make the library use the style provided by the caller application?
So that in the end, if the app has a green toolbar, the activities in the library would have a green toolbar, not the one defined in library theme.
This is the library's theme:
<style name="LibraryTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">@color/red</item>
<item name="colorPrimaryDark">@color/dark_red</item>
</style>
And this is the sample app's main theme:
<style name="SampleAppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">@color/green</item>
<item name="colorPrimaryDark">@color/dark_green</item>
<item name="colorAccent">@color/accent_color</item>
</style>
Styles and themes on Android allow you to separate the details of your app design from the UI structure and behavior, similar to stylesheets in web design. A style is a collection of attributes that specify the appearance for a single View .
Create a file named styles. xml in the your application's res/values directory. Add a root <resources> node. For each style or theme, add a <style> element with a unique name and, optionally, a parent attribute.
The Android TextAppearance attribute is a special attribute used to apply text-specific styling to your View components. The TextAppearance attribute is created to help Android developers create a modular styling system for their applications. A single Android View component can have one style attribute applied to it.
Defining Styles This XML file resides under res/values/ directory of your project and will have <resources> as the root node which is mandatory for the style file.
If "colorPrimary" will use the same value - @color/primaryColor
, then value of library primaryColor
will be overridden by primaryColor
in sample app.
If you will use different values for "colorPrimary" in library and app, for example - @color/libPrimaryColor
and @color/appPrimaryColor
then colors will be different
You can override the theme in your style file,
<style name="LibraryTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">Your color</item>
<item name="colorPrimaryDark">Your color, too</item>
</style>
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