0

I am having trouble trying to figure out how to change the drawable of a marker for when the user taps on it and then back to the original drawable when the user leaves his finger out of the screen.

I already know how to change the marker's drawable by using this:

marker.setIcon(BitmapDescriptorFactory.fromResource(R.drawable.my_drawable));

The problem is, where do I put that code exactly? If I put it inside the onMarkerClick callback then it's too late because that callback is fired after the user leaves his finger out of the screen (just like a normal click event in any view is fired only when the finger is let off the screen).

I've tried to set the drawable to a selector (as bellow) but it does not work (the code throws an exception):

final BitmapDescriptor bitmapDescriptor = BitmapDescriptorFactory.fromResource(R.drawable.my_selector);

final MarkerOptions markerOptions = new MarkerOptions().icon(bitmapDescriptor)
                                                       .anchor(0.5f, 1.0f)
                                                       .position(latLon);

Here is my_selector.xml (used above):

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/drawable_A" android:state_focused="true" android:state_pressed="false"/>
<item android:drawable="@drawable/drawable_B" android:state_focused="true" android:state_pressed="true"/>
<item android:drawable="@drawable/drawable_B" android:state_focused="false" android:state_pressed="true"/>
<item android:drawable="@android:color/white" />

1 Answers1

0

I suggest you to try OnTouchListener with maps.

On MotionEvent.ACTION_DOWN you can set one background and on MotionEvent.ACTION_UP you can set another background.

Please refer the link here and here.

Community
  • 1
  • 1
anu_r
  • 1,602
  • 7
  • 30
  • 61