1

I'm using a custompicker in my app and I need to change the cancel and ok buttons' color. I recently changed the whole theme of timepicker and datepicker for android by adding this code to the style file:

 <item name="android:datePickerDialogTheme">@style/AppCompatDialogStyle</item>
    <item name="android:timePickerDialogTheme">@style/TimePickerTheme</item>

item name="colorAccent">#039BE5</item>
    <item name="android:layout_width">wrap_content</item>
    <item name="android:layout_height">wrap_content</item>
  </style>
  <style name="TimePickerTheme" parent="Theme.AppCompat.Light.Dialog">
    <item name="colorAccent">#039BE5</item>
    <item name="android:layout_width">wrap_content</item>
    <item name="android:layout_height">wrap_content</item>
  </style>

Is that a similar code that I can add for the picker?

mohammad anouti
  • 171
  • 2
  • 18
  • Clarification: This question and the accepted answer are specific to (Xamarin) Android; it isn't a *cross-platform* solution for setting picker buttons color. – ToolmakerSteve Nov 29 '19 at 15:47

1 Answers1

2

if you change the theme <item name="colorAccent">#039BE5</item> may affect the theme of other controls,you could only change the color in your CustomRenderer like this:

public class CustomPickerRenderer : PickerRenderer
{
    private Context context;
    private IElementController ElementController => Element as IElementController;
    private AlertDialog _dialog;

    public CustomPickerRenderer(Context context) : base(context)
    {
        this.context = context;
    }

    protected override void OnElementChanged(ElementChangedEventArgs<Picker> e)
    {
        base.OnElementChanged(e);
        if (Control == null || e.NewElement == null) return;
        Control.Click += Control_Click1;
    }
    protected override void Dispose(bool disposing)
    {
        Control.Click -= Control_Click1;
        base.Dispose(disposing);
    }

    private void Control_Click1(object sender, EventArgs e)
    {
        Picker model = Element;

        var picker = new NumberPicker(Context);
        if (model.Items != null && model.Items.Any())
        {
            picker.MaxValue = model.Items.Count - 1;
            picker.MinValue = 0;
            picker.SetDisplayedValues(model.Items.ToArray());
            picker.WrapSelectorWheel = false;
            picker.DescendantFocusability = DescendantFocusability.BlockDescendants;
            picker.Value = model.SelectedIndex;
        }

        var layout = new LinearLayout(Context) { Orientation = Orientation.Vertical };
        layout.AddView(picker);

        ElementController.SetValueFromRenderer(VisualElement.IsFocusedProperty, true);

        var builder = new AlertDialog.Builder(Context);
        builder.SetView(layout);
        builder.SetTitle(model.Title ?? "");

        //change the text or color here
        builder.SetNegativeButton(Html.FromHtml("<font color='#039BE5'>Cancel</font>"), (s, a) =>
        {
            ElementController.SetValueFromRenderer(VisualElement.IsFocusedProperty, false);
            // It is possible for the Content of the Page to be changed when Focus is changed.
            // In this case, we'll lose our Control.
            Control?.ClearFocus();
            _dialog = null;
        });

         //change the text or color here
        builder.SetPositiveButton(Html.FromHtml("<font color='#039BE5'>OK</font>"), (s, a) =>
        {
            ElementController.SetValueFromRenderer(Picker.SelectedIndexProperty, picker.Value);
            // It is possible for the Content of the Page to be changed on SelectedIndexChanged.
            // In this case, the Element & Control will no longer exist.
            if (Element != null)
            {
                if (model.Items.Count > 0 && Element.SelectedIndex >= 0)
                    Control.Text = model.Items[Element.SelectedIndex];
                ElementController.SetValueFromRenderer(VisualElement.IsFocusedProperty, false);
                // It is also possible for the Content of the Page to be changed when Focus is changed.
                // In this case, we'll lose our Control.
                Control?.ClearFocus();
            }
            _dialog = null;
        });

        _dialog = builder.Create();
        _dialog.DismissEvent += (ssender, args) =>
        {
            ElementController?.SetValueFromRenderer(VisualElement.IsFocusedProperty, false);
        };
        _dialog.Show();
    }
}

update i test it on Tablet emulator,it works well

enter image description here

Leo Zhu
  • 15,726
  • 1
  • 7
  • 23
  • This way is working fine on android v9.0 but I tried it on my 2 tablets it didn't change :( – mohammad anouti Jun 03 '19 at 14:17
  • i will test it later and tell you – Leo Zhu Jun 03 '19 at 14:18
  • @mohammadanouti i test it ,and it works,you could see the gif above.what's your tablet ? – Leo Zhu Jun 04 '19 at 01:03
  • It gives me this error when adding the code on new project: Member 'DescendantFocusability.BlockDescendants' cannot be accessed with an instance reference; qualify it with a type name instead. on this line: picker.DescendantFocusability = DescendantFocusability.BlockDescendants; – mohammad anouti Jun 06 '19 at 07:22
  • I tried it on Huawei PLE-701L and SAMSUNG SM-T365 both are "Android 5.1" and nothing is changing – mohammad anouti Jun 06 '19 at 07:49
  • I replaced this: picker.DescendantFocusability = DescendantFocusability.BlockDescendants; by this: picker.DescendantFocusability = DescendantFocusability; and it worked partially on my Xiaomi POCOPHONE F1 "Android 9", I tap once and the color stay the same then unfocus and tap again and the color change, strange behavior. – mohammad anouti Jun 06 '19 at 08:03
  • I will try to find the appropriate equipment to test, maybe in a few days – Leo Zhu Jun 06 '19 at 08:14
  • How do the same for iOS? – ToolmakerSteve Nov 29 '19 at 15:50
  • [Beginning of an iOS renderer](https://stackoverflow.com/a/39905053/199364). I don't think that example changes the button color, but it shouldn't be hard to add. – ToolmakerSteve Nov 29 '19 at 16:00