1

I am creating an app that has admin and user modes, and each mode has its own layouts, strings, and drawable resource files. I want to know how to change the resources SourceSet based on the app mode which can be toggled by the user at runtime.

Currently, I am using 2 product flavors to do this. But the problem with flavors is that it is build time, and I have to create 2 different apks, one for each flavor. So, being able to change SourceSet at runtime means I can have only 1 apk.

Update: I simply want a textview to call R.string.title, and this will call different string files based the user mode (Admin or user). This is the same as changing Locale language (en vs fr for example) will call the appropriate file without the need to change the code.

Davi
  • 1,031
  • 12
  • 21

2 Answers2

1

I want to know how to change the resources SourceSet based on the app mode which can be toggled by the user at runtime.

That is not possible. Source sets are a compile-time construct. An APK contains the contents of source sets based on the build variant you chose when you compiled the APK — the contents of other source sets are not in that APK.

I am creating an app that has admin and user modes, and each mode has its own layouts, strings, and drawable resource files.

Then either those are two apps (and two APKs), or you need to have all of those resources in the one source set that goes into your one APK.

Usually, an admin mode also involves dedicated Java/Kotlin code (e.g., dedicated fragments), and so just swapping resources would be insufficient, anyway.

If you are distributing solely through the Play Store, you could look into using dynamic feature modules, if your concern is the size of the APK with both user and admin code/resources in it.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • @Davi: "I simply want a textview to call R.string.title, and this will call different string files based the user mode (Admin or user)." -- that is not possible, sorry. There is only one string named `R.string.title` in your app. There might be more than one in the *project*, but there is only one in any given APK, based on your build variant. – CommonsWare Jun 10 '19 at 19:37
  • thanks for the response but I guess my question was not clear. I have edited my question. – Davi Jun 10 '19 at 20:09
  • @Davi: My [comment](https://stackoverflow.com/questions/56532014/android-programmatically-change-sourceset-based-on-user-settings/56532316?noredirect=1#comment99648383_56532316) is in response to your edit. – CommonsWare Jun 10 '19 at 20:25
  • As an example, I have 2 string files, values/strings.xml and values-fr/strings.xml and both of them contains R.string.title, which is displayed based on the Locale change done by user at runtime. This is done by calling context.getResources().updateConfiguration(). SO can I update the configuration based on other factors selected by user, rather than the language (Locale) change? – Davi Jun 10 '19 at 21:02
  • @Davi: No, sorry, other than by having two different strings (`R.string.something_user` and `R.string.something_admin`) and choosing them by name based on your other factors. – CommonsWare Jun 10 '19 at 21:10
0

The solution which worked for me in case there were corporate applications where default launcher was locked and user launched login screen by default there were two separate applications which were switched depends on the role of the user. In other app where one apk I just made different screens launches with its own logic based on role user choose. But that also might help you Dynamically generating product flavors

Bo Z
  • 2,359
  • 1
  • 13
  • 31