Best practice for providing multiple themes for Android apps

I am trying to figure out how to organize an Android application that will have several themes. here is a simplified example of my problem,

2 themes, "light" and "dark",
each topic has two text colors: "on" and "off"

Now the problem is that when I define my TextView, I don’t want to call “light” or “dark” there, I just want to specify the theme at the application level and apply it. basically, I want CSS selectors. I want to define my topic as,

<style name="Light.enabled" .../> <style name="Light.disabled" .../> 

and my textual representation for example

 <TextView style="@style/.enabled" .../> <TextView style="@style/.disabled" .../> 

and whether it’s applied “on” or “off” depending on whether I caused “light” or “dark” at the application level.

this page, http://www.androidengineer.com/2010/06/using-themes-in-android-applications.html

shows the application that

  • defines a style, for example "light.enabled"

    # 000000

  • defines an attribute reference, for example, "enabled"

  • defines a style (theme), for example,

    @ Style / light.enabled

  • uses attr to determine the style in the view,

this is what i want, but it does not work for me. the only diff is that I'm using appwidget. The author sets the topic of activity. the only place I can set is Context.setTheme () and in the "application" tag in the manifest. none of which seem to matter.

+4
source share
3 answers

You cannot apply themes to application widgets. You just need to have a different XML that uses different styles.

Also, it is confusing when you talk about light against the dark and turn on against the disabled as similar things. On the platform they are very different.

  • Light and darkness are relevant “themes” defined by the platform, which is a set of default values ​​for resource attributes rooted in android: style / Theme. They are changed using the android: the theme in the manifest or setTheme () in the API.

  • enabled and disabled states. They are used with StateListDrawable (via the tag in drawable /) or ColorStateList (via the tag in color /) to select a drawing method / color based on the allowed state.

For example, here is a color that changes depending on the state:
https://android.googlesource.com/platform/frameworks/base/+/master/core/res/res/color/primary_text_dark.xml

And here we can conclude that the changes are based on state:
https://android.googlesource.com/platform/frameworks/base/+/master/core/res/res/drawable/btn_default.xml

0
source

I found this page, http://www.androidengineer.com/2010/06/using-themes-in-android-applications.html

which gives an excellent example of the dynamic application of several topics. I can not get it to work with application widgets, but this is another problem.

+3
source

If you are trying to support multiple themes in the application.

  • Define a topic in /styles.xml values

     <style name="AppTheme" parent="Theme.AppCompat.Light"> 

    @ Hood / ic_mode_comment_white_24dp

    ... @ Hood / ic_subject_black_24dp

  • define colors in colors.xml

<resources> <color name="colorPrimary">#FF9800</color> </resources>

  1. switch themes while working

    public abstract class BaseActivity extends ActionBarActivity { @Override protected void onCreate(Bundle savedInstanceState) { if (PreferenceManager.getDefaultSharedPreferences(this) .getBoolean("pref_dark_theme"), false)) { setTheme(R.style.AppTheme_Dark); } super.onCreate(savedInstanceState); } }

follow these articles part1 and part2

0
source

All Articles