-2
  1. Scenario: I have a scenario where I have to build an apk, which will have two sets of themes and drawable.

Can we accommodate this scenario in android, so that apk during compile time has to pick different theme and drawable . Based on some condition as below

Set 1

Set 2

Theme (dark version) In Res folder Folder name: Drawable Image1.png Image2.Png Image3.Png Image4.Png

Theme (light version) In Res folder Folder name: Drawable Image1.png Image2.Png Image3.Png Image4.Png

Note: in set 1 and set 2 the drawable images are different but with same name. That is image 1 in set 1 is different from image 1 in set 2

Kindly guide me regarding the approach which i have to choose

Thanks in advance.

Bhargavaroyal
  • 69
  • 1
  • 5
  • like the case in the below http://www.techotopia.com/index.php/An_Android_Studio_Gradle_Build_Variants_Example productFlavors { phone { applicationId "com.ebookfrenzy.buildexample.app.phone" versionName "1.0-phone" } tablet { applicationId "com.ebookfrenzy.buildexample.app.tablet" versionName "1.0-tablet" } } – Bhargavaroyal Sep 23 '16 at 15:54

1 Answers1

0

You can declare 2 different app themes in styles.xml like so:

<style name="theme1">
    <item name="drawable1">@drawable/drawable1forTheme1</item>
    // same for all your other drawables
</style>

<style name="theme2">
    <item name="drawable1">@drawable/drawable1forTheme2</item>
    // same for all your other drawables
</style>

and then declare all of these drawables in attrs.xml:

<attr name="drawable1" format="reference"/>
// same for all your other drawables

You can then simply reference the right drawable via the attribute, and the actual drawable shown will depend on what theme is set.

For example in a layout:

<ImageView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:src="?attr/drawable1"/>

Notice the "?attr/" which tells Android that the drawable should be loaded from an attribute, and this drawable attribute will change depending on the set theme.

Luke Needham
  • 3,373
  • 1
  • 24
  • 41
  • Thanks ...Can you explain in this situation ... https://developer.android.com/studio/build/build-variants.html in Configure Build Variants... Each build variant represents a different version of your app that you can build. For example, you might want to build one version of your app that's free, with a limited set of content, and another paid version that includes more. You can also build different versions of your app that target different devices, based on API level or other device variations..... using Gradle as well – Bhargavaroyal Sep 23 '16 at 17:41
  • Try checking out this question http://stackoverflow.com/questions/30279304/how-to-use-build-types-debug-vs-release-to-set-different-styles-and-app-names/30279350#30279350 – Luke Needham Sep 23 '16 at 18:27