Android Google Maps V2 Marker Click Stuckes The Map View - android

Im developing an android Tab app using FragmentTabs, A Child Fragment class uses Google Maps V2,
map = ((SupportMapFragment) getActivity().getSupportFragmentManager()
.findFragmentById(R.id.map)).getMap();
Once i clicked on a map marker, i need to move to another Child Fragment Class,
map.setOnInfoWindowClickListener(new OnInfoWindowClickListener() {
#Override
public void onInfoWindowClick(Marker marker) {
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.addToBackStack(null);
transaction.replace(R.id.container_framelayout, new FragmentClass());
transaction.commit();
getChildFragmentManager().executePendingTransactions();
}
});
But when i clicked on the marker it freeze my app, i couldn't find any exceptions either..

map.setOnInfoWindowClickListener(new OnInfoWindowClickListener() {
#Override
public void onInfoWindowClick(Marker marker) {
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
YourActivity activity = new YourActivity();
ft.addToBackStack(null);
ft.replace(R.id.container_framelayout, activity);
ft.commit();
}
});

Related

Fragment to fragment is overlapping

I want to open a fragment from my recyclerview adapter class. I am using holder class in bindviewholder, where I code to open my fragment but it overlaps on the current fragment.
holder.buys.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
getcall();
}
});
and
public void getcall() {
FragmentManager fm = context.getActivity().getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
Buy b = new Buy();
ft.replace(R.id.p_buy, b);
ft.addToBackStack(null);
ft.commit();
}

Fragment keeps on adding when the button is clicked

ImageButton imageButton3 =(ImageButton)view.findViewById(R.id.item_two_timer_id);
imageButton3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
getFragmentManager().beginTransaction()
.replace(R.id.container2_main , new TimerFragment2())
.addToBackStack(null)
.commit();
While adding fragment add a tag to identify it.get the fragment by tag and see if the fragment is present.If it is not present create a new Instance and add it.
ImageButton imageButton3 = (ImageButton) view.findViewById(R.id.item_two_timer_id);
imageButton3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
TimerFragment2 timerFragment2;
timerFragment2 = (TimerFragment2) getFragmentManager().findFragmentByTag(TimerFragment2.class.getSimpleName());
if(timerFragment2==null){
timerFragment2=new TimerFragment2();
getFragmentManager().beginTransaction()
.replace(R.id.container2_main, timerFragment2,TimerFragment2.class.getSimpleName())
.addToBackStack(TimerFragment2.class.getSimpleName())
.commit();
}else {
//Dont create fragment again
}
}
});
please try this code if you are setting one fragment
private void initFragment() {
SelectTypeFragment fragment = new SelectTypeFragment();
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.setCustomAnimations(R.anim.enter_from_right, R.anim.exit_to_left, R.anim.enter_from_left, R.anim.exit_to_right); //if you want to set animation
fragmentTransaction.replace(R.id.fl_addvehicle, fragment);
fragmentTransaction.commit();
}
if you are replacing one fragment to with another.
void goToNextFragment() {
((CreateOfferActivity) this.getActivity()).setCreateOfferModel(createOfferModel);
SelectVehicleFragment fragment = new SelectVehicleFragment();
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.setCustomAnimations(R.anim.enter_from_right, R.anim.exit_to_left, R.anim.enter_from_left, R.anim.exit_to_right);
fragmentTransaction.replace(R.id.fl_create_offer, fragment).
addToBackStack("select_vehicle");
fragmentTransaction.commit();
}
You can check it in Fragment but first,
You need to create an object of Fragment TimerFragment2 and check if it's visible or not in timerFragment.isVisible()
ImageButton imageButton3 =(ImageButton)view.findViewById(R.id.item_two_timer_id);
TimerFragment2 timerFragment = new TimerFragment2();
imageButton3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(!timerFragment.isVisible())
getFragmentManager().beginTransaction()
.replace(R.id.container2_main , timerFragment)
.addToBackStack(null)
.commit();

Display Map in fragment after button click

I am trying to use Google Map in an Android project. The details of a person will be displayed in row along with a button on clicking which the location will be shown using Latitude and longitude saved against that person. I am using fragment for display purpose.
activity_show_map.xml
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.dioglgt.myapplication.ClinicFragment" />
The corresponding ClinicFragment java class is given below:
public class ClinicFragment extends Fragment implements OnMapReadyCallback {
private GoogleMap mMap;
public static ClinicFragment newInstance() {
ClinicFragment fragment = new ClinicFragment();
return fragment;
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_show_map, null, false);
SupportMapFragment mapFragment = (SupportMapFragment) this.getChildFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
return view;
}
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
LatLng testLocation = new LatLng(93.972, 26.515);
mMap.addMarker(new MarkerOptions().position(testLocation).title("Marker in Sydney"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(testLocation));
}
}
Now to trigger map display on button click, I have use the code below:
public void onShowLocationClick(View view) {
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.add(R.id.map,new ClinicFragment());
transaction.addToBackStack(null);
transaction.commit();
}
But I am getting the error:
java.lang.IllegalArgumentException: No view found for id 0x7f0e0087 (com.example.dioglgt.myapplication:id/map) for fragment ClinicFragment{9dd7da #0 id=0x7f0e0087}
Please guide in this regard.
Have to use this
public void onShowLocationClick(View view) {
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.add(android.R.id.content,new ClinicFragment());
transaction.addToBackStack(null);
transaction.commit();
}
You are trying to add ClinicFragment on fragment with id map with is inside activity_show_map which is set to Clinic fragment. Try to use frameLayout and replace that frameLayout with your fragment on button click link below:
Fragmentmanager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.fragment_container, new ClinicFragment()).commit();
In above code fragment_container is a FrameLayout which will be replaced with you're ClinicFragment;

CameraUpdate, SupportMapFragment inside ViewPager, not loading second time

I'm facing problems when placing SupportMapFragment inside a fragment that is part of a viewpager with 5 tabs, everything works correctly when viewpager loads the map for first time, but not showing anything whe comming back from another tab. I load the map as follows:
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
FragmentManager fm = getFragmentManager();
mapFragment = (SupportMapFragment) fm.findFragmentById(R.id.map_estate);
if (mapFragment == null) {
mapFragment = SupportMapFragment.newInstance();
fm.beginTransaction().replace(R.id.map_estate, mapFragment).commit();
}
mapFragment.getMapAsync(this);
}
and then use it like:
#Override
public void onMapReady(GoogleMap googleMap) {
this.map = googleMap;
this.map.setMyLocationEnabled(true);
this.map.setMapType(GoogleMap.MAP_TYPE_HYBRID);
this.map.getUiSettings().setMyLocationButtonEnabled(false);
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(center, 16);
mapFragment.getMap().moveCamera(cameraUpdate);
}
Anyone facing same problem? Thanks in advance
ReplacingFragmentManager fm = getFragmentManager(); for FragmentManager fm = getChildFragmentManager(); solved the problem.

Managing Fragments inside tabs

I'm creating a simple Activity, which contains two tabs. I'm following the documentation, so I'm using Fragment.
In my Activity there are only two tabs. The first one is a Map (MapFragment), while the second is a simple List (ListFragment)
All works good, my only problem is that I can't manage correctly the map.
In the onTabSelected callback I have use the following code:
public void onTabSelected(Tab tab, FragmentTransaction ft) {
if (mFragment == null) {
mFragment = Fragment.instantiate(mActivity, mClass.getName(), mArgs);
ft.add(android.R.id.content, mFragment, mTag);
} else {
ft.attach(mFragment);
if(mTag.compareToIgnoreCase("map")==0)
setUpMapIfNeeded((MapFragment)mFragment);
}
}
where map is the tag of my first Activity and the mthod setUpMapIfNeeded is:
private static void setUpMapIfNeeded(MapFragment mMapFragment) {
// Do a null check to confirm that we have not already instantiated the map.
if (mMap == null) {
mMap = mMapFragment.getMap();
// Check if we were successful in obtaining the map.
if (mMap != null) {
mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
}
}
}
So the marker is added in the map only if I select the tab.
The questione is: How can I modify my code for performing the addMarker action also when the activity is started (without pressing explicitly the first tab)?
I have solved adding this code:
#Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
FragmentManager fm = getFragmentManager();
Fragment f = fm.findFragmentByTag("map");
if(f!= null && f.isAdded())
setUpMapIfNeeded((MapFragment)f);
}
I would think that you could:
Refactor the content of onTabSelected to call a addMapFragment(FragmentTransaction ft), just put the whole code inside the addMapFragment method and return the FragmentTransaction. onTabSelected then becomes:
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// if map tab
ft = addMapFragment(ft);
}
This will make it easy to handle the map and markers in onCreate:
protected void onCreate(Bundle savedInstanceState) {
...
if (savedInstanceState != null) {
// not first startup - map should already be created with markers
} else {
FragmentTransaction ft = getFragmentMangager.beginTransaction();
ft = addMapFragment(ft);
ft.commit();
}
}

Categories

Resources