private void ChangeColor(Color newColor) { tabs.SetBackgroundColor(newColor); // change ActionBar color just if an ActionBar is available Drawable colorDrawable = new ColorDrawable(newColor); Drawable bottomDrawable = new ColorDrawable(Resources.GetColor(Android.Resource.Color.Transparent)); LayerDrawable ld = new LayerDrawable(new Drawable[]{colorDrawable, bottomDrawable}); if (oldBackground == null) { SupportActionBar.SetBackgroundDrawable(ld); } else { TransitionDrawable td = new TransitionDrawable(new Drawable[]{oldBackground, ld}); SupportActionBar.SetBackgroundDrawable(td); td.StartTransition(200); } oldBackground = ld; currentColor = newColor; }
public static void UpdateBackground(Border border, Android.Views.View view) { var strokeThickness = border.StrokeThickness; var context = view.Context; // create stroke drawable GradientDrawable strokeDrawable = null; // if thickness exists, set stroke drawable stroke and radius if(strokeThickness.HorizontalThickness + strokeThickness.VerticalThickness > 0) { strokeDrawable = new GradientDrawable (); strokeDrawable.SetColor (border.BackgroundColor.ToAndroid ()); // choose thickest margin // the content is padded so it will look like the margin is with the given thickness strokeDrawable.SetStroke ((int)context.ToPixels (strokeThickness.ThickestSide()), border.Stroke.ToAndroid ()); strokeDrawable.SetCornerRadius ((float)border.CornerRadius); } // create background drawable var backgroundDrawable = new GradientDrawable (); // set background drawable color based on Border's background color backgroundDrawable.SetColor (border.BackgroundColor.ToAndroid ()); backgroundDrawable.SetCornerRadius((float)border.CornerRadius); if (strokeDrawable != null) { // if stroke drawable exists, create a layer drawable containing both stroke and background drawables var ld = new LayerDrawable (new Drawable[] { strokeDrawable, backgroundDrawable }); ld.SetLayerInset (1, (int)context.ToPixels (strokeThickness.Left), (int)context.ToPixels (strokeThickness.Top), (int)context.ToPixels (strokeThickness.Right), (int)context.ToPixels (strokeThickness.Bottom)); view.SetBackgroundDrawable (ld); } else { view.SetBackgroundDrawable (backgroundDrawable); } // set Android.View's padding to take into account the stroke thickiness view.SetPadding ( (int)context.ToPixels (strokeThickness.Left + border.Padding.Left), (int)context.ToPixels (strokeThickness.Top + border.Padding.Top), (int)context.ToPixels (strokeThickness.Right + border.Padding.Right), (int)context.ToPixels (strokeThickness.Bottom + border.Padding.Bottom)); }
private void UpdateColors() { var button = Element as ShadowButton; var normal = new Android.Graphics.Drawables.GradientDrawable (); normal.SetColor (button.BackgroundColor.ToAndroid ()); normal.SetStroke ((int)button.BorderWidth, button.BorderColor.ToAndroid ()); normal.SetCornerRadius (button.BorderRadius); var shadow = new Android.Graphics.Drawables.GradientDrawable (); shadow.SetColor (button.ShadowColor.ToAndroid ()); shadow.SetStroke ((int)button.BorderWidth, button.ShadowColor.ToAndroid ()); shadow.SetCornerRadius (button.BorderRadius); _normal = new Android.Graphics.Drawables.LayerDrawable ( new Drawable [] { shadow, normal } ); var density = 3; _normal.SetLayerInset (1, 0, 0, 0, 2 * density); _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); var sld = new StateListDrawable (); sld.AddState (new int[] { Android.Resource.Attribute.StatePressed }, _pressed); sld.AddState (new int[] {}, _normal ); Control.SetPaddingRelative (0, 0, 2, 0); Control.SetBackgroundDrawable (sld); }
public static void SetTheme(EditText editText, FlatTheme theme, FlatUI.FlatFontFamily fontFamily, FlatUI.FlatFontWeight fontWeight, FlatUI.FlatTextAppearance textAppearance, int style, int radius, int padding, int border) { float[] outerR = new float[]{radius, radius, radius, radius, radius, radius, radius, radius}; // creating normal state drawable var normalFront = new ShapeDrawable(new RoundRectShape(outerR, null, null)); normalFront.SetPadding(padding, padding, padding, padding); var normalBack = new ShapeDrawable(new RoundRectShape(outerR, null, null)); normalBack.SetPadding(border, border, border, border); if (style == 0) { // flat normalFront.Paint.Color = Color.Transparent; normalBack.Paint.Color = theme.LightAccentColor; editText.SetTextColor(theme.VeryLightAccentColor); } else if (style == 1) { // box normalFront.Paint.Color = Color.White; normalBack.Paint.Color = theme.LightAccentColor; editText.SetTextColor(theme.BackgroundColor); } else if (style == 2) { // transparent normalFront.Paint.Color = Color.Transparent; normalBack.Paint.Color = Color.Transparent; editText.SetTextColor(theme.BackgroundColor); } Drawable[] d = {normalBack, normalFront}; LayerDrawable normal = new LayerDrawable(d); editText.SetBackgroundDrawable(normal); editText.SetHintTextColor(theme.VeryLightAccentColor); if (textAppearance == FlatUI.FlatTextAppearance.Dark) editText.SetTextColor(theme.DarkAccentColor); else if (textAppearance == FlatUI.FlatTextAppearance.Light) editText.SetTextColor(theme.VeryLightAccentColor); var typeface = FlatUI.GetFont(editText.Context, fontFamily, fontWeight); if (typeface != null) editText.SetTypeface(typeface, TypefaceStyle.Normal); }
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); }
private void ChangeColor(Color newColor) { _tabs.IndicatorColor = newColor; _tabs.TextColor = newColor; // change ActionBar color just if an ActionBar is available if (Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.Honeycomb) { Drawable colorDrawable = new ColorDrawable(newColor); Drawable bottomDrawable = Resources.GetDrawable(Resource.Drawable.actionbar_bottom); LayerDrawable ld = new LayerDrawable(new Drawable[] { colorDrawable, bottomDrawable }); if (_oldBackground == null) { if ((int)Build.VERSION.SdkInt < 17) { ld.Callback = _drawableCallback; } else { ActionBar.SetBackgroundDrawable(ld); } } else { TransitionDrawable td = new TransitionDrawable(new Drawable[] { _oldBackground, ld }); // workaround for broken ActionBarContainer drawable handling on // pre-API 17 builds // https://github.com/android/platform_frameworks_base/commit/a7cc06d82e45918c37429a59b14545c6a57db4e4 if ((int)Build.VERSION.SdkInt < 17) { td.Callback = _drawableCallback; } else { ActionBar.SetBackgroundDrawable(td); } td.StartTransition(200); } _oldBackground = ld; // http://stackoverflow.com/questions/11002691/actionbar-setbackgrounddrawable-nulling-background-from-thread-handler ActionBar.SetDisplayShowTitleEnabled(false); ActionBar.SetDisplayShowTitleEnabled(true); } _currentColor = newColor; }
private Drawable CreateDrawable (int color) { var ovalShape = new OvalShape (); var shapeDrawable = new ShapeDrawable (ovalShape); shapeDrawable.Paint.Color = new Color (color); if (hasShadow && !HasLollipopApi) { var shadowDrawable = Resources.GetDrawable (size == FabSize.Normal ? Resource.Drawable.fab_shadow : Resource.Drawable.fab_shadow_mini); var layerDrawable = new LayerDrawable (new Drawable[]{ shadowDrawable, shapeDrawable }); layerDrawable.SetLayerInset (1, shadowSize, shadowSize, shadowSize, shadowSize); return layerDrawable; } else { return shapeDrawable; } }
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); }
public static void SetActionBarTheme(Activity context, FlatTheme theme, bool dark) { var color1 = theme.LightAccentColor; var color2 = theme.BackgroundColor; if (dark) { color1 = theme.BackgroundColor; color2 = theme.DarkAccentColor; } var front = new PaintDrawable(color1); var bottom = new PaintDrawable(color2); var d = new Drawable[] { bottom, front}; var drawable = new LayerDrawable(d); drawable.SetLayerInset(1, 0, 0, 0, 3); var actionBar = context.ActionBar; actionBar.SetBackgroundDrawable(drawable); // invalidating action bar actionBar.Title = actionBar.Title; }
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); }