81

I would like to have a MapView inside a ScrollView, however when I try to scroll the map, the ScrollView takes priority! Is there a way to give the MapView priority when scrolling inside the map, and the ScrollView otherwise?

Thanks!

Cesar
  • 2,027
  • 2
  • 18
  • 29
  • What's your layout? can you post a screenshot of the final layout that is rendered. – Aman Alam Jul 01 '11 at 09:49
  • 1
    MapView automatically handles the scrolling.... – Sandy Jul 01 '11 at 12:03
  • 2
    I know. I have a ScrollView which contains a lot of different things, including a MapView. The problem is that when I try to scroll inside the mapview, the scrollview will override it. – Cesar Jul 01 '11 at 12:18
  • I was also doing that in my app, but didn't get any solution so i put mapview outside the scroll view and rest of the layout in the scrollview. – Sandy Jul 01 '11 at 18:37
  • You can use this template: . Also provide the layout_weight value to each LL(LinearLayout) According to your size. – Sandy Jul 13 '11 at 07:40
  • 1
    Nothing new to other answers but I've created a Gist on how I solved this. https://gist.github.com/Sottti/890daaeead1bd4784dfce7066a9011aa – Sotti Jun 25 '17 at 10:26
  • Check this [Answer](https://stackoverflow.com/questions/16974983/google-maps-api-v2-supportmapfragment-inside-scrollview-users-cannot-scroll-th/53411743#53411743) and let me know if you have any doubts. – Saamzzz Nov 21 '18 at 12:14

9 Answers9

104

I have had a same problem for 10 days, but I got a solution a few minutes ago!! Here is the solution. I made a custom MapView and override onTouchEvent() like this.

@Override
public boolean onTouchEvent(MotionEvent ev) {
    int action = ev.getAction();
    switch (action) {
    case MotionEvent.ACTION_DOWN:
        // Disallow ScrollView to intercept touch events.
        this.getParent().requestDisallowInterceptTouchEvent(true);
        break;

    case MotionEvent.ACTION_UP:
        // Allow ScrollView to intercept touch events.
        this.getParent().requestDisallowInterceptTouchEvent(false);
        break;
    }

    // Handle MapView's touch events.
    super.onTouchEvent(ev);
    return true;
}
Emily Sooryum
  • 1,146
  • 1
  • 9
  • 3
  • this works for me, but are you experiencing any weird flashing in the layout that the mapview is contained? – aimango Mar 26 '12 at 00:35
  • 2
    @Luizje: the flashing is caused by the background colors you are using for each view. So make sure you have the outermost view using the corect bg color. – aimango Aug 22 '12 at 18:38
  • thnx @aimango, after some testing i found it by my self.. Lucky it was not that hard as it looks to solve. Thnx for your help :) – Luciano Aug 23 '12 at 09:30
  • 39
    if it doesn't work for you, try to override the dispatchTouchEvent method instead (same implementation, just change the super call at the end). thanks. – Rotem Mar 09 '13 at 17:07
57

A better/simpler way to do this without manipulating individual touch events. This will work if you are using MapView:

  @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        /**
         * Request all parents to relinquish the touch events
         */
        getParent().requestDisallowInterceptTouchEvent(true);
        return super.dispatchTouchEvent(ev);
    }

Full class:

public class CustomMapView extends MapView {

    public CustomMapView(Context context) {
        super(context);
    }

    public CustomMapView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CustomMapView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public CustomMapView(Context context, GoogleMapOptions options) {
        super(context, options);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        /**
         * Request all parents to relinquish the touch events
         */
        getParent().requestDisallowInterceptTouchEvent(true);
        return super.dispatchTouchEvent(ev);
    }
}

If you are using a MapFragment then you can put the fragment in a Custom View, and in the dispatchTouchEvent() make the requestDisallowInterceptTouchEvent call.

AmeyaB
  • 989
  • 8
  • 12
  • Nice, although `requestDisallowInterceptTouchEvent` is necessary for `MotionEvent.ACTION_DOWN` only. – arekolek Jul 04 '19 at 09:31
21

Make your own map and use it. It works fully for me.

public class CustomMapView extends MapView {

public CustomMapView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
    switch (ev.getAction()) {
    case MotionEvent.ACTION_UP:
        System.out.println("unlocked");
        this.getParent().requestDisallowInterceptTouchEvent(false);
        break;
    case MotionEvent.ACTION_DOWN:
        System.out.println("locked");
        this.getParent().requestDisallowInterceptTouchEvent(true);
        break;
    }
    return super.dispatchTouchEvent(ev);
}} 

In your layout xml,

<com.yourpackage.xxxx.utils.CustomMapView
                android:id="@+id/customMap"
                android:layout_width="match_parent"
                android:layout_height="400dp"
                android:layout_marginLeft="5dp"
                android:layout_marginRight="5dp"
                />
Zin Win Htet
  • 2,448
  • 4
  • 32
  • 54
16

For those who want the whole working code . here it is

Custom map view class

public class CustomMapView extends MapView {

private ViewParent mViewParent;
public CustomMapView(Context context) {
    super(context);
}

public CustomMapView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public CustomMapView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}

public CustomMapView(Context context, GoogleMapOptions options) {
    super(context, options);
}

public void setViewParent(@Nullable final ViewParent viewParent) { //any ViewGroup
    mViewParent = viewParent;
}

@Override
public boolean onInterceptTouchEvent(final MotionEvent event) {
    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            if (null == mViewParent) {
                getParent().requestDisallowInterceptTouchEvent(true);
            } else {
                mViewParent.requestDisallowInterceptTouchEvent(true);
            }
            break;
        case MotionEvent.ACTION_UP:
            if (null == mViewParent) {
                getParent().requestDisallowInterceptTouchEvent(false);
            } else {
                mViewParent.requestDisallowInterceptTouchEvent(false);
            }
            break;
        default:
            break;
    }

    return super.onInterceptTouchEvent(event);
  }
}

Activity layout xml

  <ScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <location.to.your.CustomMapView
        android:id="@+id/mapView"
        android:layout_width="match_parent"
        android:layout_height="250dp"
         />

</ScrollView>

Instantiating the custom map class in your activity or fragment

       CustomMapView mapView = (CustomMapView) findViewById(R.id.mapView);

That's it enjoy

Ismail Iqbal
  • 2,774
  • 1
  • 25
  • 46
9

You can create a custom MapView like this:

public class CustomMapView extends MapView {

    private MapFragment.ControlLock mCallbackControl;

    public CustomMapView(Context context) {
        this(context, null);
    }

    public CustomMapView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CustomMapView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public CustomMapView(Context context, GoogleMapOptions options) {
        super(context, options);
    }

    public void setCallback(MapFragment.ControlLock callbackControl) {
        this.mCallbackControl = callbackControl;
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {

        switch (event.getAction()) {
            case MotionEvent.ACTION_UP:
                System.out.println("unlocked");
                mCallbackControl.unlock(); /* Interface */
                break;
            case MotionEvent.ACTION_DOWN:
                System.out.println("locked");
                mCallbackControl.lock(); /* Interface */
                break;
        }

        return super.dispatchTouchEvent(event);
    }
}
Pedro Paulo Amorim
  • 1,838
  • 2
  • 27
  • 50
8

I've tried with overriding MapView.onTouchEvent(...), but it didn't work for me. Here is code which works well (overriding MapView.onInterceptTouchEvent(...)):

public class MyMapView extends MapView {
    private ViewParent mViewParent;

//add constructors here

    public void setViewParent(@Nullable final ViewParent viewParent) { //any ViewGroup
            mViewParent = viewParent;
    }

    @Override
        public boolean onInterceptTouchEvent(final MotionEvent event) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    if (null == mViewParent) {
                        getParent().requestDisallowInterceptTouchEvent(true);
                    } else {
                        mViewParent.requestDisallowInterceptTouchEvent(true);
                    }
                    break;
                case MotionEvent.ACTION_UP:
                    if (null == mViewParent) {
                        getParent().requestDisallowInterceptTouchEvent(false);
                    } else {
                        mViewParent.requestDisallowInterceptTouchEvent(false);
                    }
                    break;
                default:
                    break;
            }

            return super.onInterceptTouchEvent(event);
        }
}
ultraon
  • 2,220
  • 2
  • 28
  • 27
  • Overriding `onInterceptTouchEvent()` worked for me too whereas `onTouchEvent()` didn't. But I didn't need the mViewParent though, it seems that `requestDisallowInterceptTouchEvent()` is propagated up the view hierarchy. – Klemens Zleptnig Feb 25 '17 at 23:03
3

If somebody wants this class in kotlin.
I used dispatchTouchEvent like suggested by @rotem

class CustomMapView : MapView {
           constructor(context: Context):
            super(context)
    constructor(context: Context, googleMapOptions: GoogleMapOptions):
            super(context, googleMapOptions)
    constructor(context: Context, attributeSet: AttributeSet):
            super(context, attributeSet)
    constructor(context: Context, attributeSet: AttributeSet, int: Int):
            super(context, attributeSet, int)

    override fun dispatchTouchEvent(event: MotionEvent?): Boolean {
        Log.d("CustomWebView", "touchevent")
        when(event?.action) {
            MotionEvent.ACTION_UP -> {
                Log.d("CustomWebView", "disallow Intercept")
                parent.requestDisallowInterceptTouchEvent(false)
            }
            MotionEvent.ACTION_DOWN -> {
                Log.d("CustomWebView", "allow Intercept")
                parent.requestDisallowInterceptTouchEvent(true)
            }
        }
        return super.dispatchTouchEvent(event)
    }
}
fupduck
  • 490
  • 4
  • 12
2

You can simply put the MapView in a Layout itself and override onTouch or set an Click-Listener - easiest way for me since i needed a touch on the whole MapView in my ScrollView.

Lukas Olsen
  • 5,294
  • 7
  • 22
  • 28
1

If you have mapview in scroll view then you have to explicity mention the follwing paramets to the MapView:

mMapView.setClickable(true);
mMapView.setFocusable(true);
mMapView.setDuplicateParentStateEnabled(false);
skuntsel
  • 11,624
  • 11
  • 44
  • 67
Jaya
  • 999
  • 11
  • 8
  • But then after you specified it and the map view would become unscrollable –  Feb 06 '15 at 14:54