Display Legal Notices For Google Maps Android Api V2 On Options Menu

Posted on

Last postal service demonstrate a simplest map app using Google Maps Android API v2 provided yesteryear Android Studio.

Refer to the document “Introduction to the Google Maps Android API – Attribution Requirements“:

If y’all role the Google Maps Android API inwards your application, y’all must include the Google Play Services attribution text equally business office of a “Legal Notices” department inwards your application. Including legal notices equally an independent carte item, or equally business office of an “About” carte item, is recommended.

The attribution text is available yesteryear making a telephone yell upward to GoogleApiAvailability.getOpenSourceSoftwareLicenseInfo.

This postal service is going to add together “Legal Notices” to OptionsMenu, to display the OpenSourceSoftwareLicenseInfo inwards dialog, as well as besides “About Me”.

Edit values/strings.xml, to add together sting resources of “menu_legalnotices” as well as “menu_about”:

<resources>     <string name="app_name">AndroidStudioMapApp</string>     <string name="title_activity_maps">Map</string>     <string name="menu_legalnotices">Legal Notices</string>     <string name="menu_about">About</string> </resources>  

Create menu/activity_main.xml to define out menu:

<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:app="http://schemas.android.com/apk/res-auto">     <item         android:id="@+id/menu_legalnotices"         android:orderInCategory="100"         app:showAsAction="ifRoom"         android:title="Display Legal Notices for Google Maps Android API v2 on Options Menu"/>     <item         android:id="@+id/menu_about"         android:orderInCategory="100"         app:showAsAction="ifRoom"         android:title="Display Legal Notices for Google Maps Android API v2 on Options Menu"/>  </menu> 

Edit MapsActivity, to grip OptionsMenu. In gild to display OptionsMenu inwards FragmentActivity, alter to extend AppCompatActivity.

package com.blogspot.android_er.androidstudiomapapp;          import android.app.AlertDialog;         import android.content.DialogInterface;         import android.content.Intent;         import android.net.Uri;         import android.os.Bundle;         import android.support.v7.app.AppCompatActivity;         import android.view.Menu;         import android.view.MenuItem;          import com.google.android.gms.common.GoogleApiAvailability;         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.model.LatLng;         import com.google.android.gms.maps.model.MarkerOptions;  populace shape MapsActivity extends AppCompatActivity implements OnMapReadyCallback {      someone GoogleMap mMap;      @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_maps);         // Obtain the SupportMapFragment as well as instruct notified when the map is produce to endure used.         SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()                 .findFragmentById(R.id.map);         mapFragment.getMapAsync(this);      }      /**      * Manipulates the map in ane trial available.      * This callback is triggered when the map is produce to endure used.      * This is where nosotros tin add together markers or lines, add together listeners or motion the camera. In this case,      * nosotros only add together a marking nigh Sydney, Australia.      * If Google Play services is non installed on the device, the user volition endure prompted to install      * it within the SupportMapFragment. This method volition solely endure triggered in ane trial the user has      * installed Google Play services as well as returned to the app.      */     @Override     populace void onMapReady(GoogleMap googleMap) {         mMap = googleMap;          // Add a marking inwards Sydney as well as motion the photographic idiot box camera         LatLng sydney = novel LatLng(-34, 151);         mMap.addMarker(new MarkerOptions().position(sydney).title("Marker inwards Sydney"));         mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));     }      @Override     populace boolean onCreateOptionsMenu(Menu menu) {         getMenuInflater().inflate(R.menu.activity_main, menu);         render super.onCreateOptionsMenu(menu);     }      @Override     populace boolean onOptionsItemSelected(MenuItem item) {         switch (item.getItemId()) {             illustration R.id.menu_legalnotices:                 String LicenseInfo = GoogleApiAvailability                         .getInstance()                         .getOpenSourceSoftwareLicenseInfo(MapsActivity.this);                 AlertDialog.Builder LicenseDialog =                         novel AlertDialog.Builder(MapsActivity.this);                 LicenseDialog.setTitle("Legal Notices");                 LicenseDialog.setMessage(LicenseInfo);                 LicenseDialog.show();                 render true;             illustration R.id.menu_about:                 AlertDialog.Builder aboutDialogBuilder =                         novel AlertDialog.Builder(MapsActivity.this);                 aboutDialogBuilder.setTitle("About Me")                         .setMessage("https://rootandroid.org/");                  aboutDialogBuilder.setPositiveButton("visit",                         novel DialogInterface.OnClickListener() {                     @Override                     populace void onClick(DialogInterface dialog, int which) {                         String url = "https://rootandroid.org/";                         Intent i = novel Intent(Intent.ACTION_VIEW);                         i.setData(Uri.parse(url));                         startActivity(i);                     }                 });                  aboutDialogBuilder.setNegativeButton("Dismiss",                         novel DialogInterface.OnClickListener() {                     @Override                     populace void onClick(DialogInterface dialog, int which) {                         dialog.cancel();                     }                 });                  AlertDialog aboutDialog = aboutDialogBuilder.create();                 aboutDialog.show();                  render true;         }         render super.onOptionsItemSelected(item);     } }  

Step-by-step of Android Google Maps Activity using Google Maps Android API v2, on Android Studio.