28

xml:

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/mapFragment"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  class="com.google.android.gms.maps.SupportMapFragment"/>

In regular fragment it goes like this:

  mFragment = (mFragment) (getSupportFragmentManager().findFragmentById(R.id.mFragment));
  mFragment.getView().setVisibility(View.INVISIBLE);

In Google map fragment:

  mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.mapFragment)).getMap();

But how can I set map fragment visibility progrematically?

Can't do it like others fragment.

David
  • 37,109
  • 32
  • 120
  • 141
  • 1
    You can try to cast `mMap` to `Fragment` and execute same `getView().setVisibility(View.INVISIBLE)`, because `SupportMapFragment` is still a fragment. – Evos Dec 12 '12 at 06:03
  • Cannot cast from GoogleMap to Fragment – David Dec 12 '12 at 06:20
  • Did you try to use `getSupportFragmentManager().beginTransacton().hide(mMap).commit()` or `getSupportFragmentManager().beginTransacton().detach(mMap).commit()`? – Evos Dec 12 '12 at 06:25
  • Tried. But didn't work. As long as map declared like this: `private GoogleMap mMap;` I'm getting: "The method hide/detach(Fragment) in the type FragmentTransaction is not applicable for the arguments (GoogleMap)" – David Dec 12 '12 at 06:35
  • Sory my mistake, try this one:`getSupportFragmentManager().beginTransacton().hide(getSupportFragmentManager().findFragmentById(R.id.mapFragment)).commit()` or `getSupportFragmentManager().findFragmentById(R.id.mapFragment).getView().setVisibility(View.INVISIBLE);` – Evos Dec 12 '12 at 06:49

5 Answers5

66

Simple as this:

private GoogleMap mMap;
private SupportMapFragment mMapFragment;

mMapFragment = ((SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.mapFragment));
mMap = mMapFragment.getMap();

mMapFragment.getView().setVisibility(View.INVISIBLE);
David
  • 37,109
  • 32
  • 120
  • 141
  • 3
    This helped me figure out how to programmatically change the height of the support map fragment. Thanks! http://stackoverflow.com/questions/21469345/how-do-you-programmatically-change-the-width-height-of-google-maps-v2-support/21483722#21483722 – loeschg Jan 31 '14 at 15:33
  • 1
    If the map is in a ScrollView, when I hide my scrollview the map doesn't hide. If I hide the map before the Scrollview it still doesn't hide. I had to hide the map, wait 1 milisecond, and then hide the scrollview using Timer. – melanke Jul 02 '14 at 15:35
22

You can do:

getSupportFragmentManager().beginTransaction().hide(mFragment).commit();

to show it:

getSupportFragmentManager().beginTransaction().show(mFragment).commit();
Fernando Gallego
  • 4,064
  • 31
  • 50
1

Both the answer By @David and @ferdy182 are right but they don't told the context.

if you hide/show fragment Programmatically then use @ferdy182 and if you want to hide/show fragment which is in xml. you should follow @David

Let me explain

If you are having a single frameLayout in xml and you want to replace the other fragment in that particular one after another. use this code to add all fragment . they will be place on one another.

for(int i=0;i<4;i++)
        {
            getFragmentManager().beginTransaction().add(R.id.container, frag[i])
            //.addToBackStack(null)
            .commit();
        }// add all these fragment and put them on each other then 

hide all other fragment except which you want to show.

for(int j=0;j<4;j++)
        {
        getFragmentManager().beginTransaction().hide(frag[j]).commit();
        }
        getFragmentManager().beginTransaction().show(frag[0]).commit();

Benefit These fragment work like form in c#. Forum.show and forum.hide(); . Where there current state remain their. these fragment don't call again and again. A problem here i solve it using this technique. 2nd Method

when you are having multiple frameLayout or fragment in xml . you can hide that particular by getting its id.

private GoogleMap mMap;
private SupportMapFragment mMapFragment;

mMapFragment = ((SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.mapFragment));
mMap = mMapFragment.getMap();

mMapFragment.getView().setVisibility(View.INVISIBLE);

Codes

// to show fragment when it is hidden

FragmentManager fm = getSupportFragmentManager();
fm.beginTransaction()
          .show(fragment1)
          .commit();

// to hide fragment

FragmentManager fm = getSupportFragmentManager();
fm.beginTransaction()
          .hide(fragment1)
          .commit();
Community
  • 1
  • 1
Zar E Ahmer
  • 33,936
  • 20
  • 234
  • 300
  • You should not hide it by changing its visibility. hide and show work also with fragments defined in xml, you only have to do a findFragmentById to pass the fragment instance to the fragment transaction – Fernando Gallego Jul 21 '14 at 07:26
0

Only that works for me :

View fragmentMap = layout.findViewById(R.id.map_fragment_container_id);
fragmentMap.setVisibility(View.GONE);
Alexey O.
  • 220
  • 2
  • 11
0

Instead of creating a SupportMapFragment in xml, there could be another approach in which we can define container for SupportMapFragment in xml and then load map from class.

In XML, let say container is FrameLayout-

    <FrameLayout
        android:id="@+id/mapContainer"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="gone" />

In my java class which is a Fragment, I have created two methods to show and hide map. I used detach and it depends on need. We can also use hide instead of detach.

   private void showMap() {
   mMapContainer.setVisibility(View.VISIBLE);
   mSupportMapFragment = SupportMapFragment.newInstance();
   getChildFragmentManager().beginTransaction()
                         .replace(R.id.mapContainer, mSupportMapFragment).commit();
   }

    private void hideMap() {
        mMapContainer.setVisibility(View.VISIBLE);
        if (mSupportMapFragment != null) {
            getChildFragmentManager().beginTransaction()
                                 .detach(mSupportMapFragment).commit();
        }
    }
Rahul
  • 10,457
  • 4
  • 35
  • 55