Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Google Maps v2 inside Scrollview shows gray tiles on some devices

There is Android Google Maps v2 inside Scrollview It works on many devices but shows gray tiles on some devices API KEY is Correct,

To use Google Maps V2 inside Scrollview should to use custom class extends SupportMapFragment, Like This :

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

And also set :

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

The image bellow taken by users, Android version : 4.2.2 , Device : Samsung Grand 2

App tested on : Samsung Galaxy Tab 2 10.1" : 4.0.3 And Asus Fonepad 7" Tablet : 5.0.1 And Works well

android:minSdkVersion="10"

enter image description here

like image 400
Hossein Kurd Avatar asked Aug 15 '15 11:08

Hossein Kurd


1 Answers

For a similar problem I have taken a different approach.

Yes, you're right that you cannot have a map directly into a ScrollView... but you can have a FrameLayout and replace it at runtime with the map. Like this:

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="-100dp"
    android:layout_marginBottom="-100dp"
    android:id="@+id/mapViewDetail"
    android:scaleType="centerCrop"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_above="@+id/bottom_layout"
    tools:context="com.yourpackage.app.HomeActivity"
    tools:ignore="MergeRootFrame" />

Then I have created a custom map because I needed some "tweak" (you can avoid this step if you are fine with the default map):

public abstract class MiniMapFragment extends MapFragment {

    @Override
    public View onCreateView(LayoutInflater arg0, ViewGroup arg1, Bundle arg2) {
        View v = super.onCreateView(arg0, arg1, arg2);
        initMap();
        return v;
    }

    public abstract void initMap();
}

Then, in the fragment I just added:

    MiniMapFragment mMapFragment = new MiniMapFragment() {
        @Override
        public void initMap() {
            googleMap = getMap();
            getMap().getUiSettings().setMyLocationButtonEnabled(false);
            getMap().getUiSettings().setZoomControlsEnabled(false);
            getMap().getUiSettings().setScrollGesturesEnabled(false);
        }
    };

    FragmentTransaction fragmentTransaction =
            getFragmentManager().beginTransaction();
    fragmentTransaction.add(R.id.mapViewDetail, mMapFragment);
    fragmentTransaction.commit();
    getFragmentManager().executePendingTransactions();

    MapsInitializer.initialize(this);

And you should be fine.

like image 161
Filnik Avatar answered Sep 25 '22 23:09

Filnik