Bhavin Himmat Goswami Bhavin Himmat Goswami - 3 months ago 43
Android Question

Add toolbar in fragment activity

Currently i'm working on google map api . I want to add toolbar/actionbar to my map fragment.

please check the image in the link below:-

This is the Screen shot please check this out

this is my layout file .xml

<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`enter code here`="match_parent"
android:layout_height="match_parent"
tools:context="com.android.bhavin04.MapsActivity"
/>


my MaintActivity extends FragmentActivity and implements OnMapReadyCallback.
how to add toolbar to my main activity. a help will be appreciated.

main Activity code:

package com.android.bhavin04.ganeshutsavmumbai;

import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Point;
import android.location.LocationManager;
import android.provider.Settings;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.widget.Toolbar;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.UiSettings;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.LatLngBounds;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import com.pushbots.push.Pushbots;

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

private GoogleMap mMap;

private LatLngBounds Mumbai = new LatLngBounds(
new LatLng(-44, 113), new LatLng(19.075984, 72.877656));


@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);

Pushbots.sharedInstance().init(this);




// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}

@Override
public void onMapReady(GoogleMap googleMap) {

mMap = googleMap;

mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(Mumbai.getCenter(), 14));

if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

return;
}
// mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
mMap.setMyLocationEnabled(true);
mMap.setTrafficEnabled(true);

LatLng Mumbai = new LatLng(19.075979, 72.879696);
mMap.moveCamera(CameraUpdateFactory.newLatLng(Mumbai));


}
}

Answer

Extend your activity from ActionBarActivity instead of FragmentActivity. (MainActivity extends ActionBarActivity)

ActionBarActivity extends from FragmentActivity hence it will work.

Comments