2

I've been using custom styles and I was doing fine but now I'm confused on how to use completely different styles for widgets such as CompoundButtons, Spinners, ImageButtons, etc.. I have custom Buttons made for each type and each has its own drawable depending on its state. That I have gotten figured out but I need two completely different Themes (ex. use meal_orange ImageButton when state is pressed and use meal_blue for ImageButton state pressed for my other Theme) depending on which of two customers the app is for. I've searched for this problem so either it isn't possible without two separate apps or I'm not searching the right terms or fully understanding themes (possibly both).

A small example of what I currently have is in my styles.xml

// these point to separate selector files to show the different states
<style name="MealButton" parent="@android:style/Widget.ImageButton">
    <item name="android:background">@drawable/meal_button</item>
</style>

<style name="CatButton" parent="@android:style/Widget.ImageButton">
    <item name="android:background">@drawable/cat_button</item>
</style>

I originally created a custom theme with something like

<style name="CustomActivityTheme" parent="@android:style/Theme.Holo.Light">
    <item name="android:actionBarStyle">@style/MyActionBar</item>
</style>

the ActionBar part is irrelevant because I created a custom bar but if I could do that and inside here dictate to use the above examples of the ImageButton styles then that would be great.

I hope I explained this correctly. Can someone tell me if this is possible. I can't imagine I would need two separate versions of the app but maybe. I thought I could declare these styles inside the different themes but I didn't seem to be able to do that for widgets.

Just to clarify, the question is how can I use that first chunk of code inside the second chunk and do that for two separate themes? Thanks!

codeMagic
  • 44,549
  • 13
  • 77
  • 93
  • I'm really confused. You want to have different styles for your buttons depending on their state? Is that your end goal? – Catherine May 11 '13 at 05:35
  • I already have two different icons for them but two sets. Each `Button` has an icon for pressed and one for normal. But I have two sets of those...say, one set for company A and one set for company B. I want to have it use set 1 buttons if downloaded by company A and set two if downloaded by company B. I hope that makes sense – codeMagic May 11 '13 at 08:09
  • Short of sending them separate APKs, I'm not sure you can do it--see http://stackoverflow.com/questions/3246447/how-to-set-the-style-attribute-programmatically-in-android – Catherine May 12 '13 at 05:44
  • @Catherine thanks for your responses. I'm not sure that postreally helps me and I'm trying to avoid having to make two separate apps to keep up with. The thing is, this is a specialty app that is only authorized for our customers that subscribe to our service and we load the apps before sending out devices. I guess the easy way would be to just name all of the `drawables` and `styles` the same and just have to update java and `layout` files when I had updates working. But would be much more ideal to have one app with a flag sent from the server to use a certain `theme` if possible – codeMagic May 12 '13 at 06:12
  • That post helps because what you want is to apply a theme programmatically, and that is not possible according to it. – Catherine May 13 '13 at 04:01
  • Ok, I see your point in that but I could set the `Theme` programmatically but the problem is that I can't do the `Themes` in the way that I need. Thanks for the link and for taking the time to look into my problem. I'm apparently stuck with creating two different, but basically the same, apps – codeMagic May 13 '13 at 04:05
  • Well, you're only really stuck with creating two different, but basically the same, builds. The code can be pretty much the same, just swap the XML files around (Raghav Sood suggests the same thing). – Catherine May 13 '13 at 18:39

1 Answers1

2

Unfortunately, this isn't possible.

App styles cannot be controlled programmatically app wide, so you can't really set a flag and have it work separately for each client.

You could perhaps have a bash script, or use maven, to automate the builds by swapping the XML files in and out on a per build basis.

Or if your codebase is exactly the same for both clients, you could move it into a library project, and create two projects that stem from it to have separate themes.

Raghav Sood
  • 81,899
  • 22
  • 187
  • 195
  • Thanks for your quick response. That's why I voted for you in the last election :) That sounds like it may be a good possibility but I will have to look into how I would do that exactly. Is it just me or should this be a possibility? I've been searching and thinking for awhile how to do this and I need to make a decision in the next couple of days. The way I think of it is that you should be able to have a `Theme` containing different `styles` and change the `Theme`, along with the respective `styles`, programmatically. – codeMagic May 12 '13 at 06:21
  • Does my "solution" in my last comment of my OP seem like a decent one to you? – codeMagic May 12 '13 at 06:25
  • Thanks again for your quick response and explanation. If you could give me a reason why this isn't possible or if my "solution" seems like a viable one, that would be great. **Note** the code is exactly the same for now but I have to leave it flexible because, well, you know how salesman are ;) – codeMagic May 12 '13 at 06:46
  • @codeMagic Your last comment seems to be the best solution. I saw a post long back from someone who made a semi parser for styles. I'll see if I can find it. It may allow you to use only one apk if it works well. – Raghav Sood May 12 '13 at 10:18
  • Ok, I was hoping what I was after was possible within the framework, guess not. Thanks – codeMagic May 12 '13 at 13:30
  • @codeMagic It isn't sadly :( It should be, but the Java side support for app theming is very limited. – Raghav Sood May 12 '13 at 13:31
  • Ok, well at least I can stop looking now. Should have asked a couple weeks ago but I wanted to make sure I put in a full effort. Thanks for responding – codeMagic May 12 '13 at 13:34