MaterialProgressBar icon indicating copy to clipboard operation
MaterialProgressBar copied to clipboard

colorControlActivated is not the default

Open leinardi opened this issue 8 years ago • 13 comments

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.

leinardi avatar Jun 11 '17 17:06 leinardi

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.

zhanghai avatar Jun 11 '17 18:06 zhanghai

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.

leinardi avatar Jun 11 '17 18:06 leinardi

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?

zhanghai avatar Jun 12 '17 10:06 zhanghai

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.

leinardi avatar Jun 12 '17 11:06 leinardi

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 avatar Jun 14 '17 05:06 zhanghai

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.

leinardi avatar Jun 14 '17 10:06 leinardi

Well I mean android.R.attr.

zhanghai avatar Jun 14 '17 10:06 zhanghai

Sorry, I did notice the android change.

Anyway tested and is still getting the wrong color.

leinardi avatar Jun 14 '17 11:06 leinardi

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.

zhanghai avatar Jun 14 '17 12:06 zhanghai

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);

leinardi avatar Jun 14 '17 14:06 leinardi

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.

zhanghai avatar Jun 14 '17 14:06 zhanghai

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.

leinardi avatar Jun 14 '17 15:06 leinardi

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?

zhanghai avatar Jun 20 '17 08:06 zhanghai