private void SetButtonIcon()
        {
            var withIcon = !string.IsNullOrEmpty(_materialButton.Image);

            _helper.UpdateHasIcon(withIcon);

            if (!withIcon)
            {
                return;
            }
            var fileName = _materialButton.Image.File.Split('.').First();
            var id       = this.Resources.GetIdentifier(fileName, "drawable", Material.Context.PackageName);
            var width    = _materialButton.ButtonType == MaterialButtonType.Text ? (int)MaterialHelper.ConvertToDp(18) : (int)MaterialHelper.ConvertToDp(18 + 4);
            var height   = (int)MaterialHelper.ConvertToDp(18);
            var left     = _materialButton.ButtonType == MaterialButtonType.Text ? 0 : (int)MaterialHelper.ConvertToDp(4);

            using (var drawable = MaterialHelper.GetDrawableCopyFromResource <Drawable>(id))
            {
                drawable.SetBounds(left, 0, width, height);
                drawable.TintDrawable(_materialButton.TextColor.ToAndroid());

                this.Control.SetCompoundDrawables(drawable, null, null, null);
                this.Control.CompoundDrawablePadding = 0;
            }
        }
Exemplo n.º 2
0
        private RippleDrawable GetRippleDrawable()
        {
            RippleDrawable rippleDrawable;

            if (_button.ButtonType == MaterialButtonType.Text || _button.ButtonType == MaterialButtonType.Outlined)
            {
                if (_button.ButtonType == MaterialButtonType.Outlined)
                {
                    rippleDrawable = _withIcon ? MaterialHelper.GetDrawableCopyFromResource <RippleDrawable>(Resource.Drawable.drawable_ripple_outlined_with_icon) : MaterialHelper.GetDrawableCopyFromResource <RippleDrawable>(Resource.Drawable.drawable_ripple_outlined);
                }
                else
                {
                    rippleDrawable = MaterialHelper.GetDrawableCopyFromResource <RippleDrawable>(Resource.Drawable.drawable_ripple_text);
                }
            }

            else
            {
                rippleDrawable = _withIcon ? MaterialHelper.GetDrawableCopyFromResource <RippleDrawable>(Resource.Drawable.drawable_ripple_with_icon) : MaterialHelper.GetDrawableCopyFromResource <RippleDrawable>(Resource.Drawable.drawable_ripple);
            }

            if (!(rippleDrawable.FindDrawableByLayerId(Android.Resource.Id.Mask) is InsetDrawable maskDrawable))
            {
                return(rippleDrawable);
            }

            if (maskDrawable.Drawable is GradientDrawable rippleMaskGradientDrawable)
            {
                rippleMaskGradientDrawable.SetCornerRadius(_cornerRadius);
            }

            return(rippleDrawable);
        }
Exemplo n.º 3
0
        private GradientDrawable CreateShapeDrawable(float cornerRadius, int borderWidth, Color backgroundColor, Color borderColor)
        {
            var shapeDrawable = _withIcon ? MaterialHelper.GetDrawableCopyFromResource <GradientDrawable>(Resource.Drawable.drawable_shape_with_icon) : MaterialHelper.GetDrawableCopyFromResource <GradientDrawable>(Resource.Drawable.drawable_shape);

            shapeDrawable.SetCornerRadius(cornerRadius);
            shapeDrawable.SetColor(backgroundColor);
            shapeDrawable.SetStroke(borderWidth, borderColor);

            return(shapeDrawable);
        }
Exemplo n.º 4
0
        private Drawable GetDrawable()
        {
            var normalStateDrawable   = this.CreateShapeDrawable(_cornerRadius, _borderWidth, _normalColor, _enabledBorderColor);
            var disabledStateDrawable = this.CreateShapeDrawable(_cornerRadius, _borderWidth, _disabledColor, _disabledBorderColor);

            if (Material.IsLollipop)
            {
                var rippleDrawable = this.GetRippleDrawable();
                var insetDrawable  = rippleDrawable.FindDrawableByLayerId(Resource.Id.inset_drawable) as InsetDrawable;
                if (insetDrawable != null)
                {
                    var stateListDrawable = insetDrawable.Drawable as StateListDrawable;
                    SetStates(stateListDrawable, normalStateDrawable, normalStateDrawable, disabledStateDrawable);
                }

                rippleDrawable.SetColor(new ColorStateList(new int[][]
                {
                    new int[] {}
                },
                                                           new int[]
                {
                    _pressedColor
                }));

                return(rippleDrawable);
            }
            else
            {
                var pressedStateDrawable             = this.CreateShapeDrawable(_cornerRadius, _borderWidth, _pressedColor, _enabledBorderColor);
                StateListDrawable stateListDrawable  = null;
                Drawable          backgroundDrawable = null;

                if (Material.IsJellyBean)
                {
                    stateListDrawable  = new StateListDrawable();
                    backgroundDrawable = stateListDrawable;
                }
                else
                {
                    var insetDrawable = MaterialHelper.GetDrawableCopyFromResource <InsetDrawable>(Resource.Drawable.drawable_selector);
                    stateListDrawable  = insetDrawable.Drawable as StateListDrawable;
                    backgroundDrawable = insetDrawable;
                }

                SetStates(stateListDrawable, normalStateDrawable, pressedStateDrawable, disabledStateDrawable);

                return(backgroundDrawable);
            }
        }
Exemplo n.º 5
0
        private void SetButtonIcon()
        {
            _withIcon = !string.IsNullOrEmpty(_materialButton.Image);

            if (_withIcon)
            {
                var fileName = _materialButton.Image.File.Split('.').First();
                var id       = this.Resources.GetIdentifier(fileName, "drawable", Material.Context.PackageName);
                var width    = (int)MaterialHelper.ConvertToDp(18);
                var height   = (int)MaterialHelper.ConvertToDp(18);
                var drawable = MaterialHelper.GetDrawableCopyFromResource <Drawable>(id);
                drawable.SetBounds(0, 0, width, height);
                drawable.SetTint(_materialButton.TextColor.ToAndroid());

                this.Control.SetCompoundDrawables(drawable, null, null, null);
                this.Control.CompoundDrawablePadding = 8;
            }
        }
Exemplo n.º 6
0
        private GradientDrawable CreateShapeDrawable(float cornerRadius, int borderWidth, Color backgroundColor, Color borderColor)
        {
            GradientDrawable shapeDrawable = null;

            if (_button.ButtonType != MaterialButtonType.Text)
            {
                shapeDrawable = _withIcon ? MaterialHelper.GetDrawableCopyFromResource <GradientDrawable>(Resource.Drawable.drawable_shape_with_icon) : MaterialHelper.GetDrawableCopyFromResource <GradientDrawable>(Resource.Drawable.drawable_shape);
            }
            else
            {
                shapeDrawable = MaterialHelper.GetDrawableCopyFromResource <GradientDrawable>(Resource.Drawable.drawable_shape_text);
            }

            shapeDrawable.SetCornerRadius(cornerRadius);
            shapeDrawable.SetColor(backgroundColor);
            shapeDrawable.SetStroke(borderWidth, borderColor);

            return(shapeDrawable);
        }
Exemplo n.º 7
0
        private void CreateContainedButtonDrawable(bool elevated)
        {
            var normalStateDrawable   = this.CreateShapeDrawable(_cornerRadius, _borderWidth, _normalColor, _borderColor);
            var disabledStateDrawable = this.CreateShapeDrawable(_cornerRadius, _borderWidth, _disabledColor, _disabledBorderColor);

            if (Material.IsLollipop)
            {
                var rippleColor       = _normalColor.IsColorDark() ? Color.ParseColor("#52FFFFFF") : Color.ParseColor("#52000000");
                var rippleDrawable    = _withIcon ? MaterialHelper.GetDrawableCopyFromResource <RippleDrawable>(Resource.Drawable.drawable_ripple_with_icon) : MaterialHelper.GetDrawableCopyFromResource <RippleDrawable>(Resource.Drawable.drawable_ripple);
                var insetDrawable     = rippleDrawable.FindDrawableByLayerId(Resource.Id.inset_drawable) as InsetDrawable;
                var statelistDrawable = insetDrawable.Drawable as StateListDrawable;
                this.SetStates(statelistDrawable, normalStateDrawable, normalStateDrawable, disabledStateDrawable);

                rippleDrawable.SetColor(new Android.Content.Res.ColorStateList(new int[][]
                {
                    new int[] {}
                },
                                                                               new int[]
                {
                    rippleColor
                }));
                this.Control.Background        = rippleDrawable;
                this.Control.StateListAnimator = elevated ? AnimatorInflater.LoadStateListAnimator(this.Context, Resource.Animator.material_button_state_list_anim) : null;
            }

            else if (Material.IsJellyBean)
            {
                var stateListDrawable    = new StateListDrawable();
                var pressedStateDrawable = this.CreateShapeDrawable(_cornerRadius, _borderWidth, _pressedColor, _borderColor);
                this.SetStates(stateListDrawable, normalStateDrawable, pressedStateDrawable, disabledStateDrawable);
                this.Control.Background = stateListDrawable;
            }

            else
            {
                var insetDrawable        = MaterialHelper.GetDrawableCopyFromResource <InsetDrawable>(Resource.Drawable.drawable_selector);
                var stateListDrawable    = insetDrawable.Drawable as StateListDrawable;
                var pressedStateDrawable = this.CreateShapeDrawable(_cornerRadius, _borderWidth, _pressedColor, _borderColor);
                this.SetStates(stateListDrawable, normalStateDrawable, pressedStateDrawable, disabledStateDrawable);
                this.Control.Background = insetDrawable;
            }
        }
Exemplo n.º 8
0
        private void CreateOutlinedButtonDrawable()
        {
            var normalStateDrawable = this.CreateShapeDrawable(_cornerRadius, _borderWidth, _normalColor, _borderColor);

            normalStateDrawable.SetColor(Color.Transparent);

            var disabledStateDrawable = this.CreateShapeDrawable(_cornerRadius, _borderWidth, _disabledColor, _disabledBorderColor);

            disabledStateDrawable.SetColor(Color.Transparent);

            if (Material.IsLollipop)
            {
                var rippleDrawable    = _withIcon ? MaterialHelper.GetDrawableCopyFromResource <RippleDrawable>(Resource.Drawable.drawable_ripple_outlined_with_icon) : MaterialHelper.GetDrawableCopyFromResource <RippleDrawable>(Resource.Drawable.drawable_ripple_outlined);
                var insetDrawable     = rippleDrawable.FindDrawableByLayerId(Resource.Id.inset_drawable) as InsetDrawable;
                var statelistDrawable = insetDrawable.Drawable as StateListDrawable;
                this.SetStates(statelistDrawable, normalStateDrawable, normalStateDrawable, disabledStateDrawable);

                this.Control.Background        = rippleDrawable;
                this.Control.StateListAnimator = null;
            }

            else if (Material.IsJellyBean)
            {
                var stateListDrawable    = new StateListDrawable();
                var pressedStateDrawable = this.CreateShapeDrawable(_cornerRadius, _borderWidth, _pressedColor, _borderColor);
                this.SetStates(stateListDrawable, normalStateDrawable, pressedStateDrawable, disabledStateDrawable);
                this.Control.Background = stateListDrawable;
            }

            else
            {
                var insetDrawable        = MaterialHelper.GetDrawableCopyFromResource <InsetDrawable>(Resource.Drawable.drawable_selector);
                var stateListDrawable    = insetDrawable.Drawable as StateListDrawable;
                var pressedStateDrawable = this.CreateShapeDrawable(_cornerRadius, _borderWidth, _pressedColor, _borderColor);
                this.SetStates(stateListDrawable, normalStateDrawable, pressedStateDrawable, disabledStateDrawable);
                this.Control.Background = insetDrawable;
            }
        }
Exemplo n.º 9
0
        private void CreateTextButtonDrawable()
        {
            #region Recursive logic to get parent with background color

            //var parentView = _materialButton.Parent as VisualElement;
            //var parentColor = parentView.BackgroundColor;

            //while(parentColor.IsDefault)
            //{
            //    parentView = parentView.Parent as VisualElement;

            //    if(parentView == null)
            //    {
            //        break;
            //    }

            //    parentColor = parentView.BackgroundColor;
            //}

            //_normalColor = parentColor.ToAndroid();
            //_cornerRadius = MaterialHelper.ConvertToDp(4);

            //var normalStateDrawable = this.CreateShapeDrawable(_cornerRadius, 0, _normalColor, Color.Transparent);
            //var disabledStateDrawable = this.CreateShapeDrawable(_cornerRadius, 0, Color.Transparent, Color.Transparent);


            //if (Material.IsLollipop)
            //{
            //    if (parentColor.IsDefault)
            //    {
            //        this.Control.Background = ContextCompat.GetDrawable(this.Context, Resource.Drawable.drawable_ripple_text);
            //    }

            //    else
            //    {
            //        var rippleDrawable = this.GetTemplateDrawable<RippleDrawable>();
            //        var insetDrawable = rippleDrawable.FindDrawableByLayerId(Resource.Id.inset_drawable) as InsetDrawable;
            //        var statelistDrawable = insetDrawable.Drawable as StateListDrawable;
            //        this.SetStates(statelistDrawable, normalStateDrawable, normalStateDrawable, disabledStateDrawable);
            //        this.Control.Background = rippleDrawable;
            //        this.Control.StateListAnimator = null;
            //    }
            //}

            //else
            //{
            //    var insetDrawable = this.GetTemplateDrawable<InsetDrawable>();
            //    var stateListDrawable = insetDrawable.Drawable as StateListDrawable;
            //    var pressedStateDrawable = this.CreateShapeDrawable(_cornerRadius, 0, _pressedColor, Color.Transparent);
            //    this.SetStates(stateListDrawable, normalStateDrawable, pressedStateDrawable, disabledStateDrawable);
            //    this.Control.Background = insetDrawable;
            //}

            #endregion

            if (Material.IsLollipop)
            {
                this.Control.Background = MaterialHelper.GetDrawableCopyFromResource <Drawable>(Resource.Drawable.drawable_ripple_text);
            }

            else if (Material.IsJellyBean)
            {
                var normalStateDrawable  = this.CreateShapeDrawable(_cornerRadius, _borderWidth, Color.Transparent, Color.Transparent);
                var pressedStateDrawable = this.CreateShapeDrawable(_cornerRadius, _borderWidth, Color.ParseColor("#52000000"), Color.Transparent);
                var stateListDrawable    = new StateListDrawable();
                this.SetStates(stateListDrawable, normalStateDrawable, pressedStateDrawable, normalStateDrawable);
                this.Control.Background = stateListDrawable;
            }

            else
            {
                var normalStateDrawable  = this.CreateShapeDrawable(_cornerRadius, _borderWidth, Color.Transparent, Color.Transparent);
                var pressedStateDrawable = this.CreateShapeDrawable(_cornerRadius, _borderWidth, Color.ParseColor("#52000000"), Color.Transparent);
                var insetDrawable        = MaterialHelper.GetDrawableCopyFromResource <InsetDrawable>(Resource.Drawable.drawable_selector);
                var stateListDrawable    = insetDrawable.Drawable as StateListDrawable;
                this.SetStates(stateListDrawable, normalStateDrawable, pressedStateDrawable, normalStateDrawable);
                this.Control.Background = insetDrawable;
            }
        }