Use custom Google map fragment in your XML.
Here is the complete code that worked for me. If you guys have any questions, please let me know.
In your XML file, add the following as map fragment
<fragment android:id="@+id/map_with_scroll_fix" android:name="com.myapplication.maputil.GoogleMapWithScrollFix" android:layout_width="match_parent" android:layout_height="match_parent" />
Here is the custom class for map
package com.myapplication.maputil; import android.content.Context; import android.os.Bundle; import android.view.LayoutInflater; import android.view.MotionEvent; import android.view.View; import android.view.ViewGroup; import android.widget.FrameLayout; import com.google.android.gms.maps.SupportMapFragment; public class GoogleMapWithScrollFix extends SupportMapFragment { private OnTouchListener mListener; @Override public View onCreateView(LayoutInflater layoutInflater, ViewGroup viewGroup, Bundle savedInstance) { View layout = super.onCreateView(layoutInflater, viewGroup, savedInstance); TouchableWrapper touchableWrapper = new TouchableWrapper(getActivity()); touchableWrapper.setBackgroundColor(getResources().getColor(android.R.color.transparent)); ((ViewGroup) layout).addView(touchableWrapper, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); return layout; } public void setListener(OnTouchListener listener) { mListener = listener; } public interface OnTouchListener { void onTouch(); } public class TouchableWrapper extends FrameLayout { public TouchableWrapper(Context context) { super(context); } @Override public boolean dispatchTouchEvent(MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: mListener.onTouch(); break; case MotionEvent.ACTION_UP: mListener.onTouch(); break; } return super.dispatchTouchEvent(event); } } }
Add the following in your activity class, to initialize mapview. That's it. Tada :)
((GoogleMapWithScrollFix) getSupportFragmentManager() .findFragmentById(R.id.map_with_scroll_fix)).getMapAsync(new OnMapReadyCallback() { @Override public void onMapReady(GoogleMap googleMap) { ScrollView mScrollView = findViewById(R.id.scrollview); //parent scrollview in xml, give your scrollview id value ((GoogleMapWithScrollFix) getSupportFragmentManager() .findFragmentById(R.id.map_with_scroll_fix)).setListener(new GoogleMapWithScrollFix.OnTouchListener() { @Override public void onTouch() { //Here is the magic happens. //we disable scrolling of outside scroll view here mScrollView.requestDisallowInterceptTouchEvent(true); } }); } });
onInterceptTouchEventand usedispatchTouchEventto dispatch the touches to necessary views. Try capturing touch events and dispatch them to the map fragment once uses touches the map.dispatchTouchEvent. I ended up testing bounds and returning true/false based on it (see my answer below)