garretyoder/Colorful

"setPrimaryColor" and "setCustomThemeOverride" conflict

tcqq opened this issue · 12 comments

tcqq commented

@garretyoder Using "setPrimaryColor" and "setCustomThemeOverride" at the same time, "setPrimaryColor" will not work when the application is restarted

Code

Activity.java

        Colorful().edit()
                .setAccentColor(ThemeColor.PINK)
                .setCustomThemeOverride(R.style.AppThemeWhite)
                .apply(getContext(), () -> {
                    recreate();
                    return null;
                });

BaseActivity.java

ColorfulKt.Colorful().apply(this, false, true);

Style.xml

    <style name="AppThemeWhite" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="colorPrimary">@color/white</item>
        <item name="toolbarStyle">@style/AppThemeWhite.Toolbar</item>
    </style>

    <style name="AppThemeWhite.Toolbar" parent="@style/Widget.AppCompat.Toolbar">
        <item name="titleTextAppearance">@style/AppThemeWhite.Toolbar.Title</item>
        <item name="subtitleTextAppearance">@style/AppThemeWhite.Toolbar.Subtitle</item>
    </style>

    <style name="AppThemeWhite.Toolbar.Title" parent="@style/TextAppearance.Widget.AppCompat.Toolbar.Title">
        <item name="android:textColor">?attr/colorAccent</item>
    </style>

    <style name="AppThemeWhite.Toolbar.Subtitle" parent="@style/TextAppearance.Widget.AppCompat.Toolbar.Subtitle">
        <item name="android:textColor">?attr/colorAccent</item>
    </style>
tcqq commented

@garretyoder Because I want to create this style of UI, so need to configure the toolbar widget color in the style resource. How should I use "Colorful" to create this style of UI?
image

This is a duplicate of #27. Short version: the custom theme override is the last thing colorful applies, so it will of course override everything else. This is why it's called the custom theme override.

If you want to achieve a white toolbar, don't set color primary in your theme. This is the exact same value colorful sets. Just set the toolbar color.

Or, even easier, in your xml layout just set the toolbar background color to white.

Alternately, if you want to match that exact picture, set color primary to white and accent to blue, then just set your toolbar background color and text color to ?attr:colorPrimary and ?attr:colorAccent respectively.
Though if you're trying to recreate a static theme I'm not sure why you want dynamic theming.

tcqq commented

I set the toolbar color in style is because the toolbar needs to support both styles:

  1. Primary color = BLUE
    Accent color = WHITE
  2. Primary color = WHITE
    Accent color = BLUE

Switch style when changing styles, But I use the above solution, the application can only in one style no problem
image
image

What should I do to support both styles? Thank you.

Either switch colorful's theme to whatever color you need, apply a theme to the toolbar manually, or, if you aren't needing runtime themes for this activity, then just use a regular style and don't use Colorful for that.

tcqq commented

"Either switch colorful's theme to whatever color you need, apply a theme to the toolbar manually, ”

If change the toolbar programmatically, meaning all other widgets related to the primary/accent color, I need to programmatically change all widgets the color.

Are you trying to use dynamic themes? Because that's what colorful is for. If you only need two different themes for this activity than just use a if statement and apply style files. If you want to set app-wide runtime themes then use Colorful.

tcqq commented

I try to set the dynamic theme using "Colorful", But when I use "Colorful" to switch themes, it won't work immediately, When the application is restarted, the style will change. What should I do to enable the application to not restart the theme can change?

Code

BaseApp.java

            Defaults defaults = new Defaults(
                    ThemeColor.DEEP_PURPLE,
                    ThemeColor.PINK,
                    false,
                    false,
                    0);
            ColorfulKt.initColorful(this, defaults);

BaseActivity.java

ColorfulKt.Colorful().apply(this, true, true);

Activity.java

        Colorful().edit()
                .setCustomThemeOverride(R.style.AppWhite)
                .apply(getContext(), () -> {
                        recreate();
                });

Style.java

<style name="AppWhite" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="preferenceTheme">@style/PreferenceThemeOverlay.v14.Material</item>
    <item name="toolbarStyle">@style/AppThemeWhite.Toolbar</item>
</style>

<style name="AppThemeWhite.Toolbar" parent="@style/Widget.AppCompat.Toolbar">
    <item name="titleTextAppearance">@style/AppThemeWhite.Toolbar.Title</item>
    <item name="subtitleTextAppearance">@style/AppThemeWhite.Toolbar.Subtitle</item>
</style>

<style name="AppThemeWhite.Toolbar.Title" parent="@style/TextAppearance.Widget.AppCompat.Toolbar.Title">
    <item name="android:textColor">?attr/colorAccent</item>
</style>

<style name="AppThemeWhite.Toolbar.Subtitle" parent="@style/TextAppearance.Widget.AppCompat.Toolbar.Subtitle">
    <item name="android:textColor">?attr/colorAccent</item>
</style>
tcqq commented

I use resetPrefs to reset the theme, and call recreate Activity, but it will not change immediately, After restart application, it will change. Is this a problem?

        Colorful().edit()
                .resetPrefs(getContext());
        recreate();

This is explained the first section in the readme entitled "Using Themes".
I highly recommend you read the readme in it's entirety to get a good understanding on how to use Colorful.

tcqq commented

I implements CThemeInterface for my BaseActivity, and generate these methods in BaseActivity

    @NotNull
    @Override
    public String getThemeString() {
        return null;
    }

    @Override
    public void setThemeString(String s) {

    }

    @Override
    public void handleOnCreate(Activity activity, Bundle bundle, boolean b, boolean b1) {

    }

    @Override
    public void handleOnResume(Activity activity) {

    }

Then I try to change theme and recreate Activity, but theme not changed, when I restarted the application, the theme will changed. You know more about this library than I do, how should I change it specifically, can I update the theme without restarting the application? thank you so much.

  • This is the configuration before switching theme(default):
Colorful().edit()
                        .setCustomThemeOverride(0)
                        .setPrimaryColor(ThemeColor.DEEP_PURPLE)
                        .setAccentColor(ThemeColor.PINK)
                        .apply(getContext(), () -> {
                            recreate();
                            return null;
                        })
  • This is the configuration after switching theme:
Colorful().edit()
                        .setCustomThemeOverride(R.style.AppCompat_Light_Inverse_Pink)
                        .apply(getContext(), () -> {
                            recreate();
                            return null;
                        })
    <style name="AppCompat.Light.Inverse" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="preferenceTheme">@style/PreferenceThemeOverlay.v14.Material</item>
        <item name="toolbarStyle">@style/Toolbar.Inverse</item>
    </style>

    <style name="AppCompat.Light.Inverse.Pink" parent="AppCompat.Light.Inverse">
        <item name="colorAccent">@color/md_pink_500</item>
    </style>
  • BaseActivity:
    ColorfulKt.Colorful().apply(this, true, true);

The theme will not automatically change as soon as the theme is updated. The activity is checked for a theme change in onResume. If you want to immediately update your activity, again see the readme:

The apply method optionally takes a high-order function as a argument. This serves as a callback that will be triggered once Colorful has finished it's theme changes. A example usage would be to recreate the current activity after setting a new theme to immediately reflect changes.

Colorful().edit()
    .setPrimaryColor(ThemeColor.PURPLE)
    .setAccentColor(ThemeColor.GREEN)
    .apply(this) { 
        this.recreate() 
    }
tcqq commented

Ok, I understand now, thanks.