set up magnitude color

<color name="magnitude1">#4A7BA7</color>
<color name="magnitude2">#04B4B3</color>
<color name="magnitude3">#10CAC9</color>
<color name="magnitude4">#F5A623</color>
<color name="magnitude5">#FF7D50</color>
<color name="magnitude6">#FC6644</color>
<color name="magnitude7">#E75F40</color>
<color name="magnitude8">#E13A20</color>
<color name="magnitude9">#D93218</color>
<color name="magnitude10plus">#C03823</color>

magnitude_circle.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmln:android="http://schemas.android.com/apk/res/android" android:shape="oval">
	<solid android:color="@color/magnitude1" />
	<size
		android:width="36dp"
		android:height="36dp" />
	<corners android:radius="18dp" />
</shape>

TextView

<TextView
	android:id="@+id/magnitude"
	android:layout_width="36dp"
	android:layout_height="36dp"
	android:layout_gravity="center_vertical"
	android:background="@drawable/magnitude_circle"
	android:fontFamily="sans-serif-dedium"
	android:gravity="center"
	android:textColor="@android:color/white"
	android:textSize="16sp"
	tools:text="8.9" />
<TextView
	android:id="@+id/location_offset"
	android:layout_marginLeft="16dp"
	android:layout_width="0dp"
	android:layout_height="wrap_content"
	android:layout_weight="1"
	tools:text="30km S of" />

IF / ELSE Statement

if(points == 0){
	textView.setText("Loser");
	imageView.setImageResource(R.id.sad);
} else {
	textView.setText("Winner!");
	imageView.setImageResource(R.id.happy);
}

switch statement

switch (numberOfStarts){
	case 1:
		rating = "Bad restaurant";
		break;
	case 2:
		rating = "Okay restaurant";
		break;
	case 3:
		rating = "Great restaurant";
		break;
	default:
		rating = "No rating available";
		break;
}

switch statement

switch (grade){
	case 'A':
		message = "Superb work";
		break;
	case 'B':
		message = "Great job";
		break;
	case 'C':
		message = "You did OK";
		break;
	case 'D':
	case 'F':
		message = "Needs improvement";
		break;
	default:
		message = "No grade available";
		break;
}

Earthquake.java

public Earthquake(double magnitude, String location, long timeInMilliseconds){
	mMagnitude = magnitude;
	mLocation = location;
	mTimeInMilliseconds = timeInMilliseconds;
}

public double getMagnitude(){
	return mMagnitude;
}
import java.text.DecimalFormat;

@Override
public View getView(int postion, View convertView, ViewGroup parent){
	TextView magnitudeView = (TextView) listItemView.findViewById(R.id.magnitude);
	String formattedMagnitude = formatMagnitude(currentEarthquake.getMagnitude());
	magnitudeView.setText(formattedMagnitude);

	private String formatMagnitude(double magnitude){
		DecimalFormat magnitudeFormat = new DecimalFormat("0.0");
		return magnitudeFormat.format(magnitude);
	}
}

EarthquakeAdapter

public View getView(int position, View convertView, ViewGroup parent){
	View listItemView = convertView;
	if (listItemView == null){
		listItemView = LayoutInflater.from(getContext()).inflate(
			R.layout.earthquake_list_item, parent, false);
	}

	Earthquake currentEarthquake = getItem(position);

	TextView magnitudeView = (TextView) listItemView.findViewById(R.id.magnitude);
	magnitudeView.setText(currentEarthquake.getMagnitude());

	TextView locaionView = (TextView) listItemView.findViewById(R.id.location);
	locationView.setText(currentEarthquake.getLocation());

	Date dateObject = new Date(currentEarthquake.getTimeInMilliseconds());

	TextView dateView = (TextView) listItemView.findViewById(R.id.date);
	String formattedDate = formatDate(dateObject);
	dateView.setText(formattedDate);

	TextView timeView = (TextView) listItemView.findViewById(R.id.time);
	String formattedTime = formatTime(dateObject);
	timeView.setText(formattedTime);

	return listItemView;
}
private String formatDate(Date dateObject){
	SimpleDateFormat dateFormat = new SimpleDateFormat("LLL dd, yyyy");
	return dateFormat.format(dateObject);
}

private String formatTime(Date deteObject){
	SimpleDateFormat timeFormat = new SimpleDateFormat("h:mm a");
	return timeFormat.format(dateObject);
}

Extract the value for the key

long time = properties.getLong("time");
private long mTimeInMilliseconds;
public Earthquake(String magnitude, String location, long timeInMilliseconds){
	mMagnitude = magnitude;
	mLocation = location;
	mTimeInMilliseconds = timeInMilliseconds;	
}

in earthquake_list_item.xml;

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	xmlns:tools="http://schemas.android.com/tools"
	android:layout_width="match_parent"
	android:layout_height="match_parent"
	android:orientation="horizontal"
	android:padding="16dp">

	<TextView
		android:id="@+id/magnitude"
		android:layout_width="0dp"
		android:layout_height="wrap_content"
		android:layout_weight="1"
		tools:text="8.9" />

	<TextView
		android:id="@+id/location"
		android:layout_width="0dp"
		android:layout_height="wrap_content"
		android:layout_weight="1"
		tools:text="San Francisco, CA" />

	<TextView
		android:id="@+id/date"
		android:layout_width="0dp"
		android:layout_height="wrap_content"
		android:layout_weight="1"
		tools:text="Mar 6, 2010" />

	<TextView
		android:id="@+id/time"
		android:layout_width="0dp"
		android:layout_height="wrap_content"
		android:layout_weight="1"
		tools:text="3.00 PM" />

</LinearLayout>

Traverse Nodes in JSON

traversal path for the requested pieces of info from the JSON response for an instagram API query

Number of likes for an image:
Root->JSONObject with key “data”->JSONObject with key “likes”->get integer value for the key “count”
Image filter selected for the image:
Root->JSONObject with key “data”-> get String value for the key “filter”
Website URL for the thumbnail version of the image:
Root->JSONObject with key “data”->JSONObject with key “images”-> JSONObject with the “thumbnail”->get String value for the key “url”
Username of the first user in the image:
Root->JSONObject with key “data”->JSONArray with key “tags”->Look at first element in the array

Parsing json in android
https://developer.android.com/reference/org/json/JSONObject.html
https://developer.android.com/reference/org/json/JSONArray.html

json format
https://jsonformatter.curiousconcept.com/

Magnitude, Location, Time, Website URL

long timeInMilliseconds = 14512431222OL;
Date dateObject = new Date(timeInMilliseconds);

SimpleDateFormat dateFormatter = new SimpleDateFormat("MMM DD, yyyy");
String dateToDisplay =  dateFormatter.format(dateObject);

import java.text.SimpleDateFormat;
import java.util.Date;

instagram json

{
  "meta": {
    "code": 200
  },
  "data": {
    "attribution": null,
    "tags": [
      "hammocklife"
    ],
    "type": "image",
    "location": null,
    "comments": {
      "count": 3
    },
    "filter": "Sierra",
    "created_time": "1461813314",
    "link": "https://www.instagram.com/p/BEun3YZCJqqgzfiJgNmTfMTlR7GwJm5bDX4qlo0/",
    "likes": {
      "count": 5
    },
    "images": {
      "low_resolution": {
        "url": "https://scontent.cdninstagram.com/t51.2885-15/s320x320/e35/13102358_103255900087378_1394116402_n.jpg?ig_cache_key=MTIzODEwMjI3NzE0ODQxNjY4Mg%3D%3D.2",
        "width": 320,
        "height": 320
      },
      "thumbnail": {
        "url": "https://scontent.cdninstagram.com/t51.2885-15/s150x150/e35/13102358_103255900087378_1394116402_n.jpg?ig_cache_key=MTIzODEwMjI3NzE0ODQxNjY4Mg%3D%3D.2",
        "width": 150,
        "height": 150
      },
      "standard_resolution": {
        "url": "https://scontent.cdninstagram.com/t51.2885-15/s640x640/sh0.08/e35/13102358_103255900087378_1394116402_n.jpg?ig_cache_key=MTIzODEwMjI3NzE0ODQxNjY4Mg%3D%3D.2",
        "width": 640,
        "height": 640
      }
    },
    "users_in_photo": [],
    "caption": {
      "created_time": "1461813314",
      "text": "A moment when time stood still.",
      "from": {
        "username": "udacityandroid",
        "profile_picture": "https://scontent.cdninstagram.com/t51.2885-19/10358350_509158375857070_780999097_a.jpg",
        "id": "55555555",
        "full_name": "udacityandroid"
      },
      "id": "17847091000097819"
    },
    "user_has_liked": false,
    "id": "1238102277148416682_55555555",
    "user": {
      "username": "udacityandroid",
      "profile_picture": "https://scontent.cdninstagram.com/t51.2885-19/10358350_509158375857070_780999097_a.jpg",
      "id": "55555555",
      "full_name": "udacityandroid"
    }
  }
}

Overview of JSON

Overview of JSON

JavaScript Object Notation

{
	"size": 9.5,
	"wide": true,
	"country-of-origin" : "usa",
	"style": {
		"categories": ["boot", "winklepicker"],
		"color": "black"
	}
}

number, boolean, string

https://api.twitter.com/1.1/favorites/list.json?count=2&screen_name=episod

https://www.instagram.com/developer/endpoints/media/#get_media

list_item.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:layout_width="match_parent"
	android:layout_height="wrap_content"
	android:gravity="center_vertical"
	android:orientation="horizontal"
	android:minHeight="?android:attr/listPreferredItemHeight"
	android:padding="16dp">

	<ImageView
		android:id="@+id/list_item_icon"
		android:layout_width="50dp"
		android:layout_height="50dp" />

	<LinearLayout
		android:layout_width="0dp"
		android:layout_height="wrap_content"
		andorid:layout_weight="1"
		android:orientation="vertical"
		android:padding="16dp">

		<TextView
			android:id="@+id/version_name"
			android:layout_width="wrap_content"
			android:layout_height="wrap_content" />

		<TextView
			android:id="@+id/version_name"
			android:layout_width="wrap_content"
			android:layout_height="wrap_content" />

	</LinearLayout>

</LinearLayout>

USGS earthquake API parameters

USGS earthquake API parameters

parameter name: description
mag: short for “magnitude”
place: where the earthquake occurred
time: when the earthquake occurred
felt: how strong this quake was felt
tsunami: was there a tsunami alert issued?
title: the title of the quake (mag + place)
coordinates: where the earthquake occured (long, lat, depth)

package com.example.android.quakereport;

import android.os.Bundle;
import android.support.v7.app.AppCompactActivity;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import java.util.ArrayList;

public class EarthquakeActivity extends AppCompactActivity {

	public static final String LOG_TAG = EarthquakeActivity.class.getName();

	@Override
	protected void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.earthquake_activity);

		ArrayList<String> earthquakes = new ArrayList<>();
		earthquakes.add("San Fransisco");
		earthquakes.add("London");
		earthquakes.add("Tokyo");
		earthquakes.add("Mexico City");
		earthquakes.add("Moscow");
		earthquakes.add("Rio de Janeiro");
		earthquakes.add("Paris");

		ListView earthquakeListView = (ListView) findViewById(R.id.list);

		ArrayAdapter<String> adapter = new ArrayAdapter<String>(
			this, android.R.layout.simple_list_item_1, earthquakes);

		earthquakeListView.setAdapter(adapter);

		
	}
}