Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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>
like image 878
Yasin YILDIRIM Avatar asked Feb 10 '16 12:02

Yasin YILDIRIM


People also ask

What is the use of styles and themes in android explain it?

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 .

How to add styles xml in android Studio?

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.

What is TextAppearance in android?

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.

Where is styles xml in android Studio?

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.


2 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

like image 161
mobiledev Alex Avatar answered Sep 18 '22 12:09

mobiledev Alex


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>
like image 38
BollMose Avatar answered Sep 17 '22 12:09

BollMose