Home » Android » android – How to override styles of a library which has its own Activity

android – How to override styles of a library which has its own Activity

Posted by: admin May 14, 2020 Leave a comment

Questions:

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>
How to&Answers:

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

Answer:

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>