Search code examples
google-mapsxamarinxamarin.androidxamarin.formsandroid-maps-v2

how to do a button click in Xamarin Forms Android custom info window


In my Xamarin Forms Android project I am showing a Map using CustomMapRenderer. I am popping up a Info Window in Map screen and this info window has button name called "Call". I need to do a "OnCLick" button click operation for this button. I have googled it, but unfortunately I didn't come across any solutions. at last I have started to work on converting the Android Native code -Java into Xamarin Forms Android - c#, for this conversion I have been using this answers Google Maps Android API v2 - Interactive InfoWindow (like in original android google maps) . This converted code shows A field initializer cannot reference the non static field, method or property OnInfoWindowElemTouchListener.onClickConfirmed(view, marker) this Error inside Java.Lang.Runnable please help me to resolve the problem.

OnInfoWindowElemTouchListener.cs

 using System.Threading.Tasks;
    using Android.App;
    using Android.Content;
    using Android.Gms.Maps.Model;
    using Android.Graphics.Drawables;
    using Android.OS;
    using Android.Views;
    using Android.Views.Accessibility;

    using Java.Lang;

    namespace Hotel.Droid
    {
        public abstract class OnInfoWindowElemTouchListener : Java.Lang.Object
        , View.IOnTouchListener
        {
            private View view;

            private Drawable bgDrawableNormal;
            private Drawable bgDrawablePressed;
            private Handler handler = new Handler();
            private Marker marker;
            private static bool endPressStatus = false;
            private bool pressed = false;


            public OnInfoWindowElemTouchListener(View view, Drawable bgDrawableNormal, Drawable bgDrawablePressed)
            {
                this.view = this.view;
                this.bgDrawableNormal = this.bgDrawableNormal;
                this.bgDrawablePressed = this.bgDrawablePressed;
            }
            public OnInfoWindowElemTouchListener()
            {

            }

            public void setMarker(Marker marker)
            {
                this.marker = this.marker;
            }



            public bool OnTouch(View vv, MotionEvent e)
            {
                if (0 <= e.GetX() && e.GetX() <= vv.Width && 0 <= e.GetY() && e.GetY() <= vv.Height)
                {
                    switch (e.ActionMasked)
                    {
                        case MotionEventActions.Down:
                            startPress();
                            break;

                        // We need to delay releasing of the view a little so it shows the
                        // pressed state on the screen
                        case MotionEventActions.Up:
                            //handler.PostDelayed(ConfirmClickRunnable, 150);
Task.Factory.StartNew(() => onClickConfirmed(view, marker));
Task.Delay(150);
                            break;

                        case MotionEventActions.Cancel:
                            endPress();
                            break;
                        default:
                            break;
                    }
                }
                else {
                    // If the touch goes outside of the view's area
                    // (like when moving finger out of the pressed button)
                    // just release the press
                    endPress();
                }

                return false;
            }


            private void startPress()
            {
                if (!pressed)
                {
                    pressed = true;
                    //handler.RemoveCallbacks(ConfirmClickRunnable);
                    view.SetBackgroundDrawable(bgDrawablePressed);
                    if ((marker != null))
                    {
                        marker.ShowInfoWindow();
                    }

                }

            }

            public bool endPress()
            {
                if (pressed)
                {
                    this.pressed = false;
                    handler.RemoveCallbacks(ConfirmClickRunnable);
                    view.SetBackgroundDrawable(bgDrawableNormal);


                    if ((marker != null))
                    {
                        marker.ShowInfoWindow();
                    }
                    endPressStatus = true;
                    return true;
                }
                else {
                    endPressStatus = false;
                    return false;
                }

            }

            private  Runnable ConfirmClickRunnable = new Java.Lang.Runnable(() =>
            {
               if (endPressStatus)
               {

                    onClickConfirmed(view, marker);
               }
            });




            /*private  class RunnableAnonymousInnerClassHelper : Java.Lang.Object, Java.Lang.IRunnable
            {
                private readonly Context outerInstance;     

                public RunnableAnonymousInnerClassHelper(Context outerInstance)
                {
                    this.outerInstance = outerInstance;         
                }

                public void Run()
                {
                    if (endPressStatus)
                    {
                        onClickConfirmed();
                    }
                }
            }*/


            protected abstract void onClickConfirmed(View v, Marker marker);


        }

    }

enter image description here

Updated

I have implemented the Task.Factory.StartNew instead of Android Runnableand now I am stucking on the below lines. I am struggling on converting this below Java codes into C#since it is written by Anonymous class concept.

Java

this.infoButtonListener = new OnInfoWindowElemTouchListener(infoButton,
                getResources().getDrawable(R.drawable.btn_default_normal_holo_light),
                getResources().getDrawable(R.drawable.btn_default_pressed_holo_light)) 
        {
            @Override
            protected void onClickConfirmed(View v, Marker marker) {
                // Here we can perform some action triggered after clicking the button
                Toast.makeText(MainActivity.this, marker.getTitle() + "'s button clicked!", Toast.LENGTH_SHORT).show();
            }
        }; 
        this.infoButton.setOnTouchListener(infoButtonListener);

infoButton in code is Call button

C# - Please help me to resolve the problem by converting/using(How to use) the above java code


Solution

  • The solution is too complicated for this window. Please see chat room for step by step solution. https://chat.stackoverflow.com/rooms/128847/discussion-between-jamal-and-yuri-s