android-libraries: [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 6 years ago
  • Reactions: 7
  • Comments: 23 (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)
}
var method = Class.ForName("com.google.android.material.slider.BaseSlider").GetDeclaredMethods().FirstOrDefault(x => x.Name == "addOnChangeListener");
method?.Invoke(slider, this); // this is implementing IBaseOnChangeListener

With the latest Nuget packages, this warning now also shows the deprecation warning: While this type is 'public', Google considers it internal API and reserves the right to modify or delete it in the future. Use at your own risk

When trying to use the non-deprecated Slider.IOnChangeListener class (which inherits from IBaseOnChangeListener) it leads to other errors:

1>obj\Debug\net8.0-android\android\src\crc64cd18cf298694e84d\Dialog.java(4,8): javac.exe error JAVAC0000:  error: BaseOnChangeListener cannot be inherited with different arguments: <com.google.android.material.slider.Slider> and <>
1>obj\Debug\net8.0-android\android\src\crc64cd18cf298694e84d\Dialog.java(4,8): javac.exe error JAVAC0000: public class Dialog
1>obj\Debug\net8.0-android\android\src\crc64cd18cf298694e84d\Dialog.java(4,8): javac.exe error JAVAC0000:
1>obj\Debug\net8.0-android\android\src\crc64cd18cf298694e84d\Dialog.java(59,14): javac.exe error JAVAC0000:  error: name clash: class Dialog has two methods with the same erasure, yet neither overrides the other
1>obj\Debug\net8.0-android\android\src\crc64cd18cf298694e84d\Dialog.java(59,14): javac.exe error JAVAC0000: 	public void onValueChange (com.google.android.material.slider.Slider p0, float p1, boolean p2)
1>obj\Debug\net8.0-android\android\src\crc64cd18cf298694e84d\Dialog.java(59,14): javac.exe error JAVAC0000:   first method:  onValueChange(Slider,float,boolean) in BaseOnChangeListener
1>obj\Debug\net8.0-android\android\src\crc64cd18cf298694e84d\Dialog.java(59,14): javac.exe error JAVAC0000:   second method: onValueChange(Object,float,boolean) in Dialog
1>obj\Debug\net8.0-android\android\src\crc64cd18cf298694e84d\Dialog.java(59,14): javac.exe error JAVAC0000:
1>obj\Debug\net8.0-android\android\src\crc64cd18cf298694e84d\Dialog.java(67,14): javac.exe error JAVAC0000:  error: name clash: onValueChange(Object,float,boolean) in Dialog and onValueChange(Slider,float,boolean) in BaseOnChangeListener have the same erasure, yet neither overrides the other
1>obj\Debug\net8.0-android\android\src\crc64cd18cf298694e84d\Dialog.java(67,14): javac.exe error JAVAC0000: 	public void onValueChange (java.lang.Object p0, float p1, boolean p2)
1>obj\Debug\net8.0-android\android\src\crc64cd18cf298694e84d\Dialog.java(67,14): javac.exe error JAVAC0000:

It really would be appreciated if some working workaround could be provided by the bindings, without having to implement it by ourselves that causes build warnings

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

Sadly, this issue is still present with 1.4.0

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