Reputation: 9541
Please note that I am not referring to android themes like Theme.App.Light, Theme.App.Dark
etc.
I need to provide different themes in my application using shared preferences. The themes will basically differ from each other only in the backgronud image and image buttons.
The choice of theme would be specified by user using shred preferences. So assume that I have got the selected theme preference like:
theme_name = sharedPrefs.getString("theme_name", "NULL");
Now what is the best way to implement this.
One rather crude way would be set background resource and image button in each activity like:
if (theme_name == "red") {
button.setImageResource( R.drawable.red_button );
}
elif (theme_name == "green") {
button.setImageResource( R.drawable.green_button );
}
...
Now if I keep doing that in every activity, i will essentially mix presentation, logic and UI elements in one big spaghetti code.
What is a better way of handling this kind of code?
Upvotes: 0
Views: 58
Reputation: 116
You can write a custom button view. Example:
public final class ThemedButton extends Button { }
and in the constructor, you can init the view as you want. In the constructor you have the context, so get the preference and set the background color of your themed button. This way you avoid to dirty your activity.
Hope this helps :)
Upvotes: 1