Skip to content
This repository was archived by the owner on May 1, 2024. It is now read-only.

Add Radiobutton.RadioColor #10411

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using System;
namespace Xamarin.Forms.Controls.GalleryPages.RadioButtonGalleries
{
public class ColoredRadioGallery : ContentPage
{
readonly Random _rand = new Random();
readonly RadioButton _radioButton = new RadioButton
{
Text = "I can change color",
RadioColor = Color.Red
};

readonly RadioButton _radioButtonDisabled = new RadioButton
{
Text = "I'm disabled",
IsEnabled = false
};

public ColoredRadioGallery()
{
var stackLayout = new StackLayout
{
VerticalOptions = LayoutOptions.Center,
HorizontalOptions = LayoutOptions.Center
};

var button = new Button
{
Text = "Change color"
};

button.Clicked += Button_Clicked;

stackLayout.Children.Add(button);
stackLayout.Children.Add(_radioButton);
stackLayout.Children.Add(_radioButtonDisabled);

Content = stackLayout;
}

void Button_Clicked(object sender, EventArgs e)
{
var c = Color.FromRgb(_rand.Next(256), _rand.Next(256), _rand.Next(256));
_radioButton.RadioColor = c;
_radioButtonDisabled.RadioColor = c;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ public RadioButtonGalleries()
descriptionLabel,
button,
GalleryBuilder.NavButton("RadioButton Group Gallery", () =>
new RadioButtonGroupGalleryPage(), Navigation)
new RadioButtonGroupGalleryPage(), Navigation),
GalleryBuilder.NavButton("RadioButton Colored Radio Gallery", () =>
new ColoredRadioGallery(), Navigation)
}
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@
Margin="0, 0, 0, 10"/>
<Label Text="StackLayout" />
<StackLayout>
<RadioButton Text="RadioButton, Group=null" />
<RadioButton Text="RadioButton, Group=null"/>
<RadioButton Text="RadioButton, Group=null"/>
<RadioButton Text="RadioButton, Group=null, RadioColor=Red" RadioColor="Red" />
<RadioButton Text="RadioButton, Group=null, RadioColor=Blue" RadioColor="Blue" />
<RadioButton Text="RadioButton, Group=null, RadioColor=Yellow" RadioColor="Yellow" />
</StackLayout>
<Label Text="StackLayout" Margin="0, 10"/>
<StackLayout>
Expand Down
3 changes: 3 additions & 0 deletions Xamarin.Forms.Controls/Xamarin.Forms.Controls.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@
<Compile Update="TabIndexTest\DaysOfWeekView.xaml.cs">
<DependentUpon>DaysOfWeekView.xaml</DependentUpon>
</Compile>
<Compile Update="GalleryPages\RadioButtonGalleries\ColoredRadioGallery.cs">
<SubType></SubType>
</Compile>
</ItemGroup>
<ItemGroup>
<EmbeddedResource Update="TabIndexTest\DayView.xaml">
Expand Down
9 changes: 9 additions & 0 deletions Xamarin.Forms.Core/RadioButton.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ public class RadioButton : Button, IElementConfiguration<RadioButton>
public static readonly BindableProperty GroupNameProperty = BindableProperty.Create(
nameof(GroupName), typeof(string), typeof(RadioButton), null, propertyChanged: (b, o, n) => ((RadioButton)b).OnGroupNamePropertyChanged((string)o, (string)n));

public static readonly BindableProperty RadioColorProperty = BindableProperty.Create(
nameof(RadioColor), typeof(Color), typeof(RadioButton), Color.Default);

// TODO Needs implementations beyond Android
//public static readonly BindableProperty ButtonSourceProperty = BindableProperty.Create(
// nameof(ButtonSource), typeof(ImageSource), typeof(RadioButton), null);
Expand All @@ -39,6 +42,12 @@ public string GroupName
set { SetValue(GroupNameProperty, value); }
}

public Color RadioColor
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we just call it Color?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’m fine with anything really. I just figured it would make it more clear with RadioColor to what it refers.
And it’s a bit more consistent with other properties like BackgroundColor or TextColor etc.

"Radio" might not be the best, but I guess it's called RadioButton for a reason. So that thing is called the Radio, right? :P

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I expected Color as well...

{
get { return (Color)GetValue(RadioColorProperty); }
set { SetValue(RadioColorProperty, value); }
}

// TODO Needs implementations beyond Android
//public ImageSource ButtonSource
//{
Expand Down
47 changes: 47 additions & 0 deletions Xamarin.Forms.Platform.Android/AppCompat/RadioButtonRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
#if __ANDROID_29__
using AndroidX.Core.View;
using AndroidX.AppCompat.Widget;
using AndroidX.Core.Widget;
#else
using Android.Support.V7.Widget;
using Android.Support.V4.View;
using Android.Support.V4.Widget;
#endif
using Android.Util;
using Android.Views;
Expand All @@ -18,6 +20,8 @@
using AView = Android.Views.View;
using Android.Graphics.Drawables;
using Android.Widget;
using Android.Content.Res;
using AAttribute = Android.Resource.Attribute;

namespace Xamarin.Forms.Platform.Android
{
Expand All @@ -40,6 +44,16 @@ public class RadioButtonRenderer : AppCompatRadioButton,
IPlatformElementConfiguration<PlatformConfiguration.Android, Button> _platformElementConfiguration;
Button _button;

static readonly int[][] CheckedStates = new int[][]
{
new int[] { AAttribute.StateEnabled, AAttribute.StateChecked },
new int[] { AAttribute.StateEnabled, -AAttribute.StateChecked },
new int[] { -AAttribute.StateEnabled, AAttribute.StateChecked },
new int[] { -AAttribute.StateEnabled, -AAttribute.StatePressed },
};

static readonly AColor DefaultRadioColor = new AColor(Resource.Drawable.abc_btn_radio_material);

public event EventHandler<VisualElementChangedEventArgs> ElementChanged;
public event EventHandler<PropertyChangedEventArgs> ElementPropertyChanged;

Expand Down Expand Up @@ -213,6 +227,7 @@ protected virtual void OnElementChanged(ElementChangedEventArgs<Button> e)
_buttonLayoutManager?.Update();
//UpdateButtonImage(true);
UpdateIsChecked();
UpdateRadioColor();
ElevationHelper.SetElevation(this, e.NewElement);
}

Expand All @@ -237,6 +252,10 @@ protected virtual void OnElementPropertyChanged(object sender, PropertyChangedEv
{
UpdateIsChecked();
}
else if (e.Is(RadioButton.RadioColorProperty))
{
UpdateRadioColor();
}
//else if (e.PropertyName == RadioButton.ButtonSourceProperty.PropertyName)
//{
// UpdateButtonImage(false);
Expand Down Expand Up @@ -370,6 +389,34 @@ void UpdateIsChecked()
Checked = ((RadioButton)Element).IsChecked;
}

ColorStateList GetColorStateList()
{
var tintColor = ((RadioButton)Element).RadioColor == Color.Default ? Color.Accent.ToAndroid() : ((RadioButton)Element).RadioColor.ToAndroid();

var list = new ColorStateList(
CheckedStates,
new int[]
{
tintColor,
DefaultRadioColor,
tintColor,
DefaultRadioColor
});

return list;
}

void UpdateRadioColor()
{
if (Element == null || Control == null)
return;

var mode = PorterDuff.Mode.SrcIn;

CompoundButtonCompat.SetButtonTintList(Control, GetColorStateList());
CompoundButtonCompat.SetButtonTintMode(Control, mode);
}

void IOnCheckedChangeListener.OnCheckedChanged(CompoundButton buttonView, bool isChecked)
{
((IElementController)Element).SetValueFromRenderer(RadioButton.IsCheckedProperty, isChecked);
Expand Down
21 changes: 18 additions & 3 deletions Xamarin.Forms.Platform.UAP/FormsRadioButton.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@ namespace Xamarin.Forms.Platform.UWP
{
public class FormsRadioButton : Windows.UI.Xaml.Controls.RadioButton
{
public static readonly DependencyProperty BorderRadiusProperty = DependencyProperty.Register(nameof(BorderRadius), typeof(int), typeof(FormsButton),
new PropertyMetadata(default(int), OnBorderRadiusChanged));
public static readonly DependencyProperty BorderRadiusProperty = DependencyProperty.Register(nameof(BorderRadius), typeof(int), typeof(FormsRadioButton),
new PropertyMetadata(default(int), OnBorderRadiusChanged));

public static readonly DependencyProperty BackgroundColorProperty = DependencyProperty.Register(nameof(BackgroundColor), typeof(Brush), typeof(FormsButton),
public static readonly DependencyProperty BackgroundColorProperty = DependencyProperty.Register(nameof(BackgroundColor), typeof(Brush), typeof(FormsRadioButton),
new PropertyMetadata(default(Brush), OnBackgroundColorChanged));

public static readonly DependencyProperty RadioColorProperty = DependencyProperty.Register(nameof(RadioColor), typeof(Brush), typeof(FormsRadioButton),
new PropertyMetadata(default(Brush)));

WContentPresenter _contentPresenter;

public Brush BackgroundColor
Expand All @@ -26,6 +29,18 @@ public Brush BackgroundColor
}
}

public Brush RadioColor
{
get
{
return (Brush)GetValue(RadioColorProperty);
}
set
{
SetValue(RadioColorProperty, value);
}
}

public int BorderRadius
{
get
Expand Down
Loading