5

I want to produce a single app that would let the user select a theme and have this applied over the entire app.

I understand that this can be achieved by using setTheme in onCreate of each activity.

But I need this to work in a different way. I don't want to have the themes stored in theme.xml or styles.xml.

I want to have a list of themes stored on the web and be able to dynamically download a new theme and have it applied in the app. I want to be able to create new themes without having to build a new version or an updated version of the app.

Images would be easy to replace. Just download from a url and store locally to be re-used. But the actual theme of the app, the colours of buttons etc should be changed at run time from a theme.xml file which isn't part of the apk but is fetched online.

Is this possible?

Stephen
  • 762
  • 1
  • 12
  • 32

1 Answers1

2

It depends how much styling you want to be able to do. You currently can't set view items styles grammatically outside of using a resource. But you can control things like text color and background color. If that's all you need to change, I would recommend writing a Theme factory class for you app that you use to get each view element you need. For example a getButton() function that will return you a button with the background color and text color you need.

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
Ben
  • 1,285
  • 1
  • 9
  • 15
  • Hi Ben, Thanks for your response. This sounds very promising as I think that yes, we would only want to change colours of backgrounds and text. In such a factory method how would you change the colour of a button using a custom colour not defined in the app at build time and not part of the android sdk? – Stephen Mar 07 '14 at 14:42
  • 1
    So if you're downloaded style is in say json, you would parse out the field for background color. Then your theme getButton() you would create a new button and call button.getBackground().setColorFilter("YOUR COLOR", PorterDuff.Mode.MULTIPLY). Not this filter works buy multiplying your color against the default so it works ok. But there are lots of ColorFilter options you could use. Similarly you can use button.setTextColor(YOUR BUTTON TEXT COLOR); to change the text of the button. – Ben Mar 07 '14 at 14:48
  • Ooh...That's sexy. Thanks man! Going to knock together a test. Make it an answer and I will give it a tick. – Stephen Mar 07 '14 at 14:59