John
John

Reputation: 750

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.

Upvotes: 0

Views: 534

Answers (2)

Raja
Raja

Reputation: 2815

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();
    }

Upvotes: 1

Shrikant
Shrikant

Reputation: 589

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;

Upvotes: 0

Related Questions