how to set google map fragment inside scroll view

map fragment inside nestedscrollview
map view within scrollview in android
supportmapfragment inside scrollview android
google map inside fragment android example
mapview inside listview android
how to load google map faster in android
android google map touch listener
google map in recyclerview android

I want to set a Google map fragment inside vertical ScrollView, when I do the map does not zoom vertically, how can I override the touch event listener on MapView over the listener of ScrollView?

This is my xml code:

  <ScrollView  android:layout_height="wrap_content"

   ///other things


  //other things

Create a custom SupportMapFragmet so that we can override its touch event:

import android.content.Context;
import android.os.Bundle;
import android.widget.FrameLayout;

import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;


public class WorkaroundMapFragment extends SupportMapFragment {
    private OnTouchListener mListener;

    public View onCreateView(LayoutInflater layoutInflater, ViewGroup viewGroup, Bundle savedInstance) {
        View layout = super.onCreateView(layoutInflater, viewGroup, savedInstance);

        TouchableWrapper frameLayout = new TouchableWrapper(getActivity());


        ((ViewGroup) layout).addView(frameLayout,
                new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));

        return layout;

    public void setListener(OnTouchListener listener) {
        mListener = listener;

    public interface OnTouchListener {
        public abstract void onTouch();

    public class TouchableWrapper extends FrameLayout {

        public TouchableWrapper(Context context) {

        public boolean dispatchTouchEvent(MotionEvent event) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                case MotionEvent.ACTION_UP:
            return super.dispatchTouchEvent(event);

In this above class, we intercept the touch event by using TouchableWrapper class that extends the FrameLayout. There is also a custom listener OnTouchListener to dispatch the touch event to the main activity MyMapActivity that handles the map. When touch event occurred, dispatchTouchEvent will be called and the listener mListener will handle it.

Then replace fragment class in xml with this class="packagename.WorkaroundMapFragment" instead of

Then in your activity initialise map as follows:

private GoogleMap mMap;

inside onCreate do this:

  // check if we have got the googleMap already
  if (mMap == null) {
        SupportMapFragment mapFragment = (WorkaroundMapFragment) getChildFragmentManager().findFragmentById(;
        mapFragment.getMapAsync(new OnMapReadyCallback() {
                public void onMapReady(GoogleMap googleMap)
                        mMap = googleMap;

                        mScrollView = findViewById(; //parent scrollview in xml, give your scrollview id value
                        ((WorkaroundMapFragment) getChildFragmentManager().findFragmentById(
                                .setListener(new WorkaroundMapFragment.OnTouchListener() {
                            public void onTouch()

Update : Answer updated to getMapAsync based on answer by @javacoder123

how to set google map fragment inside scroll view, I want to set a Google map fragment inside vertical ScrollView when I do the map does not zoom vertically how can I override the touch e This post is about Android google map in fragment example. We will implement a google map inside a fragment using supportmapfragment class. Integration of google map inside fragment is little different than the integration in android activity. Udemy Course. You can enhance your android skill with below course.

Just make CustomScrollView.class,

public class CustomScrollView extends ScrollView {

public CustomScrollView(Context context) {

public CustomScrollView(Context context, AttributeSet attrs) {
    super(context, attrs);

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

public boolean onInterceptTouchEvent(MotionEvent ev) {
    final int action = ev.getAction();
    switch (action) {
        case MotionEvent.ACTION_DOWN:
            //Log.i("CustomScrollView", "onInterceptTouchEvent: DOWN super false" );

        case MotionEvent.ACTION_MOVE:
            return false; // redirect MotionEvents to ourself

        case MotionEvent.ACTION_CANCEL:
            // Log.i("CustomScrollView", "onInterceptTouchEvent: CANCEL super false" );

        case MotionEvent.ACTION_UP:
            //Log.i("CustomScrollView", "onInterceptTouchEvent: UP super false" );
            return false;

            //Log.i("CustomScrollView", "onInterceptTouchEvent: " + action );

    return false;

public boolean onTouchEvent(MotionEvent ev) {
    //Log.i("CustomScrollView", "onTouchEvent. action: " + ev.getAction() );
    return true;

Then in xml


if xml beffore didn't work change to this...


how to use programmaticaly

CustomScrollView myScrollView = (CustomScrollView) findViewById(;

How to Make Android Map V2 Scrollable Inside a ScrollView Layout, Placing a map inside a ScrollView can make the map becoming difficult to scroll. In this case, i work with the SupportMapFragment in Android Map V2 GoogleMap mMap and ScrollView container mScollView then set the� @Rotem your solution worked for me. I had a in a ScrollView and it didn't work with the above code. I extened this class and wrote your solution with overriding the dispatchTouchEvent method. – Tooroop Sep 13 '13 at 8:00

My suggestion is to use lighter version of google map when dealing with this situation. this resolved my issue.

add this attribute in fragment.


and my issue resolved. i also added customScrollView in addition to this. but after placing liteMode property my issue resolved.

MapFragment in ScrollView - LE DUY CUONG, mask while scrolling. So the solution for it, I found in this answer with a small change. to my map fragment since it was vertical scrollview. So my layout now � "The problem is that what you are trying to do shouldn't be done. You shouldn't be inflating fragments inside other fragments. From Android's documentation: Note: You cannot inflate a layout into a fragment when that layout includes a . Nested fragments are only supported when added to a fragment dynamically.

Please refer to this answer:

It creates a transparent view with the same size of mapFragment, at the same position with mapFragment, then invoke requestDisallowInterceptTouchEvent (true), preventing its ancestors to intercept the touch events.

Add a transparent view over the mapview fragment:

                 android:layout_height="match_parent" />
             <!-- workaround to make google map scrollable -->
                 android:layout_height="match_parent" />

Set touch listener for that transparent view:

    var transparentTouchPanel = view.FindViewById (Resource.Id.transparent_touch_panel);
    transparentTouchPanel.SetOnTouchListener (this);


public bool OnTouch (View v, MotionEvent e) {
    switch (e.Action) {
    case MotionEventActions.Up:
        v.Parent.RequestDisallowInterceptTouchEvent (false);
        v.Parent.RequestDisallowInterceptTouchEvent (true);

    return false;

I thought RequestDisallowInterceptTouchEvent could work with being invoked just once, but apparently it has to be called for EVERY touch event, that's why it has to be put inside onTouchEvent. And the parent will propagate this request to the parent's parent.

I tried set touch event listener for fl_google_map, or for the mapFragment.getView(). Neither of them worked, so creating a transparent sibling view is a tolerable workaround for me.

I have tried, this solution works perfectly. Check the comments under the original post if you do not believe me. I prefer this way because it does not need to create a custom SupportMapFragmet.

Using MapFragment inside ScrollView � Cheesy bits and bobs, I have an app where I need to put a Map inside a ScrollView, doing so will make the ScrollView intercept all the touch events. So the solution is� I have a map fragment inside a vertical scrollview. I want it to be scrollable. but it is not working. I have tried adding a transparent image and also the custom Scrollview from these answers: how to set google map fragment inside scroll view. Google Maps API v2 SupportMapFragment inside ScrollView - users cannot scroll the map vertically

This is the Kotlin version of Alok Nair's answer, I have to say that I am relatively new writing Kotlin code, any comment is appreciated.

Custom SupportMapFragmet:

class FocusMapFragment: SupportMapFragment() {
    var listener: OnTouchListener? = null
    lateinit var frameLayout: TouchableWrapper

    override fun onAttach(context: Context) {

        frameLayout = TouchableWrapper(context)
        frameLayout.setBackgroundColor(ContextCompat.getColor(context, android.R.color.transparent))

    override fun onCreateView(inflater: LayoutInflater, viewGroup: ViewGroup?, bundle: Bundle?): View? {
        val layout = super.onCreateView(inflater, viewGroup, bundle)

        val params = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
        (layout as? ViewGroup)?.addView(frameLayout, params)

        return layout

    interface OnTouchListener {
        fun onTouch()

    inner class TouchableWrapper(context: Context): FrameLayout(context) {
        override fun dispatchTouchEvent(event: MotionEvent?): Boolean {
            when (event?.action) {
                MotionEvent.ACTION_DOWN, MotionEvent.ACTION_UP -> listener?.onTouch()
            return super.dispatchTouchEvent(event)

Then change the fragment class in the xml:


And finally when you initialize the map:

override fun onMapReady(googleMap: GoogleMap) {
    this.googleMap = googleMap

    (childFragmentManager.findFragmentById( as? FocusMapFragment)?.let {
        it.listener = object: FocusMapFragment.OnTouchListener {
            override fun onTouch() {
    // ...

I tested this code in API 28 and 29, I couldn't find a Kotlin version anywhere so thought it could be useful for somebody.

MapFragment inside a ScrollView � GitHub, MapFragment inside a ScrollView. Activity.cs. public class MyActivity : Activity. {. private GoogleMap _map;. private HorizontalScrollView _hsv;. protected� Find local businesses, view maps and get driving directions in Google Maps. When you have eliminated the JavaScript , whatever remains must be an empty page. Enable JavaScript to see Google Maps.

How to solve scrolling issue in Google Maps API v2 , Inside xml file inside java class. How to solve scrolling issue in Google Maps API v2 SupportMapFragment inside ScrollView (when we try to� A Map component in an app. This fragment is the simplest way to place a map in an application. It's a wrapper around a view of a map to automatically handle the necessary life cycle needs. Being a fragment, this component can be added to an activity's layout file simply with the XML below.

android, Google Maps API v2 SupportMapFragment inside ScrollView - users cannot scroll the map vertically. Seudy. I am trying to put a Google map inside a scroll view,� Add a Fragment object to the Activity that will handle the map. The easiest way to do this is to add a <fragment> element to the layout file for the Activity. Implement the OnMapReadyCallback

how to set google map fragment inside scroll view, i want to set a google map fragment inside vertical scroll view, when i do the map does not zoom vertically, how can i override the listener of touching map view� The Xamarin.GooglePlayServices.Maps package contains the Xamarin.Android bindings for the Google Play Services Maps API. To add the Google Play Services Map package, right-click the References folder of your project in the Solution Explorer and click Manage NuGet Packages: This opens the NuGet Package Manager.

  • Check this answer :) it should be helpful!
  • i added transparent image but still couldn't zoom the google map view vertically
  • OMG :D Its workinggg thank you a lot you are the best
  • Hi, it works! The only correction: getColor inside the is now deprecatred, so I used ContextCompat.getColor(getActivity().getApplicationContext(), android.R.color.transparent).
  • Spot on! Thank you very much sir!
  • @AlokNair i have used your answer and it's working fine inside scrollview but now i have case like need to detect ACTION_UP of TouchableWrapper and it wont getting called. do i need to return true from touch event of touchableWrapper?? . can you help ??
  • Very Good Answer! It works properly. But I didn't understand what is the getMap() method and comes from where! I simply wrote mMap = googleMap inside the event-oriented method: onMapReady(GoogleMap googleMap).
  • This solved my problem for a MapView inside a ScrollView (which was in a fragment of a swippable tabs fragment). I previously tried to override onTouchEvent in the MapView, with no success. You saved my day!
  • Fantastic solution. I have no idea why it works. Can someone explain?
  • Overriding Motion events , Smart one