private void ProcessButtonSize(List <string> classes)
 {
     if (classes.Any(o => o.Equals(Constant.BtnXS)))
     {
         Element.FontSize = Device.GetNamedSize(NamedSize.Micro, typeof(Xamarin.Forms.Button));
         thisButton.SetPadding(8, 7, 8, 7);
         Element.HeightRequest = Element.FontSize + 14 + 10;
     }
     else if (classes.Any(o => o.Equals(Constant.BtnSM)))
     {
         Element.FontSize = Device.GetNamedSize(NamedSize.Small, typeof(Xamarin.Forms.Button));
     }
     else if (classes.Any(o => o.Equals(Constant.BtnMD)))
     {
         Element.FontSize = Device.GetNamedSize(NamedSize.Medium, typeof(Xamarin.Forms.Button));
         thisButton.SetPadding(18, 15, 18, 15);
         Element.HeightRequest = Element.FontSize + 30 + 10;
     }
     else if (classes.Any(o => o.Equals(Constant.BtnLG)))
     {
         Element.FontSize = Device.GetNamedSize(NamedSize.Large, typeof(Xamarin.Forms.Button));
         thisButton.SetPadding(21, 17, 21, 17);
         Element.HeightRequest = Element.FontSize + 34 + 10;
     }
 }
예제 #2
0
        protected override void OnElementChanged(ElementChangedEventArgs <Xamarin.Forms.Button> e)
        {
            base.OnElementChanged(e);

            thisButton = Control as Android.Widget.Button;
            if (thisButton != null)
            {
                thisButton.SetMaxLines(1);
                thisButton.Ellipsize = TruncateAt.End;
            }
            List <string> classes = Element.GetValue(ResponsiveProperty.ClassProperty)?.ToString().Split(" ".ToCharArray()).ToList();

            if (classes != null)
            {
                ProcessButtonTheme(classes);

                ProcessButtonSize(classes);
            }


            var padding = (Thickness)Element.GetValue(ButtonProperty.PaddingProperty);

            if (padding.Left != -100 && padding.Top != -100 && padding.Right != -100 && padding.Bottom != -100)
            {
                int top    = int.Parse(padding.Top.ToString());
                int left   = int.Parse(padding.Left.ToString());
                int bottom = int.Parse(padding.Bottom.ToString());
                int right  = int.Parse(padding.Right.ToString());

                thisButton.SetPadding(left, top, right, bottom);
                Element.HeightRequest = Element.FontSize + padding.VerticalThickness + 10;
            }
        }
        protected override void OnElementChanged(ElementChangedEventArgs <Xamarin.Forms.Button> e)
        {
            base.OnElementChanged(e);
            if (e.NewElement != null && this.Control != null)
            {
                if (iconSpan == null)
                {
                    nativeBtn  = (Android.Widget.Button) this.Control;
                    iconButton = (IconButton)e.NewElement;

                    iconFont            = TrySetFont("fontawesome-webfont.ttf");
                    textFont            = iconButton.Font.ToTypeface();
                    iconButton.IconSize = iconButton.IconSize == 0 ? (float)iconButton.FontSize : iconButton.IconSize;
                    var computedString = BuildRawTextString();

                    iconSpan = BuildSpannableString(computedString);
                    if (iconButton.TextAlignement == Xamarin.Forms.TextAlignment.Center)
                    {
                        nativeBtn.Gravity = Android.Views.GravityFlags.Center;
                    }
                    else if (iconButton.TextAlignement == Xamarin.Forms.TextAlignment.End)
                    {
                        nativeBtn.Gravity = Android.Views.GravityFlags.Right;
                    }
                    else if (iconButton.TextAlignement == Xamarin.Forms.TextAlignment.Start)
                    {
                        nativeBtn.Gravity = Android.Views.GravityFlags.Left;
                    }
                    nativeBtn.TransformationMethod = null;
                    nativeBtn.SetPadding(0, 0, 0, 0);
                    nativeBtn.AfterTextChanged += nativeBtn_AfterTextChanged;
                }
            }
        }
        protected override void OnElementChanged(ElementChangedEventArgs <Xamarin.Forms.Button> e)
        {
            base.OnElementChanged(e);
            Android.Widget.Button button = Control as Android.Widget.Button;

            if (button != null)
            {
                button.Gravity = GravityFlags.Center;
                button.SetPadding(0, 0, 0, 0);
            }
        }
        private Android.Widget.Button GetButton(ToolbarItem item)
        {
            var button = new Android.Widget.Button(_rightMenuLayout.Context)
            {
                Text = item.Text
            };

            button.Click += (object sender, System.EventArgs e) => {
                item.Activate();
            };
            //button.SetTextColor(Element.BarTextColor.ToAndroid());
            button.SetTextColor(Android.Graphics.Color.Red);
            button.SetBackgroundColor(Android.Graphics.Color.Transparent);
            button.SetMinWidth(10);
            button.SetMinimumWidth(10);
            button.SetPadding(5, 0, 5, 0);
            button.LayoutParameters = new LayoutParams(LayoutParams.WrapContent, LayoutParams.WrapContent);
            return(button);
        }
예제 #6
0
        public View BuildNativeImageButton(string text, string icon, System.Action clickAction)
        {
            if (string.IsNullOrWhiteSpace(text))
            {
                text = "(text binding not set)";
            }
            if (string.IsNullOrWhiteSpace(icon))
            {
                icon = "ic_facebook_white.png";
            }

            Android.Widget.Button imgButton = new Android.Widget.Button(Forms.Context)
            {
                Text = text
            };

            if (!string.IsNullOrWhiteSpace(icon))
            {
                int imgRes = 0;
                if (icon == "ic_facebook_white.png")
                {
                    imgRes = Resource.Drawable.ic_facebook_white;
                }
                if (imgRes != 0)
                {
                    imgButton.SetCompoundDrawablesWithIntrinsicBounds(imgRes, 0, 0, 0);
                }
            }
            imgButton.Click += (sender, e) => { clickAction(); };
            imgButton.SetBackgroundResource(Resource.Drawable.bg_button_border);

            var paddingInPixels = ConvertDpToPixels(10);

            imgButton.SetPadding(paddingInPixels, 0, paddingInPixels, 0);

            imgButton.TextAlignment = Android.Views.TextAlignment.Center;

            return(imgButton.ToView());
        }
        protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.Button> e)
        {

            base.OnElementChanged(e);
            if (e.NewElement != null && this.Control != null)
            {
                if (iconSpan == null)
                {
                    nativeBtn = (Android.Widget.Button)this.Control;
                    iconButton = (IconButton)e.NewElement;

                    iconFont = TrySetFont("fontawesome-webfont.ttf");
                    textFont = iconButton.Font.ToTypeface();
                    iconButton.IconSize = iconButton.IconSize == 0 ? (float)iconButton.FontSize : iconButton.IconSize;
                    var computedString = BuildRawTextString();

                    iconSpan = BuildSpannableString(computedString);
                    if (iconButton.TextAlignement == Xamarin.Forms.TextAlignment.Center)
                    {
                        nativeBtn.Gravity = Android.Views.GravityFlags.Center;

                    }
                    else if (iconButton.TextAlignement == Xamarin.Forms.TextAlignment.End)
                    {
                        nativeBtn.Gravity = Android.Views.GravityFlags.Right;
                    }
                    else if (iconButton.TextAlignement == Xamarin.Forms.TextAlignment.Start)
                    {
                        nativeBtn.Gravity = Android.Views.GravityFlags.Left;
                    }
                    nativeBtn.TransformationMethod = null;
                    nativeBtn.SetPadding(0, 0, 0, 0);
                    nativeBtn.AfterTextChanged += nativeBtn_AfterTextChanged;
                }
            }


        }
        protected override void OnElementChanged(ElementChangedEventArgs <Xamarin.Forms.Button> e)
        {
            base.OnElementChanged(e);

            thisButton = Control as Android.Widget.Button;
            if (thisButton != null)
            {
                thisButton.SetMaxLines(1);
                thisButton.Ellipsize = TruncateAt.End;
            }
            List <string> classes = Element.GetValue(ResponsiveProperty.ClassProperty)?.ToString().Split(" ".ToCharArray()).ToList();

            if (classes != null)
            {
                ProcessButtonTheme(classes);

                ProcessButtonSize(classes);
            }

            //Handle Button Pading
            var padding = (Thickness)Element.GetValue(ButtonProperty.PaddingProperty);

            if (padding.Left != -100 && padding.Top != -100 && padding.Right != -100 && padding.Bottom != -100)
            {
                int top    = int.Parse(padding.Top.ToString());
                int left   = int.Parse(padding.Left.ToString());
                int bottom = int.Parse(padding.Bottom.ToString());
                int right  = int.Parse(padding.Right.ToString());

                thisButton.SetPadding(left, top, right, bottom);
                Element.HeightRequest = Element.FontSize + padding.VerticalThickness + 10;
            }


            // Handle PressedBackgroundColor
            pressedBackgroundColor = (Xamarin.Forms.Color)Element.GetValue(ButtonProperty.PressedBackgroundColorProperty);
            if (!pressedBackgroundColor.Equals(Color.Default))
            {
                originalBackgroundColor = Element.BackgroundColor;

                //Handle TextColor
                pressedTextColor = (Xamarin.Forms.Color)Element.GetValue(ButtonProperty.PressedTextColorProperty);
                if (!pressedTextColor.Equals(Color.Default))
                {
                    originalTextColor = Element.TextColor;
                }

                thisButton.Touch += ButtonTouched;
                thisButton.SetSingleLine(true);
                isToggleButton = Convert.ToBoolean(Element.GetValue(ButtonProperty.IsToggleButtonProperty));
            }

            // Handle Horizontal Content Alignment
            var horizontalContentAlignment = (ButtonProperty.HorizontalContentAlignmentType)Element.GetValue(ButtonProperty.HorizontalContentAlignmentProperty);

            if (horizontalContentAlignment != ButtonProperty.HorizontalContentAlignmentType.PlatformDefault)
            {
                switch (horizontalContentAlignment)
                {
                case ButtonProperty.HorizontalContentAlignmentType.Fill:
                    thisButton.Gravity = thisButton.Gravity | GravityFlags.FillHorizontal;
                    break;

                case ButtonProperty.HorizontalContentAlignmentType.Left:
                    thisButton.Gravity = thisButton.Gravity | GravityFlags.Left;
                    break;

                case ButtonProperty.HorizontalContentAlignmentType.Right:
                    thisButton.Gravity = thisButton.Gravity | GravityFlags.Right;
                    break;

                case ButtonProperty.HorizontalContentAlignmentType.Centre:
                    thisButton.Gravity = thisButton.Gravity | GravityFlags.CenterHorizontal;
                    break;
                }
            }

            // Handle Vertical Content Alignment
            var verticalContentAlignment = (ButtonProperty.VerticalContentAlignmentType)Element.GetValue(ButtonProperty.VerticalContentAlignmentProperty);

            if (verticalContentAlignment != ButtonProperty.VerticalContentAlignmentType.PlatformDefault)
            {
                switch (verticalContentAlignment)
                {
                case ButtonProperty.VerticalContentAlignmentType.Fill:
                    thisButton.Gravity = thisButton.Gravity | GravityFlags.FillVertical;
                    break;

                case ButtonProperty.VerticalContentAlignmentType.Top:
                    thisButton.Gravity = thisButton.Gravity | GravityFlags.Top;
                    break;

                case ButtonProperty.VerticalContentAlignmentType.Bottom:
                    thisButton.Gravity = thisButton.Gravity | GravityFlags.Bottom;
                    break;

                case ButtonProperty.VerticalContentAlignmentType.Centre:
                    thisButton.Gravity = thisButton.Gravity | GravityFlags.CenterVertical;
                    break;
                }
            }
        }