Search code examples
androidandroid-stylesandroid-build-flavors

Parent style depending on build variant


I have two themes defined in themes.xml.

Those two themes are applied in the manifest via productFlavors:

build.gradle

productFlavors {
            flavor1 {
                dimension "theme"
                manifestPlaceholders = [
                        appTheme: "@style/Theme1"
                ]
            }

            flavor2 {
                dimension "theme"
                manifestPlaceholders = [
                        appTheme: "@style/Theme2"
                ]
            }
    }

manifest.xml

 <activity
            android:name=".MainActivity"
            android:screenOrientation="portrait"
            android:windowSoftInputMode="adjustPan"
            android:theme="${appTheme}" >

This works correctly, depending on the build variant selected, different themes are applied.

Now I want to integrate Stripe SDK witch declares a Style called StripeDefaultTheme.

In this style I want to override the colorPrimary and colorAccent with the values of my own themes.

Is it possible to declare a parent to a style that is determined by a Build Variant ?


Solution

  • Instead of making two separate themes, you can have just one theme that varies depending on different flavor. To do so you just need to override styles.xml files in the corresponding flavor's source set. For example,

    1. The AppTheme in the main source set should contain the stripe related theming only,

      {app_module_dir}/src/main/res/values/styles.xml

      <style name="BaseAppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
          <!-- Stripe related values here. -->
      </style>
      

    2. Copy the contents of Theme1 into flavor1's source set

      {app_module_dir}/src/flavor1/res/values/styles.xml

      <style name="AppTheme" parent="BaseAppTheme">
          <!-- Theme1 contents here. -->
      </style>
      

    3. Copy the contents of Theme2 into flavor2's source set

      {app_module_dir}/src/flavor2/res/values/styles.xml

      <style name="AppTheme" parent="BaseAppTheme">
          <!-- Theme2 contents here. -->
      </style>
      

    Mannifest

    <activity
        ...
        android:theme="@style/AppTheme" >
    

    build.gradle

    productFlavors {
            flavor1 {
                dimension "theme"
            }
    
            flavor2 {
                dimension "theme"
            }
    }