ProgressBar Colour Android API Level < 21

To give my progress bar a background colour I had to create a custom renderer in which I call:

Control.ProgressTintList = Android.Content.Res.ColorStateList.ValueOf(color.ToAndroid());

However when this runs on Android versions pre API level 21, ProgressTintList method is not found.
https://developer.android.com/reference/android/widget/ProgressBar.html#setProgressTintList(android.content.res.ColorStateList)

How can I go about having a ProgressBar with a colour pre API level 21?

Answers

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    Couldn't you have just set the background color in the materials theme.xml of your android project? Seems like a lot less work.

    http://www.zoftino.com/android-progressbar-and-custom-progressbar-examples

  • darrell.tunnelldarrell.tunnell USMember ✭✭
    edited January 2018

    Ok I have found an answer.

    In the renderer

                    if (_isTintSupported)
                    {
                        //  ProgressTintList is only available >= api level 21.
                        Control.ProgressTintList = Android.Content.Res.ColorStateList.ValueOf(androidColor);
                    }
                    else
                    {
                        Drawable progressDrawable = Control.ProgressDrawable.Mutate();
                        progressDrawable.SetColorFilter(androidColor, Android.Graphics.PorterDuff.Mode.SrcIn);
                        Control.ProgressDrawable = progressDrawable;
                    }
    

    Where _isTintSupported is defined as:

       private static readonly bool _isTintSupported = Android.OS.Build.VERSION.SdkInt >= global::Android.OS.BuildVersionCodes.Lollipop;
    
  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    Thanks for sharing the solution. Not enough people do that for their own questions but it really is what makes the coding community a community.

Sign In or Register to comment.