Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Styles heritage

Tags:

android

Here is the deal: In my Styles.xml I have a general style and one inherinting the other as it follows:

<resources>

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

<style name="AppTheme.NoActionBar" parent="@style/AppTheme">
    <item name="colorPrimary">#0acf66</item>
    <item name="android:windowActionBar">false</item>
    <item name="android:windowNoTitle">true</item>
</style>

When I apply the second theme (AppTheme.NoActionBar) it inherits the color but the action bar is still there. If i change the parent of the second style to Theme.AppCompat.NoActionBar it works, but the colors are totally diferent. How can I fix it?

like image 841
Matheus Neves Avatar asked Sep 13 '25 11:09

Matheus Neves


1 Answers

So this is an inheritance problem for styles/themse. There are two ways to inherit properties from styles.

1.Through dot notation (implicit):

<style name="Theme.AppCompat.NoActionBar.Custom">
  // the parent will be Theme.AppCompat.NoActionBar
</style>

2.Through parent tag (explicit)

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
   //the parent is Theme.AppCompat.Light.DarkActionBar
</style>

One thing to always remember, explicit inheritance wins over implicit inheritance.

So in your case

<style name="AppTheme.NoActionBar" parent="@style/AppTheme">
    // your parent is AppTheme, and new style is AppThem.NoActionBar,
       but it doesn't inherit from an actual NoActionBar style.
</style>

In order to have a "NoActionBar" theme, indeed you have to inherit from a style that has that property. So you could create another AppTheme.NoActionBar that inherits from Theme.AppCompat.NoActionBar.

To get a better idea of theme/styles, check this awesome Google I/O 2016 link about styles and themes.

like image 81
danypata Avatar answered Sep 16 '25 02:09

danypata