I'm working on a simple map app, i used the navigationDrawer
to put my map in the first option of the drawer
.. now the problem is when i put any button in the same fragment
of the map or the same layout it doesn't perform what i want!
for example : i need to zoom on my location when i click the button
.
i tried some solutions i found on the site but there was no luck..
xml code :
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment android:layout_height="match_parent"
android:layout_width="405dp" android:id="@+id/mapfragment"
class="com.google.android.gms.maps.MapFragment"
android:layout_marginEnd="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginRight="8dp"
/>
<RelativeLayout android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:id="@+id/bt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="46dp"
android:text="Button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:onClick="onClick"
/>
</RelativeLayout>
</android.support.constraint.ConstraintLayout>
java code :
public class maptest extends Fragment implements OnMapReadyCallback,View.OnClickListener {
GoogleMap mp;
@Nullable
View v1;
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
Button bt2=(Button)v1.findViewById(R.id.bt1);
bt2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(getActivity(), "ggg", Toast.LENGTH_SHORT).show();
}
});
return inflater.inflate(R.layout.newmapfragment, container,false);
}
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
MapFragment fragment= (MapFragment) getChildFragmentManager().findFragmentById(R.id.mapfragment);
fragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap mp) {
// Add a marker in Sydney, Australia,
// and move the map's camera to the same location.
LatLng sydney = new LatLng(31.163937, 35.710373);
mp.addMarker(new MarkerOptions().position(sydney)
.title("Marker in Sydney"));
mp.moveCamera(CameraUpdateFactory.newLatLng(sydney));
mp.addMarker(new MarkerOptions().position(new
LatLng(31.163937,35.710373)).title("mymap")).showInfoWindow();
mp.animateCamera(CameraUpdateFactory.newLatLngZoom(new
LatLng(31.163937,35.710373),16));
}
@Override
public void onClick(View view) {
Button b = (Button) view.findViewById(R.id.bt1);
b.setOnClickListener(this);
LatLng sydney = new LatLng(31.163937, 35.710373);
mp.addMarker(new MarkerOptions().position(sydney)
.title("Marker in Sydney"));
mp.moveCamera(CameraUpdateFactory.newLatLng(sydney));
mp.addMarker(new MarkerOptions().position(new
LatLng(31.163937,35.710373)).title("mymap")).showInfoWindow();
mp.animateCamera(CameraUpdateFactory.newLatLngZoom(new
LatLng(31.163937,35.710373),16));
}
}
any help will be appreciated..