androidandroid-layoutandroid-fragmentsgoogle-maps-android-api-2supportmapfragment

Interact with Button from another layout in SupportMapFragment


I would like to know how to hide/show the FloatingActionButton when clicking a marker in the map.

Here's the Maps code:

public class MapsActivity extends SupportMapFragment implements 
OnMapReadyCallback, GoogleMap.OnMapClickListener, 
GoogleMap.OnMarkerClickListener, GoogleMap.OnInfoWindowClickListener {

private static final String TAG = "Maps";
private GoogleMap mMap;
private LocationManager locationManager;
private ArrayList<LatLng> latlngs = new ArrayList<>();
private MarkerOptions markerOptions = new MarkerOptions();
private Marker markerN;
private Marker markerO;

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

@Override
public void onMapReady(GoogleMap googleMap) {

    try {
        Criteria criteria = new Criteria();
        locationManager = (LocationManager) 
getActivity().getSystemService(Context.LOCATION_SERVICE);
        mMap = googleMap;
        mMap.setOnMapClickListener(this);
        mMap.setOnMarkerClickListener(this);
        mMap.getUiSettings().setZoomControlsEnabled(true);
        mMap.getUiSettings().setMapToolbarEnabled(false);
        mMap.setMyLocationEnabled(true);
        Location location = 
locationManager.getLastKnownLocation(locationManager
                .getBestProvider(criteria, false));
        double latitude = location.getLatitude();
        double longitude = location.getLongitude();
        LatLng sydney = new LatLng(latitude, longitude);
        mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(sydney, 15.0f));
        mMap.setOnInfoWindowClickListener(this);
    } catch (SecurityException ex) {
        Log.e(TAG, "Error", ex);
        requestPermissions(new String[]
{Manifest.permission.ACCESS_FINE_LOCATION},
                1);
    } catch (Exception e) {
        System.out.println(e);
    }

    try {
        // Customise the styling of the base map using a JSON object defined
        // in a raw resource file.
        boolean success = mMap.setMapStyle(
                MapStyleOptions.loadRawResourceStyle(
                        getContext(), R.raw.style_json));

        if (!success) {
            Log.e("MapsActivityRaw", "Style parsing failed.");
        }
    } catch (Resources.NotFoundException e) {
        Log.e("MapsActivityRaw", "Can't find style.", e);
    }

    latlngs.add(new LatLng(-22.978608, -49.869901));
    for (LatLng point : latlngs) {
        markerOptions.position(point);
        markerOptions.title("Local");
        markerOptions.snippet("Info");
        markerO = mMap.addMarker(markerOptions);
    }
}

@Override
public void onMapClick(LatLng latLng) {
    mMap.clear();
    markerOptions.position(latLng);

    markerOptions.title("Deseja cadastrar este local?");
    markerOptions.snippet("Pressione aqui");
    markerN = mMap.addMarker(markerOptions);
    mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, 17.0f));
    Toast.makeText(getContext(), "Coord: " + latLng.toString(), 
Toast.LENGTH_LONG).show();
}

@Override
public void onRequestPermissionsResult(int requestCode, String 
permissions[], int[] grantResults) {
    switch (requestCode) {
        case 1: {
            // If request is cancelled, the result arrays are empty.
            if (grantResults.length > 0
                    && grantResults[0] == PackageManager.PERMISSION_GRANTED) 
{

            } else {
                // permission denied, boo! Disable the
                // functionality that depends on this permission.
            }
            return;
        }
        // other 'case' lines to check for other
        // permissions this app might request
    }
}

@Override
public void onInfoWindowClick(Marker marker) {
    if (marker.equals(markerN)) {
        Toast.makeText(getContext(), "Clique 2", Toast.LENGTH_LONG).show();
    } else {

    }
}

@Override
public boolean onMarkerClick(Marker marker) {
    if (marker.equals(markerN)) {
        Toast.makeText(getContext(), "Clique 1", Toast.LENGTH_LONG).show();
    } else {
        Toast.makeText(getContext(), "Abrir Informações", 
Toast.LENGTH_LONG).show();
    }
    return false;
}
}

And that's the XML I want to interact:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.alan.unigeo.MainActivity">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?android:attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

</android.support.design.widget.AppBarLayout>

<include layout="@layout/content_main" />

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|end"
    android:layout_margin="@dimen/fab_margin"
    app:srcCompat="@android:drawable/ic_dialog_email" />

</android.support.design.widget.CoordinatorLayout>

I've tryed looking for examples alike that situation but failed. The Maps is inside a fragment of a layout, and the FloatingActionButton is placed in another layout "above" the previous one. I don't know if I need to show any other code to help you guys, if needed, just ask for it.


Solution

  • I have the feature in my program.

    If you do not want to show it, use this code:

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        ((FloatingActionButton) getView().getRootView().findViewById(R.id.fab)).hide();
    }
    
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ((FloatingActionButton) getView().getRootView().findViewById(R.id.fab)).show();
    }
    

    If you want to show it, just switch the .show() and .hide().