Skip to content Skip to sidebar Skip to footer

Is It Possible To Load Different String(xml) Values In Android

In my app i want to load different string values from xml. Almost like the different language. I have multiple clients accessing the same app but they want different labels and sty

Solution 1:

You could create a different buildType and or productFlavor for each client. For each buildType/productFlavor you can define a separate sourceset. This means that each buildType/productFlavor can contain a different strings.xml. The keys for the strings have to be the same, but each client can have a different string file within its buildType/productFlavor.

More information about buildTypes/productFlavors:

http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Build-Variants

http://developer.android.com/tools/building/configuring-gradle.html

e.g. you define two flavors:

android {
...
defaultConfig { ... }
signingConfigs { ... }
buildTypes { ... }
productFlavors {
    clientA {
    }
    clientB {
    }
  }
}

Then you can create for each customer a sourceset in your project:

src/
 |->main (contains the common code/strings)
 |-> clientA
 |        |-> res
 |            |-> values
 |                |-> strings.xml
 |-> clientB
          |-> res
              |-> values
                  |-> strings.xml

In each strings.xml you can add the strings for your customer. The defined strings will also be merged with the strings of the main sourceset.

How do you tell your application what SRC to use. between Main/clientA/clientB?

You can build select the builtType/productFlavor in Android Studio.

To work on files from a particular flavor, click on Build Variants on the left of the IDE window and select the flavor you want to modify in the Build Variants panel, as shown in figure 2. Android Studio may show errors in source files from flavors other than the one selected in the Build Variants panel, but this does not affect the outcome of the build.

http://developer.android.com/tools/building/configuring-gradle.html#workBuildVariants

Post a Comment for "Is It Possible To Load Different String(xml) Values In Android"