AndroidX: [Material] Slider.addOnChangeListener binding missing

Version Information

  • Visual Studio version (eg. 16.8 or 8.8): 16.8.3
  • Xamarin.Android version (eg. 11.1): 11.1
  • Using AndroidX or Support Libraries: Xamarin.Google.Android.Material 1.2.1.1

Describe your Issue:

Slider.IOnChangeListener is bound, but there is no way to add it to the Slider class, the addOnChangeListener method hasn’t been bound

Steps to Reproduce (with link to sample solution if possible):

var slider = FindViewById<Google.Android.Material.Slider>(Resource.Id.my_slider);
slider.addOnChangeListener(listener); // compile error
slider.Change += Slider_Changed; // compile error

About this issue

  • Original URL
  • State: open
  • Created 4 years ago
  • Reactions: 6
  • Comments: 17 (4 by maintainers)

Most upvoted comments

This is a workaround until they fix binding issue:

protected override void OnCreate(Bundle savedInstanceState)
{
    base.OnCreate(savedInstanceState, Resource.Layout.my_activity);
    slider = FindViewById<Slider>(Resource.Id.slider);
    slider.Touch += Slider_Touch;
}

private void Slider_Touch(object sender, View.TouchEventArgs e)
{
    switch (e.Event.Action)
    {
        case MotionEventActions.Move:
        case MotionEventActions.Up:
            OnValueChange(slider, slider.Value, true);  //(Slider slider, float value, boolean fromUser)
            break;
    }
    e.Handled = false;
}

//This footprint matches native library callback
private void OnValueChange(Java.Lang.Object sender, float value, bool fromUser)
{
    //Do what you want to do as if you are listening to ValueChanged event (or at least very near to actual event)
}

Sadly, this issue is still present with 1.4.0

workaround:

var method = Class.ForName("com.google.android.material.slider.BaseSlider").GetDeclaredMethods().FirstOrDefault(x => x.Name == "addOnChangeListener");
method?.Invoke(slider, this); // this is implementing IBaseOnChangeListener

Still same with “1.8.0” version. Any progress ? <PackageReference Include="Xamarin.Google.Android.Material" Version="1.8.0" />