Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

colorControlActivated is not the default #60

Open
leinardi opened this issue Jun 11, 2017 · 13 comments
Open

colorControlActivated is not the default #60

leinardi opened this issue Jun 11, 2017 · 13 comments
Assignees

Comments

@leinardi
Copy link

colorAccent is used instead of colorControlActivated:

image

Theme:

    <!-- Legend App Theme -->
    <style name="BaseTheme.Legend" parent="AppTheme">
        <!-- Attributes for all APIs -->
        <item name="colorPrimary">@color/material_red_500</item>
        <item name="colorPrimaryDark">@color/material_blue_700</item>
        <item name="colorAccent">@color/material_green_a700</item>
        <item name="colorControlActivated">@color/material_purple_500</item>
        <item name="colorControlHighlight">@color/material_lime_500</item>
        <item name="dialogTheme">@style/AppTheme.Dialog.Legend</item>
        <item name="alertDialogTheme">@style/AppTheme.Dialog.Alert.Legend</item>
    </style>

Layout:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
    style="@style/CardStyle.Home"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/material_theme_progress_indicators"
            android:textAppearance="@style/TextStyle.Title"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/view_spacing_medium"
            android:text="@string/material_theme_stock"
            android:textAppearance="@style/TextStyle.Body2"/>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/view_spacing_small"
            android:gravity="center_vertical">

            <ProgressBar
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="@dimen/view_spacing_small"
                android:indeterminate="true"/>

            <ProgressBar
                style="@style/ProgressBarStyle.Horizontal"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="@dimen/view_spacing_small"
                android:indeterminate="true"/>
        </LinearLayout>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/view_spacing_medium"
            android:text="@string/material_theme_material_progress"
            android:textAppearance="@style/TextStyle.Body2"/>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/view_spacing_small"
            android:gravity="center_vertical"
            android:theme="@style/AppTheme.Green">

            <me.zhanghai.android.materialprogressbar.MaterialProgressBar
                style="@style/Widget.MaterialProgressBar.ProgressBar"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="@dimen/view_spacing_small"
                android:indeterminate="true"/>

            <me.zhanghai.android.materialprogressbar.MaterialProgressBar
                style="@style/Widget.MaterialProgressBar.ProgressBar.Horizontal"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="@dimen/view_spacing_small"
                app:mpb_progressStyle="horizontal"
                android:indeterminate="true"/>
        </LinearLayout>
    </LinearLayout>
</android.support.v7.widget.CardView>

Tested on emulator API 25 and 16.

@zhanghai
Copy link
Owner

Strange. I found no reference to colorAccent in code.

Can you verify what happens if you explicitly set the android:* attributes to those values? I suspect it can probably be an AppCompat issue.

@leinardi
Copy link
Author

Hi, thanks for the quick reply.

I added these attributes for API21+:

        <item name="android:colorPrimary">@color/material_red_500</item>
        <item name="android:colorPrimaryDark">@color/material_blue_700</item>
        <item name="android:colorAccent">@color/material_green_a700</item>
        <item name="android:colorControlActivated">@color/material_purple_500</item>
        <item name="android:colorControlHighlight">@color/material_lime_500</item>

but unfortunately nothing changes.

I just noticed something: when i switch to different themes at runtime the MaterialProgressBar stays the same color (green) even if I switch to a theme that as no green color at all.

I suspect that the green color that we see in the screenshot is the colorControlActivated of the default theme that is set int he Android Manifest and that the View is not using the Theme that I'm setting at runtime.

I'm using setTheme() in the onCreate() of the Activity before calling setContentView() to change dynamically the theme.

@zhanghai
Copy link
Owner

Then it would need some debugging - check if the color retreived from colorControlActivated is correct, at here and here. Currently I'm busy with some other matters, can you set up some breakpoints and check them?

@leinardi
Copy link
Author

Sure. I've checked and the retrieved color is the wrong one.

I also tried in my Activity, between the setTheme and the setContentView, using the same code that is used in the classes that you suggested to test ( ThemeUtils.getColorFromAttrRes(R.attr.colorControlActivated, Color.BLACK, this)) and the value retrieved is the right one: -6543440 (ff9c27b0)

But when setContentView is called and I check the color of BaseIndeterminateProgressDrawable I'm getting back -16725933 (ff00c853).

It seems that the context that the View is using a different Theme.

I checked also the context of

    public MaterialProgressBar(Context context, AttributeSet attrs) {
        super(context, attrs);

        init(attrs, 0, 0);
    }

and I'm getting the wrong color there too.

Let me know if you want me to do more tests.

@zhanghai
Copy link
Owner

So maybe there is some magic inserted into LayoutInflator by AppCompat; But the framework widget seemed correct. Can you check ThemeUtils.getColorFromAttrRes(android.R.attr.colorControlActivated, Color.BLACK, context) on the Context passed to MaterialProgressBar?

@zhanghai zhanghai self-assigned this Jun 14, 2017
@leinardi
Copy link
Author

Sorry probably I'm not getting what are asking me to test because, like I said in my previous comment, I've already checked the context that MaterialProgressBar is getting in the constructor and I'm getting the wrong color also there.

Let me know if you want me to test something else.

@zhanghai
Copy link
Owner

zhanghai commented Jun 14, 2017

Well I mean android.R.attr.

@leinardi
Copy link
Author

Sorry, I did notice the android change.

Anyway tested and is still getting the wrong color.

@zhanghai
Copy link
Owner

zhanghai commented Jun 14, 2017

Strange - how did framework ProgressBar get the correct color?

Another question: Did you put the setTheme() call before super.onCreate()? It is required if you want theme switching to function properly.

@leinardi
Copy link
Author

Yep, it is before:

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        // Get passed in parameters
        Bundle args = getIntent().getExtras();

        // If no parameters were passed in, default them
        if (args == null) {
            mCurrentTheme = null;
        }
        // Otherwise, set incoming parameters
        else {
            mCurrentTheme = (MaterialTheme) args.getSerializable(KEY_ARG_CURRENT_THEME);
        }

        // If not set, default the theme
        if (mCurrentTheme == null) {
            mCurrentTheme = MaterialTheme.THEME_LEGEND;
        }

        // Theme must be set before calling super or setContentView
        setTheme(mCurrentTheme.getThemeResId());

        // Handle super calls after setting the theme
        super.onCreate(savedInstanceState);
        Timber.v(LogUtils.getSavedInstanceStateNullMessage(savedInstanceState));

        // Set the content view to a layout and reference views
        setContentView(R.layout.activity_home);

@zhanghai
Copy link
Owner

What happens if you change your default theme's colorControlActivated to something different from your colorAccent and stop switching themes?

If the behavior is correct, it should be some theme switching bug.

@leinardi
Copy link
Author

I commented the setTheme() and the MaterialProgressBar is green:

device-2017-06-14-171705

But if I define the colorControlActivated then the color works fine:
device-2017-06-14-172030

The issue happens only if I switch theme at runtime.

@zhanghai
Copy link
Owner

zhanghai commented Jun 20, 2017

Sorry for the delay.

Why did MaterialProgressBar in the first screenshot show a green color? You mentioned that setTheme() is commented out so I assume it should be the same as framework ProgressBar. What did you have in theme in that case?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants