Exemple #1
0
            public override void GetOutline(Android.Views.View view, Outline outline)
            {
                var inset        = (int)MaterialHelper.ConvertToDp(6);
                var cornerRadius = (int)MaterialHelper.ConvertToDp(_cornerRadius);

                outline.SetRoundRect(inset, inset, view.Width - inset, view.Height - inset, cornerRadius);
            }
Exemple #2
0
        private void SetButtonIcon()
        {
            var withIcon = !string.IsNullOrEmpty(_materialButton.Image);

            _helper.UpdateHasIcon(withIcon);

            if (!withIcon)
            {
                return;
            }

            var drawable = Control.GetCompoundDrawables().FirstOrDefault(s => s != null);

            if (drawable == null)
            {
                return;
            }

            var drawableCopy = drawable.GetDrawableCopy();
            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);

            drawableCopy.SetBounds(left, 0, width, height);
            drawableCopy.TintDrawable(_materialButton.TextColor.ToAndroid());

            Control.SetCompoundDrawables(drawableCopy, null, null, null);
            Control.CompoundDrawablePadding = 0;
        }
        private void UpdateStrokeColor()
        {
            var borderColor = _materialCard.BorderColor.IsDefault ? _materialCard.BackgroundColor : _materialCard.BorderColor;
            var drawable    = (GradientDrawable)this.Control.Background;

            drawable.SetStroke((int)MaterialHelper.ConvertToDp(1), borderColor.ToAndroid());
        }
Exemple #4
0
        protected override void OnElementChanged(ElementChangedEventArgs <Button> e)
        {
            base.OnElementChanged(e);

            if (Control == null)
            {
                return;
            }

            if (e?.OldElement != null)
            {
                _helper.Clean();
            }

            if (e?.NewElement == null)
            {
                return;
            }

            _materialButton = Element as MaterialButton;
            _helper         = new MaterialDrawableHelper(_materialButton, Control);
            _helper.UpdateDrawable();

            Control.SetMinimumWidth((int)MaterialHelper.ConvertToDp(64));
            Control.SetAllCaps(_materialButton != null && _materialButton.AllCaps);

            SetButtonIcon();
            SetTextColors();
            SetTextLetterSpacing();
        }
        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;
            }
        }
Exemple #6
0
        private void UpdateBorderWidth()
        {
            if (_button.ButtonType == MaterialButtonType.Text)
            {
                _borderWidth = 0;
                return;
            }

            _borderWidth = (int)MaterialHelper.ConvertToDp(_button.BorderWidth);
        }
        private void SetControl()
        {
            if (Control == null)
            {
                return;
            }

            Control.Background = new ColorDrawable(Color.Transparent.ToAndroid());
            Control.SetPadding(0, 0, 0, 0);
            Control.SetIncludeFontPadding(false);
            Control.SetMinimumHeight((int)MaterialHelper.ConvertToDp(20));

            var layoutParams = new MarginLayoutParams(Control.LayoutParameters);

            layoutParams.SetMargins(0, 0, 0, 0);
            Control.LayoutParameters = layoutParams;
        }
Exemple #8
0
        private void SetControl()
        {
            if (this.Control == null)
            {
                return;
            }

            this.Control.Background = new ColorDrawable(Color.Transparent.ToAndroid());
            this.Control.SetPadding(0, 0, 0, 0);
            this.Control.SetIncludeFontPadding(false);
            this.Control.SetMinimumHeight((int)MaterialHelper.ConvertToDp(20));

            // This removes the 'Next' button and shows a 'Done' button when the device's orientation is in landscape.
            // This prevents the crash that is caused by a `java.lang.IllegalStateException'.
            // Reported here https://github.com/xamarin/Xamarin.Forms/issues/4832.
            this.Control.ImeOptions = Android.Views.InputMethods.ImeAction.Done;
        }
Exemple #9
0
        protected override void OnElementChanged(ElementChangedEventArgs <Button> e)
        {
            base.OnElementChanged(e);

            if (e?.NewElement != null)
            {
                _materialButton = this.Element as MaterialButton;
                _cornerRadius   = MaterialHelper.ConvertToDp(_materialButton.CornerRadius);
                _borderWidth    = (int)MaterialHelper.ConvertToDp((int)_materialButton.BorderWidth);

                this.Control.SetMinimumWidth((int)MaterialHelper.ConvertToDp(64));
                this.Control.SetAllCaps(_materialButton.AllCaps);
                this.SetupColors();
                this.SetButtonIcon();
                this.UpdateDrawable();
            }
        }
Exemple #10
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;
            }
        }
Exemple #11
0
        protected override void OnElementChanged(ElementChangedEventArgs <Button> e)
        {
            base.OnElementChanged(e);

            if (e?.OldElement != null)
            {
                _helper.Clean();
            }

            if (e?.NewElement != null)
            {
                _materialButton = this.Element as MaterialButton;
                _helper         = new MaterialDrawableHelper(_materialButton, this.Control);
                _helper.UpdateDrawable();

                this.Control.SetMinimumWidth((int)MaterialHelper.ConvertToDp(64));
                this.Control.SetAllCaps(_materialButton.AllCaps);

                this.SetButtonIcon();
                this.SetTextColors();
            }
        }
Exemple #12
0
        private StateListAnimator CreateStateListAnimator()
        {
            if (_button.ButtonType != MaterialButtonType.Elevated || !_aView.Enabled)
            {
                return(null);
            }

            var stateListAnimator = new StateListAnimator();

            var objAnimTransZEnabled = ObjectAnimator.OfFloat(_aView, "translationZ", MaterialHelper.ConvertToDp(_button.Elevation.RestingElevation))
                                       .SetDuration(100);

            objAnimTransZEnabled.StartDelay = 100;

            var objAnimElevationEnabled = ObjectAnimator.OfFloat(_aView, "elevation", MaterialHelper.ConvertToDp(_button.Elevation.RestingElevation))
                                          .SetDuration(0);

            var objAnimTransZPressed = ObjectAnimator.OfFloat(_aView, "translationZ", MaterialHelper.ConvertToDp(_button.Elevation.PressedElevation))
                                       .SetDuration(100);

            var objAnimElevationPressed = ObjectAnimator.OfFloat(_aView, "elevation", MaterialHelper.ConvertToDp(_button.Elevation.RestingElevation))
                                          .SetDuration(0);

            var b = new AnimatorSet();

            b.PlayTogether(objAnimTransZEnabled, objAnimElevationEnabled);
            b.SetTarget(_aView);

            var bb = new AnimatorSet();

            bb.PlayTogether(objAnimTransZPressed, objAnimElevationPressed);
            bb.SetTarget(_aView);


            stateListAnimator.AddState(new[] { R.Attribute.StatePressed }, bb);
            stateListAnimator.AddState(new[] { R.Attribute.StateFocused, R.Attribute.StateEnabled }, bb);
            stateListAnimator.AddState(new[] { R.Attribute.StateEnabled }, b);
            stateListAnimator.AddState(new[] { R.Attribute.StateFocused }, bb);

            if (!(_aView is AppCompatImageButton))
            {
                return(stateListAnimator);
            }

            _aView.OutlineProvider = new MaterialOutlineProvider(_button.CornerRadius);
            _aView.ClipToOutline   = false;

            return(stateListAnimator);
        }
Exemple #13
0
 private void UpdateCornerRadius()
 {
     _cornerRadius = (int)MaterialHelper.ConvertToDp(_button.CornerRadius);
 }