7

Using below code to show and hide MapFragment, and it works just well:

public class MapFragmentActivity extends FragmentActivity {
...........
mMapFragment = ((SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map));
googleMap = mMapFragment.getMap();  
googleMap.setMyLocationEnabled(true);
.....
if(isChecked)
  {                                 
        mMapFragment.getView().setVisibility(View.VISIBLE);                                     
  }
  else 
  {
        mMapFragment.getView().setVisibility(View.GONE);
  }

but whenever i am using it with Animation, Map never hides, it always visible, whereas animation works for me;

if(isChecked)
    {               
        mMapFragment.getView().setVisibility(View.VISIBLE);
        mMapFragment.getView().startAnimation(AnimationUtils.loadAnimation(MapFragmentActivity.this,
         R.anim.slide_up));
    }
    else 
    {
        mMapFragment.getView().setVisibility(View.GONE);
        mMapFragment.getView().startAnimation(AnimationUtils.loadAnimation(MapFragmentActivity.this,
         R.anim.slide_down));
    }
3
  • Let me know if my answer solved your problem otherwise I'll update it... but you must accept any answer as a correct! :) Aug 23, 2015 at 12:53
  • Check my answer, it might help - stackoverflow.com/a/43299111/1380032
    – Rahul
    Apr 8, 2017 at 18:56
  • Have you solved this? In order to help other people if this helped you feel free to upvote and mark this as a correct answer. Thanks :) Jun 28, 2021 at 13:36

4 Answers 4

11

You can try this

      private GoogleMap mMap;
    private SupportMapFragment mMapFragment;

if(isCheked) {
       mMapFragment = ((SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.mapFragment));
    mMap = mMapFragment.getMap();
    
    mMapFragment.getView().setVisibility(View.Visible);
 
}
else {
   mMapFragment = ((SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.mapFragment));
    mMap = mMapFragment.getMap();
    
    mMapFragment.getView().setVisibility(View.INVISIBLE);

}

Or the easy way is doing :

    if(isCheked) {
getSupportFragmentManager().beginTransaction().show(mFragment).commit();
               
        }
        else {
          getSupportFragmentManager().beginTransaction().hide(mFragment).commit();
        }

Try this out and let me know if it works.

Here's a snippet of the first method in Kotlin...

val mapFragment = supportFragmentManager
                .findFragmentById(R.id.map) as SupportMapFragment?
mapFragment?.view?.visibility = View.GONE
0
5

Fragments can be show/hide using transactions.

try {
        FragmentTransaction ft = .getFragmentManager ().beginTransaction ();
        ft.hide (mMapFragment);
    }
    catch (Exception e) {
        e.printStackTrace ();
    }
3

The only way works for me :

View fragmentMap = layout.findViewById(R.id.map_fragment_container_id);
fragmentMap.setVisibility(View.GONE);
-1

You can wrap the map fragment within a FrameLayout

<FrameLayout
    android:id="@+id/mapLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <fragment
        android:id="@+id/theMap"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        class="com.google.android.gms.maps.SupportMapFragment" />
</FrameLayout>

and then call the animation on the FrameLayout

int valueInPixels = (int) getResources().getDimension(R.dimen.task_list_map_height);
FrameLayout mapLayout = (FrameLayout) findViewById(R.id.mapLayout);
mapLayout.animate().translationY(-valueInPixels).setDuration(600);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.