protected override void OnElementChanged(ElementChangedEventArgs <Xamarin.Forms.Button> e) { base.OnElementChanged(e); if (Control != null) { var button = e.NewElement; // Create a drawable for the button's normal state _normal = new Android.Graphics.Drawables.GradientDrawable(); if (button.BackgroundColor.R == -1.0 && button.BackgroundColor.G == -1.0 && button.BackgroundColor.B == -1.0) { _normal.SetColor(Android.Graphics.Color.ParseColor("#672323")); } else { _normal.SetColor(button.BackgroundColor.ToAndroid()); } _normal.SetStroke((int)button.BorderWidth, button.BorderColor.ToAndroid()); _normal.SetCornerRadius(button.BorderRadius); // Create a drawable for the button's pressed state _pressed = new Android.Graphics.Drawables.GradientDrawable(); var highlight = Context.ObtainStyledAttributes(new int[] { Android.Resource.Attribute.ColorActivatedHighlight }).GetColor(0, Android.Graphics.Color.Gray); _pressed.SetColor(highlight); _pressed.SetStroke((int)button.BorderWidth, button.BorderColor.ToAndroid()); _pressed.SetCornerRadius(button.BorderRadius); // Add the drawables to a state list and assign the state list to the button var sld = new StateListDrawable(); sld.AddState(new int[] { Android.Resource.Attribute.StatePressed }, _pressed); sld.AddState(new int[] { }, _normal); Control.SetBackgroundDrawable(sld); } }
private Drawable SetupDrawable() { ColorDrawable box = new ColorDrawable(DigitBorderColor); ColorDrawable background = new ColorDrawable(DigitBackgroundColor); ColorDrawable accent = new ColorDrawable(AccentColor); LayerDrawable selectedBackground = new LayerDrawable(new Drawable[] { box, accent, background }); selectedBackground.SetLayerInset(1, 5, 5, 5, 5); selectedBackground.SetLayerInset(2, 5, 5, 5, 5 + AccentHeight); LayerDrawable defaultBackground = new LayerDrawable(new Drawable[] { box, background }); defaultBackground.SetLayerInset(1, 5, 5, 5, 5); StateListDrawable stateListDrawable = new StateListDrawable(); stateListDrawable.AddState(new int[] { Android.Resource.Attribute.StateSelected }, selectedBackground); stateListDrawable.AddState(new int[] { }, defaultBackground); return(stateListDrawable); }
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; } }
new void UpdateBackgroundColor() { base.UpdateBackgroundColor(); Android.Graphics.Color normalColor = BaseElement.BackgroundColor.ToAndroid(); Android.Graphics.Color pressedColor = BaseElement.PressedBackgroundColor.ToAndroid(); Android.Graphics.Color disabledColor = BaseElement.DisabledBackgroundColor.ToAndroid(); if (pressedColor != 0 || disabledColor != 0) { var stateListColor = new StateListDrawable(); if (disabledColor != 0) { stateListColor.AddState( new[] { -Android.Resource.Attribute.StateEnabled }, new ColorDrawable(disabledColor)); } if (pressedColor != 0) { stateListColor.AddState( new[] { Android.Resource.Attribute.StateEnabled, Android.Resource.Attribute.StatePressed }, new ColorDrawable(pressedColor)); } stateListColor.AddState(new[] { Android.Resource.Attribute.StateEnabled }, new ColorDrawable(normalColor)); Control.SetBackground(stateListColor); } }
private void LoadElements() { _pushNotification = _view.FindViewById <Switch>(Resource.Id.pushnotificationId); _pushNotification.Checked = prefs.GetBoolean("PushNotification", true); StateListDrawable thumbStates = new StateListDrawable(); int white = ContextCompat.GetColor(Activity, Resource.Color.white); int mainColor = ContextCompat.GetColor(Activity, Resource.Color.mainColor); int defaultColor = ContextCompat.GetColor(Activity, Resource.Color.grey); ColorStateList thumbStateColor = new ColorStateList( new int[][] { new int[] { an.Resource.Attribute.StateChecked }, new int[] { an.Resource.Attribute.StateEnabled }, new int[] {} }, new int[] { mainColor, defaultColor, white }); ColorStateList trackStateColor = new ColorStateList( new int[][] { new int[] { an.Resource.Attribute.StateChecked }, new int[] { an.Resource.Attribute.StateEnabled }, new int[] {} }, new int[] { white, defaultColor, defaultColor }); _pushNotification.TrackDrawable.SetTintList(trackStateColor); _pushNotification.ThumbDrawable.SetTintList(thumbStateColor); }
protected override void OnViewModelSet() { base.OnViewModelSet(); DroidResources.Initialize(typeof(Resource.Layout)); SetContentView(Resource.Layout.View_CreateAccount); ViewGroup registerContainer = (ViewGroup)FindViewById <LinearLayout>(Resource.Id.registerContainer); SetDialog(registerContainer, 0); TextView lblTermsAndConditions = (TextView)FindViewById <TextView>(Resource.Id.lblTermsAndConditions); var ResourceAcklowledge = GetString(Resource.String.TermsAndConditionsAcknowledgment); var ResourceAcklowledgeBold = GetString(Resource.String.TermsAndConditionsLabel); var textWithBold = Html.FromHtml(ResourceAcklowledge.Replace(ResourceAcklowledgeBold, "<b>" + ResourceAcklowledgeBold + "</b>")); lblTermsAndConditions.SetText(textWithBold, TextView.BufferType.Spannable); var colorTheme = DrawHelper.GetTextColorForBackground(Resources.GetColor(Resource.Color.login_color)); var checkedIcon = Resources.GetDrawable(Resource.Drawable.@checked_custom); var uncheckedIcon = Resources.GetDrawable(Resource.Drawable.@unchecked_custom); var list = new StateListDrawable(); list.AddState(new int[] { Android.Resource.Attribute.StateChecked }, checkedIcon); list.AddState(new int[] { -Android.Resource.Attribute.StateChecked }, uncheckedIcon); list.AddState(new int[] { Android.Resource.Attribute.StatePressed }, checkedIcon); list.SetColorFilter(colorTheme, PorterDuff.Mode.Multiply); var checkBox = (CheckBox)FindViewById <CheckBox>(Resource.Id.cbTermsAndConditions); checkBox.SetButtonDrawable(list); checkBox.Invalidate(); DrawHelper.SupportLoginTextColor(lblTermsAndConditions); DrawHelper.SupportLoginTextColor(FindViewById <Button>(Resource.Id.btnCancel)); DrawHelper.SupportLoginTextColor(FindViewById <Button>(Resource.Id.btnCreate)); DrawHelper.SupportLoginTextColor(FindViewById <TextView>(Resource.Id.lblTitle)); }
protected virtual void CreateContentView() { var layoutInflater = (LayoutInflater)_Context.GetSystemService(Context.LayoutInflaterService); var contentView = layoutInflater.Inflate(Resource.Layout.CellBaseView, this, true); contentView.LayoutParameters = new ViewGroup.LayoutParams(-1, -1); IconView = contentView.FindViewById <ImageView>(Resource.Id.CellIcon); TitleLabel = contentView.FindViewById <TextView>(Resource.Id.CellTitle); DescriptionLabel = contentView.FindViewById <TextView>(Resource.Id.CellDescription); ContentStack = contentView.FindViewById <LinearLayout>(Resource.Id.CellContentStack); AccessoryStack = contentView.FindViewById <LinearLayout>(Resource.Id.CellAccessoryView); HintLabel = contentView.FindViewById <TextView>(Resource.Id.CellHintText); _backgroundColor = new ColorDrawable(); _selectedColor = new ColorDrawable(Android.Graphics.Color.Argb(125, 180, 180, 180)); var sel = new StateListDrawable(); sel.AddState(new int[] { global::Android.Resource.Attribute.StateSelected }, _selectedColor); sel.AddState(new int[] { -global::Android.Resource.Attribute.StateSelected }, _backgroundColor); sel.SetExitFadeDuration(250); sel.SetEnterFadeDuration(250); var rippleColor = Android.Graphics.Color.Rgb(180, 180, 180); if (CellParent.SelectedColor != Xamarin.Forms.Color.Default) { rippleColor = CellParent.SelectedColor.ToAndroid(); } _ripple = DrawableUtility.CreateRipple(rippleColor, sel); Background = _ripple; _defaultTextColor = new Android.Graphics.Color(TitleLabel.CurrentTextColor); _defaultFontSize = TitleLabel.TextSize; }
protected RippleDrawable CreateRippleDrawable(AColor?color = null) { using var sel = new StateListDrawable(); sel.AddState(new[] { Android.Resource.Attribute.StateSelected }, SelectedColor ); sel.AddState(new[] { -Android.Resource.Attribute.StateSelected }, BackgroundColor ); sel.SetExitFadeDuration(250); sel.SetEnterFadeDuration(250); AColor rippleColor = color ?? CellParent?.SelectedColor.ToAndroid() ?? SVConstants.Cell.Ripple.ToAndroid(); return(DrawableUtility.CreateRipple(rippleColor, sel)); }
protected override void OnElementChanged(ElementChangedEventArgs <Button> e) { base.OnElementChanged(e); if (Control != null) { var button = (XButton)e.NewElement; // Setup properties button.TextColor = Color.White; // Button items properties button.SizeChanged += (s, args) => { var radius = (float)Math.Min(button.Width, button.Height); // Create a drawable for the button's normal state _normal = new GradientDrawable(); _normal.SetColor(Android.Graphics.Color.ParseColor(Constants.CODIGO_COLOR_BOTON_AZUL)); _normal.SetStroke((int)button.BorderWidth, button.BorderColor.ToAndroid()); _normal.SetCornerRadius(radius); // Create a drawable for the button's pressed state _pressed = new GradientDrawable(); var highlight = Context.ObtainStyledAttributes(new int[] { Android.Resource.Attribute.ColorActivatedHighlight }).GetColor(0, Android.Graphics.Color.Gray); _pressed.SetColor(highlight); _pressed.SetStroke((int)button.BorderWidth, button.BorderColor.ToAndroid()); _pressed.SetCornerRadius(radius); // Add the drawables to a state list and assign the state list to the button var sld = new StateListDrawable(); sld.AddState(new int[] { Android.Resource.Attribute.StatePressed }, _pressed); sld.AddState(new int[] { }, _normal); Control.Background = sld; }; } }
private ViewGroup CreateRelativeLayoutButton(HierarchyButton hierarchyButton) { var buttonRelativeLayout = hierarchyButton.GetView(context, out var isCache); if (!isCache) { buttonRelativeLayout.SetPadding(50, 0, 50, 0); buttonRelativeLayout.Clickable = true; var stateListDrawable = new StateListDrawable(); stateListDrawable.AddState(new int[] { -Android.Resource.Attribute.StatePressed }, new ColorDrawable(Android.Graphics.Color.Argb (BackGroundColor[0], BackGroundColor[1], BackGroundColor[2], BackGroundColor[3]))); stateListDrawable.AddState(new int[] { Android.Resource.Attribute.StatePressed }, new ColorDrawable(Android.Graphics.Color.Argb (PressedBackGroundColor[0], PressedBackGroundColor[1], PressedBackGroundColor[2], PressedBackGroundColor[3]))); buttonRelativeLayout.Background = stateListDrawable; buttonRelativeLayout.Click += ButtonRelativeLayout_Click; var id = View.GenerateViewId(); buttonRelativeLayout.Id = id; buttonDictionary.Add(id, hierarchyButton); } return(buttonRelativeLayout); }
Drawable CreateBackgroundForButton(bool renderImage = true) { List <Drawable> drawables = new List <Drawable>(); List <Drawable> drawablesDisabled = new List <Drawable>(); drawables.Add(BackgroundExtension.CreateBackgroundGradient(button.StartColor.ToAndroid(), button.EndColor.ToAndroid(), button.CenterColor.ToAndroid(), button.CornerRadius, button.Angle.ToAndroid())); drawablesDisabled.Add(BackgroundExtension.CreateBackgroundGradient(button.StartColor.ToAndroid(), button.EndColor.ToAndroid(), button.CenterColor.ToAndroid(), button.CornerRadius, button.Angle.ToAndroid())); if (button.Image != null && !string.IsNullOrEmpty(button.Image.File) && renderImage) { var bitmapDrawable = BackgroundExtension.CreateBackgroundBitmap(button.Image.File, (int)button.HeightRequest, (int)button.WidthRequest, button.CornerRadius); if (bitmapDrawable != null) { drawables.Add(bitmapDrawable); drawablesDisabled.Add(bitmapDrawable); } } Drawable layer = new LayerDrawable(drawables.ToArray()); Drawable layerDisabled = new LayerDrawable(drawablesDisabled.ToArray()); layerDisabled.Mutate().Alpha = 80; var statesListDrawable = new StateListDrawable(); statesListDrawable.AddState(new int[] { -Android.Resource.Attribute.StateEnabled }, layerDisabled); statesListDrawable.AddState(new int[] { Android.Resource.Attribute.StatePressed }, layer); statesListDrawable.AddState(new int[] { Android.Resource.Attribute.StateEnabled }, layer); return(statesListDrawable); }
async Task BuildBackground() { using (var statesBackground = new StateListDrawable()) { if (BaseElement.NormalImage != null) { var normalHandler = BaseElement.NormalImage.GetHandler(); using (var imgNormal = await normalHandler.LoadImageAsync(BaseElement.NormalImage, base.Context)) { statesBackground.AddState( new int[] { -global::Android.Resource.Attribute.StatePressed, global::Android.Resource.Attribute.StateEnabled }, new BitmapDrawable(imgNormal) ); if (BaseElement.PressedImage == null) { statesBackground.AddState( new int[] { global::Android.Resource.Attribute.StatePressed, global::Android.Resource.Attribute.StateEnabled }, new BitmapDrawable(imgNormal) ); } if (BaseElement.DisableImage == null) { statesBackground.AddState( new int[] { -global::Android.Resource.Attribute.StateEnabled }, new BitmapDrawable(imgNormal) ); } } } if (BaseElement.PressedImage != null) { var pressedHandler = BaseElement.PressedImage.GetHandler(); using (var imgPressed = await pressedHandler.LoadImageAsync(BaseElement.PressedImage, base.Context)) { statesBackground.AddState( new int[] { global::Android.Resource.Attribute.StatePressed, global::Android.Resource.Attribute.StateEnabled }, new BitmapDrawable(imgPressed) ); } } if (BaseElement.DisableImage != null) { var disableHandler = BaseElement.DisableImage.GetHandler(); using (var imgDisable = await disableHandler.LoadImageAsync(BaseElement.DisableImage, base.Context)) { statesBackground.AddState( new int[] { -global::Android.Resource.Attribute.StateEnabled }, new BitmapDrawable(imgDisable) ); } } if (Control != null) { Control.Background = statesBackground; } } }
public static void SetTheme(ToggleButton toggleButton, FlatTheme theme, int padding, int size) { toggleButton.SetWidth(size * 5); toggleButton.SetHeight(size); //setTextOff(""); //setTextOn(""); int radius = size - 4; float[] outerR = new float[] { radius, radius, radius, radius, radius, radius, radius, radius }; // creating unchecked-enabled state drawable var uncheckedEnabledFrontCore = new ShapeDrawable(new RoundRectShape(outerR, null, null)); uncheckedEnabledFrontCore.Paint.Color = theme.LightAccentColor; var uncheckedEnabledFront = new InsetDrawable(uncheckedEnabledFrontCore, 5); var uncheckedEnabledBack = new ShapeDrawable(new RoundRectShape(outerR, null, null)); uncheckedEnabledBack.Paint.Color = Color.ParseColor("#f2f2f2"); uncheckedEnabledBack.SetPadding(0, 0, size / 2 * 5, 0); Drawable[] d1 = { uncheckedEnabledBack, uncheckedEnabledFront }; var uncheckedEnabled = new LayerDrawable(d1); // creating checked-enabled state drawable var checkedEnabledFrontCore = new ShapeDrawable(new RoundRectShape(outerR, null, null)); checkedEnabledFrontCore.Paint.Color = theme.LightAccentColor; var checkedEnabledFront = new InsetDrawable(checkedEnabledFrontCore, 5); ShapeDrawable checkedEnabledBack = new ShapeDrawable(new RoundRectShape(outerR, null, null)); checkedEnabledBack.Paint.Color = theme.VeryLightAccentColor; checkedEnabledBack.SetPadding(size / 2 * 5, 0, 0, 0); Drawable[] d2 = { checkedEnabledBack, checkedEnabledFront }; LayerDrawable checkedEnabled = new LayerDrawable(d2); // creating unchecked-disabled state drawable ShapeDrawable uncheckedDisabledFrontCore = new ShapeDrawable(new RoundRectShape(outerR, null, null)); uncheckedDisabledFrontCore.Paint.Color = Color.ParseColor("#d2d2d2"); InsetDrawable uncheckedDisabledFront = new InsetDrawable(uncheckedDisabledFrontCore, 5); ShapeDrawable uncheckedDisabledBack = new ShapeDrawable(new RoundRectShape(outerR, null, null)); uncheckedDisabledBack.Paint.Color = Color.ParseColor("#f2f2f2"); uncheckedDisabledBack.SetPadding(0, 0, size / 2 * 5, 0); Drawable[] d3 = { uncheckedDisabledBack, uncheckedDisabledFront }; LayerDrawable uncheckedDisabled = new LayerDrawable(d3); // creating checked-disabled state drawable ShapeDrawable checkedDisabledFrontCore = new ShapeDrawable(new RoundRectShape(outerR, null, null)); checkedDisabledFrontCore.Paint.Color = theme.VeryLightAccentColor; InsetDrawable checkedDisabledFront = new InsetDrawable(checkedDisabledFrontCore, 5); ShapeDrawable checkedDisabledBack = new ShapeDrawable(new RoundRectShape(outerR, null, null)); checkedDisabledBack.Paint.Color = Color.ParseColor("#f2f2f2"); checkedDisabledBack.SetPadding(size / 2 * 5, 0, 0, 0); Drawable[] d4 = { checkedDisabledBack, checkedDisabledFront }; LayerDrawable checkedDisabled = new LayerDrawable(d4); toggleButton.SetPadding(0, padding, 0, padding); PaintDrawable paintDrawable = new PaintDrawable(theme.BackgroundColor); paintDrawable.SetIntrinsicHeight(size); paintDrawable.SetIntrinsicWidth(size); paintDrawable.SetPadding(size, 0, 0, 0); StateListDrawable states = new StateListDrawable(); states.AddState(new int[] { -Android.Resource.Attribute.StateChecked, Android.Resource.Attribute.StateEnabled }, new InsetDrawable(uncheckedEnabled, padding * 2)); states.AddState(new int[] { Android.Resource.Attribute.StateChecked, Android.Resource.Attribute.StateEnabled }, new InsetDrawable(checkedEnabled, padding * 2)); states.AddState(new int[] { -Android.Resource.Attribute.StateChecked, -Android.Resource.Attribute.StateEnabled }, new InsetDrawable(uncheckedDisabled, padding * 2)); states.AddState(new int[] { Android.Resource.Attribute.StateChecked, -Android.Resource.Attribute.StateEnabled }, new InsetDrawable(checkedDisabled, padding * 2)); toggleButton.SetBackgroundDrawable(states); toggleButton.SetTextSize(ComplexUnitType.Sp, 0); }
protected override async void OnElementChanged(ElementChangedEventArgs <Xamarin.Forms.Button> e) { base.OnElementChanged(e); UpdateAlignment(); UpdateFont(); UpdatePadding(); //UpdateMargin(); _density = Resources.DisplayMetrics.Density; var targetButton = Control; if (targetButton != null) { targetButton.SetOnTouchListener(TouchListener.Instance.Value); } if (Element != null && Element.Font != Font.Default && targetButton != null) { targetButton.Typeface = Element.Font.ToExtendedTypeface(Context); } if (Element != null && BaseButton.Source != null) { await SetImageSourceAsync(targetButton, BaseButton).ConfigureAwait(false); } if (Control != null) { var button = e.NewElement; // Create a drawable for the button's normal state _normal = new Android.Graphics.Drawables.GradientDrawable(); if (button.BackgroundColor.R == -1.0 && button.BackgroundColor.G == -1.0 && button.BackgroundColor.B == -1.0) { _normal.SetColor(Android.Graphics.Color.ParseColor(buttonWhite)); } else { _normal.SetColor(button.BackgroundColor.ToAndroid()); } _normal.SetStroke((int)button.BorderWidth, button.BorderColor.ToAndroid()); _normal.SetCornerRadius(button.BorderRadius); // Create a drawable for the button's pressed state _pressed = new Android.Graphics.Drawables.GradientDrawable(); var highlight = Android.Graphics.Color.ParseColor(buttonGrey); //var highlight = Context.ObtainStyledAttributes(new int[] { Android.Resource.Attribute.ColorActivatedHighlight }).GetColor(0, Android.Graphics.Color.ParseColor(buttonWhite)); _pressed.SetColor(highlight); _pressed.SetStroke((int)button.BorderWidth, button.BorderColor.ToAndroid()); _pressed.SetCornerRadius(button.BorderRadius); // Add the drawables to a state list and assign the state list to the button var sld = new StateListDrawable(); sld.AddState(new int[] { Android.Resource.Attribute.StatePressed }, _pressed); sld.AddState(new int[] { }, _normal); Control.SetBackgroundDrawable(sld); } }
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; } }
public static void SetTheme(Button button, FlatTheme theme, FlatUI.FlatTextAppearance textAppearance, FlatUI.FlatFontFamily fontFamily, FlatUI.FlatFontWeight fontWeight, bool isFullFlat, int padding, int radius) { var bottom = 5; float[] outerR = { radius, radius, radius, radius, radius, radius, radius, radius }; // creating normal state drawable var normalFront = new ShapeDrawable(new RoundRectShape(outerR, null, null)); normalFront.Paint.Color = theme.LightAccentColor; normalFront.SetPadding(padding, padding, padding, padding); var normalBack = new ShapeDrawable(new RoundRectShape(outerR, null, null)); normalBack.Paint.Color = theme.BackgroundColor; if (isFullFlat) { bottom = 0; } normalBack.SetPadding(0, 0, 0, bottom); Drawable[] d = { normalBack, normalFront }; var normal = new LayerDrawable(d); // creating pressed state drawable var pressedFront = new ShapeDrawable(new RoundRectShape(outerR, null, null)); pressedFront.Paint.Color = theme.BackgroundColor; var pressedBack = new ShapeDrawable(new RoundRectShape(outerR, null, null)); pressedBack.Paint.Color = theme.DarkAccentColor; if (!isFullFlat) { pressedBack.SetPadding(0, 0, 0, 3); } Drawable[] d2 = { pressedBack, pressedFront }; var pressed = new LayerDrawable(d2); // creating disabled state drawable var disabledFront = new ShapeDrawable(new RoundRectShape(outerR, null, null)); disabledFront.Paint.Color = theme.VeryLightAccentColor; var disabledBack = new ShapeDrawable(new RoundRectShape(outerR, null, null)); disabledBack.Paint.Color = theme.LightAccentColor; if (!isFullFlat) { disabledBack.SetPadding(0, 0, 0, padding); } Drawable[] d3 = { disabledBack, disabledFront }; var disabled = new LayerDrawable(d3); var states = new StateListDrawable(); states.AddState(new [] { Android.Resource.Attribute.StatePressed, Android.Resource.Attribute.StateEnabled }, pressed); states.AddState(new [] { Android.Resource.Attribute.StateFocused, Android.Resource.Attribute.StateEnabled }, pressed); states.AddState(new [] { Android.Resource.Attribute.StateEnabled }, normal); states.AddState(new [] { -Android.Resource.Attribute.StateEnabled }, disabled); button.SetBackgroundDrawable(states); if (textAppearance == FlatUI.FlatTextAppearance.Dark) { button.SetTextColor(theme.DarkAccentColor); } else if (textAppearance == FlatUI.FlatTextAppearance.Light) { button.SetTextColor(theme.VeryLightAccentColor); } else { button.SetTextColor(Android.Graphics.Color.White); } var typeface = FlatUI.GetFont(button.Context, fontFamily, fontWeight); if (typeface != null) { button.SetTypeface(typeface, Android.Graphics.TypefaceStyle.Normal); } }
protected virtual async Task BuildBackground() { try { using (var statesBackground = new StateListDrawable()) { if (BaseElement.NormalImage != null) { var normalHandler = BaseElement.NormalImage.GetHandler(); if (normalHandler != null) { using (var imgNormal = await normalHandler.LoadImageAsync(BaseElement.NormalImage, base.Context)) { if (imgNormal != null) { statesBackground.AddState( new int[] { -global::Android.Resource.Attribute.StatePressed, global::Android.Resource.Attribute.StateEnabled }, new BitmapDrawable(imgNormal) ); if (BaseElement.PressedImage == null) { statesBackground.AddState( new int[] { global::Android.Resource.Attribute.StatePressed, global::Android.Resource.Attribute.StateEnabled }, new BitmapDrawable(imgNormal) ); } if (BaseElement.DisableImage == null) { statesBackground.AddState( new int[] { -global::Android.Resource.Attribute.StateEnabled }, new BitmapDrawable(imgNormal) ); } } } } } if (BaseElement.PressedImage != null) { var pressedHandler = BaseElement.PressedImage.GetHandler(); if (pressedHandler != null) { using (var imgPressed = await pressedHandler.LoadImageAsync(BaseElement.PressedImage, base.Context)) { if (imgPressed != null) { statesBackground.AddState( new int[] { global::Android.Resource.Attribute.StatePressed, global::Android.Resource.Attribute.StateEnabled }, new BitmapDrawable(imgPressed) ); } } } } if (BaseElement.DisableImage != null) { var disableHandler = BaseElement.DisableImage.GetHandler(); if (disableHandler != null) { using (var imgDisable = await disableHandler.LoadImageAsync(BaseElement.DisableImage, base.Context)) { if (imgDisable != null) { statesBackground.AddState( new int[] { -global::Android.Resource.Attribute.StateEnabled }, new BitmapDrawable(imgDisable) ); } } } } if (Control != null) { Control.Background = statesBackground; } } } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.Message); } }
public ViewHolder(View itemView) : base(itemView) { Title = itemView.FindViewById <TextView>(Resource.Id.text); Background = itemView.Background as StateListDrawable; }
public static void SetTheme(CheckBox checkBox, FlatTheme theme, FlatUI.FlatFontFamily fontFamily, FlatUI.FlatFontWeight fontWeight, int radius, int size, int border) { // creating unchecked-enabled state drawable GradientDrawable uncheckedEnabled = new GradientDrawable(); uncheckedEnabled.SetCornerRadius(radius); uncheckedEnabled.SetSize(size, size); uncheckedEnabled.SetColor(Color.Transparent); uncheckedEnabled.SetStroke(border, theme.LightAccentColor); // creating checked-enabled state drawable GradientDrawable checkedOutside = new GradientDrawable(); checkedOutside.SetCornerRadius(radius); checkedOutside.SetSize(size, size); checkedOutside.SetColor(Color.Transparent); checkedOutside.SetStroke(border, theme.LightAccentColor); PaintDrawable checkedCore = new PaintDrawable(theme.LightAccentColor); checkedCore.SetCornerRadius(radius); checkedCore.SetIntrinsicHeight(size); checkedCore.SetIntrinsicWidth(size); InsetDrawable checkedInside = new InsetDrawable(checkedCore, border + 2, border + 2, border + 2, border + 2); Drawable[] checkedEnabledDrawable = { checkedOutside, checkedInside }; LayerDrawable checkedEnabled = new LayerDrawable(checkedEnabledDrawable); // creating unchecked-enabled state drawable GradientDrawable uncheckedDisabled = new GradientDrawable(); uncheckedDisabled.SetCornerRadius(radius); uncheckedDisabled.SetSize(size, size); uncheckedDisabled.SetColor(Color.Transparent); uncheckedDisabled.SetStroke(border, theme.VeryLightAccentColor); // creating checked-disabled state drawable GradientDrawable checkedOutsideDisabled = new GradientDrawable(); checkedOutsideDisabled.SetCornerRadius(radius); checkedOutsideDisabled.SetSize(size, size); checkedOutsideDisabled.SetColor(Color.Transparent); checkedOutsideDisabled.SetStroke(border, theme.VeryLightAccentColor); PaintDrawable checkedCoreDisabled = new PaintDrawable(theme.VeryLightAccentColor); checkedCoreDisabled.SetCornerRadius(radius); checkedCoreDisabled.SetIntrinsicHeight(size); checkedCoreDisabled.SetIntrinsicWidth(size); InsetDrawable checkedInsideDisabled = new InsetDrawable(checkedCoreDisabled, border + 2, border + 2, border + 2, border + 2); Drawable[] checkedDisabledDrawable = { checkedOutsideDisabled, checkedInsideDisabled }; LayerDrawable checkedDisabled = new LayerDrawable(checkedDisabledDrawable); StateListDrawable states = new StateListDrawable(); states.AddState(new int[] { -Android.Resource.Attribute.StateChecked, Android.Resource.Attribute.StateEnabled }, uncheckedEnabled); states.AddState(new int[] { Android.Resource.Attribute.StateChecked, Android.Resource.Attribute.StateEnabled }, checkedEnabled); states.AddState(new int[] { -Android.Resource.Attribute.StateChecked, -Android.Resource.Attribute.StateEnabled }, uncheckedDisabled); states.AddState(new int[] { Android.Resource.Attribute.StateChecked, -Android.Resource.Attribute.StateEnabled }, checkedDisabled); checkBox.SetButtonDrawable(states); // setting padding for avoiding text to be appear on icon checkBox.SetPadding(size / 4 * 5, 0, 0, 0); checkBox.SetTextColor(theme.LightAccentColor); var typeface = FlatUI.GetFont(checkBox.Context, fontFamily, fontWeight); if (typeface != null) { checkBox.SetTypeface(typeface, TypefaceStyle.Normal); } }
protected override void OnElementChanged(ElementChangedEventArgs <Xamarin.Forms.Button> e) { base.OnElementChanged(e); if (e.OldElement != null || e.NewElement == null) { return; } var buttonControl = (RoundedButton)Element; gradientDrawable = new StateListDrawable(); SetCorners(); Control.Focusable = false; buttonControl.PropertyChanged += (s, ev) => { if (Control == null || Element == null) { return; } var element = (RoundedButton)s; switch (ev.PropertyName) { case "IsEnabled": Control.SetTextColor(element.TextColor.ToAndroid()); // for samsung s4 and note5 break; case "Text": Control.Text = element.Text; var width = Control.Width; Control.SetWidth(width); break; case "HoverColor": SetHoverColor(); Control.Background = gradientDrawable; break; case "PressedColor": SetPressedColor(); Control.Background = gradientDrawable; break; case "GradientColor": UpdateStateDrawable(); break; case "DisableColor": SetDisabledColor(); Control.Background = gradientDrawable; break; case "BorderColor": UpdateStateDrawable(); break; //case "Width": // SetImages(buttonControl); // break; case "DisabledTextColor": SetDisabledTextColor(); break; } }; //SetTypeface(buttonControl); //SetBackground(buttonControl); //SetImages(buttonControl); //SetPadding(buttonControl); //SetTitlePadding(buttonControl); //SetTextAlignment(buttonControl); currentWidth = Control.Width; if (buttonControl.WidthRequest > 0) { Control.SetWidth(BaseUIHelper.ConvertDPToPixels(buttonControl.WidthRequest)); } if (buttonControl.HeightRequest > 0) { Control.SetHeight(BaseUIHelper.ConvertDPToPixels(buttonControl.HeightRequest)); } SetPressedColor(); SetHoverColor(); SetDisabledColor(); SetGradientColor(); SetDisabledTextColor(); SetTextAlignment(); Control.Background = gradientDrawable; var thisButton = Control as Android.Widget.Button; thisButton.Touch += async(object sender, TouchEventArgs args) => { if (args.Event.Action == MotionEventActions.Down) { await buttonControl.ScaleTo(0.9, 100); } else if (args.Event.Action == MotionEventActions.Up) { await buttonControl.ScaleTo(1, 100); Control.CallOnClick(); } }; this.SetWillNotDraw(false); }
public static void SetTheme(Android.Widget.CheckBox checkBox, int radius, int size, int border, Android.Graphics.Color background) { // creating unchecked-enabled state drawable GradientDrawable uncheckedEnabled = new GradientDrawable(); uncheckedEnabled.SetCornerRadius(radius); uncheckedEnabled.SetSize(size, size); uncheckedEnabled.SetColor(Android.Graphics.Color.Transparent); uncheckedEnabled.SetStroke(border, Android.Graphics.Color.Black); // creating checked-enabled state drawable GradientDrawable checkedOutside = new GradientDrawable(); checkedOutside.SetCornerRadius(radius); checkedOutside.SetSize(size, size); checkedOutside.SetColor(Android.Graphics.Color.Transparent); checkedOutside.SetStroke(border, Android.Graphics.Color.Black); PaintDrawable checkedCore = new PaintDrawable(background); checkedCore.SetCornerRadius(radius); checkedCore.SetIntrinsicHeight(size); checkedCore.SetIntrinsicWidth(size); InsetDrawable checkedInside = new InsetDrawable(checkedCore, border + 2, border + 2, border + 2, border + 2); Drawable[] checkedEnabledDrawable = { checkedOutside, checkedInside }; LayerDrawable checkedEnabled = new LayerDrawable(checkedEnabledDrawable); // creating unchecked-enabled state drawable GradientDrawable uncheckedDisabled = new GradientDrawable(); uncheckedDisabled.SetCornerRadius(radius); uncheckedDisabled.SetSize(size, size); uncheckedDisabled.SetColor(Android.Graphics.Color.Transparent); uncheckedDisabled.SetStroke(border, Android.Graphics.Color.Black); // creating checked-disabled state drawable GradientDrawable checkedOutsideDisabled = new GradientDrawable(); checkedOutsideDisabled.SetCornerRadius(radius); checkedOutsideDisabled.SetSize(size, size); checkedOutsideDisabled.SetColor(Android.Graphics.Color.Transparent); checkedOutsideDisabled.SetStroke(border, Android.Graphics.Color.Black); PaintDrawable checkedCoreDisabled = new PaintDrawable(background); checkedCoreDisabled.SetCornerRadius(radius); checkedCoreDisabled.SetIntrinsicHeight(size); checkedCoreDisabled.SetIntrinsicWidth(size); InsetDrawable checkedInsideDisabled = new InsetDrawable(checkedCoreDisabled, border + 2, border + 2, border + 2, border + 2); Drawable[] checkedDisabledDrawable = { checkedOutsideDisabled, checkedInsideDisabled }; LayerDrawable checkedDisabled = new LayerDrawable(checkedDisabledDrawable); StateListDrawable states = new StateListDrawable(); states.AddState(new int[] { -Android.Resource.Attribute.StateChecked, Android.Resource.Attribute.StateEnabled }, uncheckedEnabled); states.AddState(new int[] { Android.Resource.Attribute.StateChecked, Android.Resource.Attribute.StateEnabled }, checkedEnabled); states.AddState(new int[] { -Android.Resource.Attribute.StateChecked, -Android.Resource.Attribute.StateEnabled }, uncheckedDisabled); states.AddState(new int[] { Android.Resource.Attribute.StateChecked, -Android.Resource.Attribute.StateEnabled }, checkedDisabled); checkBox.SetButtonDrawable(states); // setting padding for avoiding text to be appear on icon checkBox.SetPadding(size / 4 * 5, 0, 0, 0); checkBox.SetTextColor(Android.Graphics.Color.LightYellow); checkBox.SetTextSize(Android.Util.ComplexUnitType.Sp, 10); checkBox.SetTypeface(Typeface.Default, TypefaceStyle.Normal); }
protected override void OnElementChanged(ElementChangedEventArgs <CustomCheckBox> e) { base.OnElementChanged(e); var linearLayout = new LinearLayout(base.Context); linearLayout.Orientation = Orientation.Horizontal; if (this.Control == null) { checkBox = new Android.Widget.CheckBox(this.Context); checkBox.CheckedChange += CheckBoxCheckedChange; linearLayout.AddView(checkBox); this.SetNativeControl(linearLayout); } checkBox.Checked = e.NewElement.Checked; checkBox.SetTextColor(e.NewElement.TextColor.ToAndroid()); checkBox.CompoundDrawablePadding = 15; int spacing = checkBox.PaddingLeft; if (Element.Spacing.HasValue) { spacing = BaseUIHelper.ConvertDPToPixels(Element.Spacing.Value); } checkBox.SetPadding(spacing, checkBox.PaddingTop, checkBox.PaddingRight, checkBox.PaddingBottom); if (e.NewElement.EnableLink) { checkBox.Text = ""; var textView = new TextView(base.Context); textView.SetTextColor(e.NewElement.TextColor.ToAndroid()); var color = e.NewElement.TextColor.ToHex(); textView.TextFormatted = Html.FromHtml("<span style='color: " + color + "'>" + e.NewElement.Text + "</span> <a href='" + e.NewElement.LinkString + "' style='color: " + color + "'>" + e.NewElement.InlineText + "</a>"); textView.MovementMethod = LinkMovementMethod.Instance; textView.Gravity = Android.Views.GravityFlags.CenterVertical; Control.AddView(textView); } else { checkBox.Text = e.NewElement.Text; } int id = -1; if (Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.Lollipop) { id = this.Context.Resources.GetIdentifier("btn_check_material_anim", "drawable", "android"); } else { id = this.Context.Resources.GetIdentifier("btn_check_holo_light", "drawable", "android"); } if (Element.CheckedImage != null && Element.UnCheckedImage != null) { var checkedDrawable = this.Resources.GetDrawable(UIHelper.GetDrawableResource(Element.CheckedImage)); var uncheckedDrawable = this.Resources.GetDrawable(UIHelper.GetDrawableResource(Element.UnCheckedImage)); if (Element.ImageHeight > 0 && Element.ImageWidth > 0) { checkedDrawable.SetBounds(0, 0, BaseUIHelper.ConvertDPToPixels(Element.ImageWidth), BaseUIHelper.ConvertDPToPixels(Element.ImageHeight)); uncheckedDrawable.SetBounds(0, 0, BaseUIHelper.ConvertDPToPixels(Element.ImageWidth), BaseUIHelper.ConvertDPToPixels(Element.ImageHeight)); } var states = new StateListDrawable(); states.AddState(new int[] { -Android.Resource.Attribute.StateChecked, Android.Resource.Attribute.StateEnabled }, uncheckedDrawable); states.AddState(new int[] { Android.Resource.Attribute.StateChecked, Android.Resource.Attribute.StateEnabled }, checkedDrawable); states.AddState(new int[] { -Android.Resource.Attribute.StateChecked, -Android.Resource.Attribute.StateEnabled }, uncheckedDrawable); states.AddState(new int[] { Android.Resource.Attribute.StateChecked, -Android.Resource.Attribute.StateEnabled }, checkedDrawable); //drawable.AddState(new[] { Android.Resource.Attribute.Checked }, // this.Resources.GetDrawable(UIHelper.GetDrawableResource(Element.CheckedImage))); //drawable.AddState(new[] { ~Android.Resource.Attribute.Checked }, // this.Resources.GetDrawable(UIHelper.GetDrawableResource(Element.UnCheckedImage))); checkBox.SetButtonDrawable(states); } else { Drawable drawable = Resources.GetDrawable(id); drawable.SetColorFilter(Color.White, PorterDuff.Mode.SrcAtop); checkBox.SetButtonDrawable(drawable); } if (e.NewElement.FontSize > 0) { checkBox.TextSize = (float)e.NewElement.FontSize; } if (!string.IsNullOrEmpty(e.NewElement.FontName)) { SetTypeface(e.NewElement.FontName); } }
void UpdateBackgroundStateDrawable() { string normalDrawableName = BaseElement.NormalBackgroundDrawable?.File; string pressedDrawableName = BaseElement.PressedBackgroundDrawable?.File; string disabledDrawableName = BaseElement.DisabledBackgroundDrawable?.File; Android.Graphics.Color normalColor = BaseElement.BackgroundColor.ToAndroid(); Android.Graphics.Color pressedColor = BaseElement.PressedBackgroundColor.ToAndroid(); Android.Graphics.Color disabledColor = BaseElement.DisabledBackgroundColor.ToAndroid(); Drawable disabledDrawable = null; Drawable pressedDrawable = null; Drawable normalDrawable = null; // Disabled state if (!string.IsNullOrEmpty(disabledDrawableName)) { disabledDrawable = Context.GetDrawable(disabledDrawableName); } else if (disabledColor != 0) { disabledDrawable = new ColorDrawable(disabledColor); } // Press state if (!string.IsNullOrEmpty(pressedDrawableName)) { pressedDrawable = Context.GetDrawable(pressedDrawableName); } else if (pressedColor != 0) { pressedDrawable = new ColorDrawable(pressedColor); } // Normal state if (normalDrawableName != null) { normalDrawable = Context.GetDrawable(normalDrawableName); } else { normalDrawable = new ColorDrawable(normalColor); } var stateListDrawable = new StateListDrawable(); if (disabledDrawable != null) { stateListDrawable.AddState( new[] { -Android.Resource.Attribute.StateEnabled }, disabledDrawable); } if (pressedDrawable != null) { stateListDrawable.AddState( new[] { Android.Resource.Attribute.StateEnabled, Android.Resource.Attribute.StatePressed }, pressedDrawable); } stateListDrawable.AddState( new[] { Android.Resource.Attribute.StateEnabled }, normalDrawable); Control.Background = stateListDrawable; }