• Skip to main content
  • Skip to secondary menu
  • Skip to primary sidebar
  • Skip to footer
  • Home
  • About
  • Projects
    • GStaticMap WP Plugin
  • Contact
  • Privacy Policy

Lorenz Blog

All About Web & Mobile Application Development

  • Featured Articles
  • Gadgets
    • Android
    • Blackberry
  • Programming
    • Android
    • PHP
    • Java Script
    • MySQL
    • Postgresql
    • Flex
    • Web
  • Software
    • Mac OS
    • Windows
    • Linux
  • Web
You are Here » Home >> Information Technology >> Programming >> Android >> How to Make Android Map Scrollable Inside a ScrollView Layout

How to Make Android Map Scrollable Inside a ScrollView Layout

February 10, 2014 by Lorensius Londa 11 Comments

Technically, placing a map view  inside a ScrollView layout container can make the map becoming difficult to scroll. This is because the scrollable layout container will intercept the touch event so the map will lose the touch event, making it difficult to scroll or pan. This happens either on Android Map V2 or the previous versions.

In this case, i work with the SupportMapFragment in Android Map V2 so the workaround is to make a custom SupportMapFragment class so we can override its touch event. This solution is based on gaucho’s answer on related post in stackoverflow with some small changes made by me.

WorkaroundMapFragment.java

package net.londatiga.android.ui.fragment;

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;

import com.google.android.gms.maps.SupportMapFragment;

public class WorkaroundMapFragment extends SupportMapFragment {
	private OnTouchListener mListener;

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

        TouchableWrapper frameLayout = new TouchableWrapper(getActivity());

        frameLayout.setBackgroundColor(getResources().getColor(android.R.color.transparent));

        ((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) {
        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);
      }
    }
}

In this 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 occured, dispatchTouchEvent will be called and the listener mListener will handle it.

The ScrollView layout container, fragment_my_map.xml

<?xml version="1.0" encoding="UTF-8"?>
<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/sv_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <!-- other child views //-->
     <fragment
        android:tag="fragment_map"
       android:id="@+id/fragment_map"
       android:layout_width="match_parent"
       android:layout_height="175dp"
       android:layout_marginTop="@dimen/activity_horizontal_margin"
       class="net.londatiga.android.ui.fragment.WorkaroundMapFragment"/>
</ScrollView>

Inside map activity that uses the map, MyMapActivty.java

package net.londatiga.android.ui;

import net.londatiga.android.ui.fragment.WorkaroundMapFragment;
import com.google.android.gms.maps.GoogleMap;
import android.widget.ScrollView;

public class MyMapActivty extends MapActivity {
    private ScrollView mScrollView;
    private GoogleMap mMap;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.fragment_my_map);

        mMap = ((WorkaroundMapFragment) getSupportFragmentManager().findFragmentById(R.id.fragment_map)).getMap();
        mScrollView = (ScrollView) findViewById(R.id.sv_container);

       ((WorkaroundMapFragment) getSupportFragmentManager().findFragmentById(R.id.fragment_map)).setListener(new WorkaroundMapFragment.OnTouchListener() {
          @Override
          public void onTouch() {
              mScrollView.requestDisallowInterceptTouchEvent(true);
          }
     });
   }
}

Inside activity class, get the GoogleMap mMap and ScrollView container mScollView then set the OnTouchListener to the WorkaroundMapFragment. When touch event occured,  onTouch handler will be called and we block the event being dispatched the ScrollView layout. This will make the map scrollable without the ScrollView being scrollable too.

[ad#ad-720×90]

Facebooktwitterredditpinterestlinkedinmailby feather

Related posts:

  1. How to Make Android ListView or GridView Expandable inside ScrollView
  2. Sample Code to Get MD5 Checksum of a File in Android
  3. Simple JSON RPC Client for Android
  4. Android Coding Tips: How to Create Options Menu on Child Activity inside an ActivityGroup

Filed Under: Android Tagged With: Android, map, scrollable, scrollview

About Lorensius Londa

Passionate web and mobile application developer. Co-founder of TRUSTUDIO, loves programming, Android, aviation, travelling, photography, coffee and gym mania.

Reader Interactions

Comments

  1. Ademar says

    April 10, 2014 at 8:50 am

    Works like a charm!

    Reply
  2. Sanket says

    April 18, 2014 at 6:08 pm

    Above code is working good but one another problem arise google map controller inner ZoomIn,ZoomOut(+,-) sign not working when click

    Reply
  3. Gebeppe says

    October 16, 2014 at 9:18 pm

    It’s possible to have this project? My project doesn’t work.

    Reply
  4. Nagarjun Reddy says

    October 18, 2014 at 1:18 pm

    It’s working awesome…

    Reply
  5. Darshana says

    December 13, 2014 at 12:23 pm

    You save my day

    Reply
  6. ikonoklast says

    March 12, 2015 at 5:00 pm

    Thank you! That’s what i needed, it’s perfect.

    Reply
  7. Philip Trowe says

    March 15, 2015 at 9:46 pm

    Check out my blog. The following post includes a video that shows you how to create an Android smartphone application that displays a vertically scrolling list of countries on the screen. The application is created using the Eclipse IDE and Android SDK. It features an “OK” button at the bottom of the screen that, when pressed, terminates the application.

    In the process of making this Android application you will encounter the following SDK object types, etc:
    • ScrollView,
    • Button,
    • View,
    • TableLayout,
    • TableRow,
    • OnClickListener,
    • TextView,
    • Colours in Android represented by hexadecimal numbers.

    http://androidprogrammeringcorner.blogspot.com/2015/03/pak-longs-android-programming-corner.html

    Best regards,

    Philip

    Reply
  8. Ravi kesharwani says

    November 25, 2016 at 5:12 pm

    Where is source code

    Reply
  9. Mayank Sharma says

    May 25, 2017 at 1:53 pm

    Thanks 🙂

    Working perfect , you save my time 😀

    Reply
  10. Edwin Alvaradko says

    December 6, 2017 at 2:29 am

    Nice solution, clean and works perfectly!

    Reply
  11. Ayten Göksenin Barutçu says

    February 21, 2018 at 9:40 pm

    What happens if we are using fragment instead of activity? This code below does not work since fragment forces you to use child fragment manager?

    mMap = ((WorkaroundMapFragment) getChildFragmentManager().findFragmentById(R.id.map)).getMapAsync(this);

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About Me

A husband, father of two, passionate software developer, diy lover and home baker who loves to learn new things. Read More…

  • Facebook
  • GitHub
  • Google+
  • Instagram
  • Twitter
  • YouTube

Featured Articles

How to Setup MQTT Server Using Mosquitto and Libwebsocket on Freebsd

Blue Bamboo P25 Printer Android Demo Application With Source Code

Simple JSON RPC Client for Android

How to Send Message to Google Cloud Messaging (GCM) Server Using JSON and PHP

Footer

Recent Comments

  • Aditya Dabas on About
  • Ayten Göksenin Barutçu on How to Make Android Map Scrollable Inside a ScrollView Layout
  • mang jojot on About
  • Hussain on How to Programmatically Scan or Discover Android Bluetooth Devices

Recent Posts

  • How to Fix Blank Screen on WordPress Add/Edit Post Page
  • How to Programmatically Restart the ESP32 Board
  • How to Get Hardware Info of ESP32
  • How to Setup MQTT Server Using Mosquitto and Libwebsocket on Freebsd

Latest Tweets

To protect our users from spam and other malicious activity, this account is temporarily locked. Please log in to https://twitter.com to unlock your account.

Copyright © 2023 · Magazine Pro on Genesis Framework · WordPress · Log in