org.austindroids.austindroidstestapp.MainActivity.java Source code

Java tutorial

Introduction

Here is the source code for org.austindroids.austindroidstestapp.MainActivity.java

Source

package org.austindroids.austindroidstestapp;

import android.app.ActionBar;
import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

public class MainActivity extends Activity {

    /**
     * Note that this may be null if the Google Play services APK is not available.
     */
    private GoogleMap googleMap;
    private static final String LOGPROC = "org.austindroids.austindroidstestapp";
    final int RQS_GooglePlayServices = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if (BuildConfig.DEBUG) {
            Log.d(LOGPROC, "Inflated main activity.");
        }
        Log.d(LOGPROC, "Now setting up map.");
        setUpMapIfNeeded();

    }

    @Override
    protected void onResume() {
        super.onResume();
        setUpMapIfNeeded();
    }

    private void checkGooglePlay() {
        // Check status of Google Play Services
        int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);

        // Check Google Play Service Available
        try {
            if (status != ConnectionResult.SUCCESS) {
                GooglePlayServicesUtil.getErrorDialog(status, this, RQS_GooglePlayServices).show();
            }
        } catch (Exception e) {
            Log.e("Error: GooglePlayService Not Available: ", "" + e);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        ActionBar actionBar = getActionBar();
        actionBar.setSubtitle("Fish App");
        actionBar.setTitle("Fish App");
        actionBar.setBackgroundDrawable(getResources().getDrawable(R.drawable.fish));
        // Dim navigation buttons on bottom
        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.menu_legalnotices:
            String LicenseInfo = GooglePlayServicesUtil.getOpenSourceSoftwareLicenseInfo(getApplicationContext());
            AlertDialog.Builder LicenseDialog = new AlertDialog.Builder(MainActivity.this);
            LicenseDialog.setTitle("Legal Notices");
            LicenseDialog.setMessage(LicenseInfo);
            LicenseDialog.show();
            return true;
        case R.id.action_settings:
            Toast.makeText(getApplicationContext(), "Settings not Available.", Toast.LENGTH_SHORT).show();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    private void setUpMapIfNeeded() {
        // Do a null check to confirm that we have not already instantiated the map.
        if (googleMap == null) {
            // Try to obtain the map from the SupportMapFragment.
            googleMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
            // Check if we were successful in obtaining the map.
            if (googleMap != null) {
                setUpMap();
            } else {
                Toast.makeText(getApplicationContext(), "Sorry! unable to create maps", Toast.LENGTH_SHORT).show();
            }
        }
    }

    /**
     * This is where we can add markers or lines, add listeners or move the camera. In this case, we
     * just add a marker near Africa.
     * <p/>
     * This should only be called once and when we are sure that {@link #googleMap} is not null.
     */
    private void setUpMap() {
        LatLng latlng = new LatLng(30.22399, -97.59124);
        googleMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
        MarkerOptions marker = new MarkerOptions().position(latlng).title("Marker");
        //marker.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE));

        // Changing marker icon
        marker.icon(BitmapDescriptorFactory.fromResource(R.drawable.goldfish));
        googleMap.addMarker(marker);

        // move camera position
        CameraPosition cameraPosition = new CameraPosition.Builder().target(latlng).zoom(12).build();

        googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));

    }
}