Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Hide Map Fragment - Android

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));
    }
like image 855
Sun Avatar asked Feb 03 '15 11:02

Sun


2 Answers

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
like image 56
Skizo-ozᴉʞS Avatar answered Nov 03 '22 19:11

Skizo-ozᴉʞS


Fragments can be show/hide using transactions.

try {
        FragmentTransaction ft = .getFragmentManager ().beginTransaction ();
        ft.hide (mMapFragment);
    }
    catch (Exception e) {
        e.printStackTrace ();
    }
like image 5
Murtaza Khursheed Hussain Avatar answered Nov 03 '22 18:11

Murtaza Khursheed Hussain