2

There is any way to declare variables (using product flavors) in the build.gradle that can be substituted in the styles.xml of my Android application? I want provide custom fonts for each product flavor i use for the application like this approach:

Is it possible to declare a variable in Gradle usable in Java?

And you have some styles that depend on the value of the variable:

<style name="my-android-style">
    <item name="font_file_name">MY_VARIABLE_NAME-regular.ttf</item>
</style>

Then, in my custom TextView class i set his typeface with:

setTypeface(mFontFileName);

There is any way to face this problem? Thanks in advance.

Community
  • 1
  • 1
Ricardo Ribas
  • 409
  • 4
  • 14

2 Answers2

2

There is any way to declare variables (using product flavors) in the build.gradle that can be substituted in the styles.xml of my Android application?

No. But you can have different versions of a styles-fonts.xml file where you declare your typeface. Have your default in main and override that file in other product flavors as needed. Isolate the smallest possible bit of style information into styles-fonts.xml (or whatever you want to name it) and you can minimize redundancy between build variants.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
0

Like you wants to do it, following the answer of CommonWare, it is not possible. But you can do in another way: For my whitelabels products Ive created a file called branding.xml (inside of the values folder) where in this file I set up all the things that i need to take care when i am building different products from my root project. (you can called this file with the name that you wish of course. This file has to be also included into the root project).

I am going to start showing you a small piece of my gradle file, ok?

   signingConfigs {
      rootproject{
        storeFile file("rootprojec_keystore") //Path to the keystore file
        keyAlias "aliasname"
        storePassword "xxxxxxxxx"
        keyPassword "xxxxxxx"
      }

      whitelabel1 {
        storeFile file("whitelabels/whitelabel1_keystore") //Path to the keystore file
        keyAlias "whitelabel_1"
        storePassword "xxxxxxxx"
        keyPassword "xxxxxxx"
      }
  }

  productFlavors {
    rootproject{
        applicationId  = "com.rootproject.android"
        signingConfig = signingConfigs.rootproject
    }

    whitelabel1{
        applicationId  = "com.whitelaber1.android"
        signingConfig = signingConfigs.whitelabel1
    }
 }

  sourceSets {
    main {
        manifest.srcFile 'AndroidManifest.xml'
        java.srcDirs = ['src','test']
        resources.srcDirs = ['src','test']
        aidl.srcDirs = ['src','test']
        renderscript.srcDirs = ['src','test']
        res.srcDirs = ['res']
        assets.srcDirs = ['assets']
    }

    whitelabel1{
        res.srcDirs = ['whitelabels/whitelabel1/res']
    }
}

In the last block (sourceSets) is where you have the key. For my whitelabels I am using a full res folder but you could even use only the file i think like: whitelabels/whitelabel1/res/values/branding.xml

Ok? Now we know how the compiler is going to use a different file/folder for you whitelabel.

Well, now we are going to describe the branding.xml files:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <integer name="kind_font">assets/fonts/nameofthefont.ttf</integer>
</resources>

You must create the same file in your root project and the whitelabel one, chaning the value of the font, of course.

Ok, one elegant way to use custom fonts along to your app is using a custom text view so you must follow the instructions here, it is pretty easy:

http://www.techrepublic.com/article/pro-tip-extend-androids-textview-to-use-custom-fonts/

And now finally the line of code where you have to access to the variable with the name of the font that you want to load. In the last manual there is the line:

Typeface myTypeface = Typeface.createFromAsset(getContext().getAssets(), "fonts/"+fontName);

and you should modify with the name of the variable described into the branding.xml file:

String fontName = ((String)getResources().getString(R.bool.kind_font)

and that's all...hahaha...vote me if it was valid...and happy coding!

Juan Pedro Martinez
  • 1,924
  • 1
  • 15
  • 24