Array and ArrayList

Array, ArrayList
-can change size one created? no, yes
-is a class? no, yes
-use methods to access and modify element? no, yes
-what can it store? primitive and objects, only objects

ArrayList

import java.util.*;

public class ArrayListExample {
   public static void main(String args[]) {
      /*Creation of ArrayList: I'm going to add String
       *elements so I made it of string type */
	  ArrayList<String> obj = new ArrayList<String>();

	  /*This is how elements should be added to the array list*/
	  obj.add("Ajeet");
	  obj.add("Harry");
	  obj.add("Chaitanya");
	  obj.add("Steve");
	  obj.add("Anuj");

	  /* Displaying array list elements */
	  System.out.println("Currently the array list has following elements:"+obj);

	  /*Add element at the given index*/
	  obj.add(0, "Rahul");
	  obj.add(1, "Justin");

	  /*Remove elements from array list like this*/
	  obj.remove("Chaitanya");
	  obj.remove("Harry");

	  System.out.println("Current array list is:"+obj);

	  /*Remove element from the given index*/
	  obj.remove(1);

	  System.out.println("Current array list is:"+obj);
   }
}

NumberActivity.java

/.../
package com.example.android.miwok;

public class NumbersActivity extends AppCompactActivity {

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

		// Create on array of words
		ArrayList<String> words = new ArrayList<String>();

        words.add("one");
        words.add("two");
        words.add("three");
        words.add("four");
        words.add("five");
        words.add("six");
        words.add("seven");
        words.add("eight");
        words.add("nine");
        words.add("ten");

		Log.v("NumbersActivity","Word at index 0: " + words.get(0));
		Log.v("NumbersActivity","Word at index 1: " + words.get(1));
	}
}

Arrays, Lists, Loops

All variables need a data type
integer, boolean, TextView, ImageView

int x = 14;
int y = 60;
int result = x + y;
not make sense of two boolean

int, double, boolean, char, long, float, short, byte, String

create array
int [] shoeSizesAvailable = new int[3];

initialize elements in an array
shoeSizesAvailable[0] = 5;
shoeSizesAvailable[1] = 7;
shoeSizesAvailable[2] = 10;

Access elements in an array
shoeSizesAvailable[0] -> value of 5
shoeSizesAvailable[1] -> value of 7
shoeSizesAvailable[2] -> value of 10

int[] soccerTeam = new int[11];
soccerTeam[0] = 5;
soccerTeam[10] = 1;
soccerTeam[4] = 23;
soccerTeam[1] = 10;

number of array list

/.../
package com.example.android.miwok;

public class NumbersActivity extends AppCompactActivity {

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

		// Create on array of words
		String[] words = new String[10];
		word[0] = "one";
		word[1] = "two";
		word[2] = "three";
		word[3] = "four";
		word[4] = "five";
		word[5] = "six";
		word[6] = "seven";
		word[7] = "eight";
		word[8] = "nine";
		word[9] = "ten";

		Log.v("NumbersActivity","Word at index 0: " + words[0]);
		Log.v("NumbersActivity","Word at index 1: " + words[1]);
	}
}

shorter code

package com.example.android.miwok;

import android.conent.Intent;
import android.support.v7.app.AppCompactActivity;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends AppCompactActivity {

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

		// Find the View that shows the numbers category
		TextView numbers = (TextView)findViewById(R.id.numbers);

		//Set a clickListener on that View
		numbers.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View view){
				Intent numbersIntent = new Intent(MainActivity.this, NumbersActivity.class);

				startActivity(numbersIntent);
			}
		});
	}

}
Button button = (Button) findViewById(R.id.ze_button);
button.setOnClickListener(new View.OnClickListener(){
	@Override
	public void onClick(view v){
		doSomeStuff();
	}
});
android:onClick="myListener"
public void myListener(View view){
	doSomeStuff();
}

setup an event listener

setup an event listener

public class NumbersClickListener implements OnClickListener {
	@Override
	public void onClick(View view) { Toast.makeText(view.getContext(),
		"Open the list of numbers", Toast.LENGTH_SHORT).show();}
}

NumbersClickListener clickListener = new NumbersClickListener();

buttonView.setOnClickListener(clickListener);
TextView priceTextView = new TextView()
package com.example.android.miwok;

import android.view.View;
import android.widget.Toast;

public class NumbersClickListener implements View.OnClickListener {

	@Override
	public void onClick(View view) { 
		Toast.makeText(view.getContext(),
			"Open the list of numbers", Toast.LENGTH_SHORT).show();
	}
}

MainActivity.java

package com.example.android.miwok;

import android.conent.Intent;
import android.support.v7.app.AppCompactActivity;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends AppCompactActivity {

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

		NumbersClickListener clickListener = new NumbersClickListener();

		// Find the View that shows the numbers category
		TextView numbers = (TextView)findViewById(R.id.numbers);

		//Set a clickListener on that View
		numbers.setOnClickListener(click.Listener);
	}

}

AndroidManifest activity tag

AndroidManifest activity tag
https://developer.android.com/guide/topics/manifest/activity-element.html

Miwok App
background colors(colors.xml)
theme of the app(styles.xml)
how a category looks(category style in styles.xml)
list item height(dimensions.xml)
category names(strings.xml)
MainActivity.java(activity_main.xml layout file)

listen for user input event
-user interact with app
-> click
-behind the scenes

android input event
https://developer.android.com/guide/topics/ui/ui-events.html

callback method
onClick(), onLongClick(), onFocusChange(), onKey(), onTouch(), onCreateContextMenu()
onLongClick(), onKey(), onTouch()

TextView method
-getText, setText, setOnClickListener

onClick:show a toast message, change the background of the TextView, pop up a dialog

onViewClick Method
https://developer.android.com/reference/android/view/View.OnClickListener.html

concrete class, abstract class, interface

Types of intents

implicit intent
-action, data uri, category, components, extras

Implicit Intent

Intent intent = new Intent(Intent.ACTION_SENDTO);
sendIntent.setData(Uri.parse("mailto:"));
sendIntent.putExtra(Intent.EXTRA_SUBJECT, "Just Java order for " + name);
sendIntent.putExtra(Intent.EXTRA_TEXT, priceMessage);

if (sendIntent.resolveActivity(getPackageManager()) != null){
	startActivity(sendIntent);
}

Explicit Intent

Intent intent = new Intent(this, NumbersActivity.class);
startActivity(intent);

Use an Intent

Add onclick method on main java file and xml file.

res > layout > activity_main.xml

<?xml version="1.0" encoding="utf-8" ?>
<LinearLayout
	xmlns:android="http://schemas.android.com/apk/res/android"
	xmlns:tools="http://chemas.android.com/tools"
	android:layout_width="match_parent"
	android:layout_height="match_parent"
	android:background="@color/tan_background"
	android:orientation="vertical"
	tools:context="com.example.android.miwok.MainActivity">

	<TextView
		android:id="@+id/numbers"
		style="@style/CategoryStyle"
		android:background="@color/category_numbers"
		android:text="Numbers"
		android:onClick="openNumberList" />

	<TextView
		android:id="@+id/family"
		style="@style/CategoryStyle"
		android:background="@color/category_family"
		android:text="Family Members" />

	<TextView
		android:id="@+id/colors"
		style="@style/CategoryStyle"
		android:background="@color/category_colors"
		android:text="Calors" />

	<TextView
		android:id="@+id/phrases"
		style="@style/CategoryStyle"
		android:background="@color/category_phrases"
		android:text="Phrases" />

</LinearLayout>

MainActivity.java

package com.example.android.miwok;

import android.conent.Intent;
import android.support.v7.app.AppCompactActivity;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends AppCompactActivity {

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

	public void openNumbersList(View view){
		//TODO: Write your code here!
		Intent i = new Intent(this, NumbersActivity.class);
		startActivity(i);
	}
}

android intent tutorial site
http://www.vogella.com/tutorials/AndroidIntent/article.html#usingintents_call

Read MainActivity.java

MainActivity
intent -> NumbersActivity, FamilyActivity, ColorsActivity, PhraseActivity

package com.example.android.miwok;

import android.support.v7.app.AppCompactActivity;
import android.os.Bundle;

public class MainActivity extends AppCompactActivity {

	@Override
	protected void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	}
}
<application
	android:allowBackup="true"
	android:icon="@mipmap/ic_launcher"
	android:label="Miwok"
	android:supportsRtl="true"
	android:theme="@style/AppTheme" >
	<activity android:name=".MainActivity" >
		<intent-filter>
			<action android:name="android.intent.action.MAIN" />

			<category android:name="android.intent.category.LAUNCHER" />
		</intent-filter>
	</activity>
	<activity android:name=".NumbersActivity" >
	</activity>
	<activity android:name=".FamilyActivity" >
	</activity>
	<activity android:name=".ColorsActivity" >
	</activity>
	<activity android:name=".PhrasesActivity" >
	</activity>
</application>

Navigate to other activities: intent

Miwok language

Miwok app to start off
app / src / main / res / layout / activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2016 The Android Open Source Project
     Licensed under the Apache License, Version 2.0 (the "License");
     you may not use this file except in compliance with the License.
     You may obtain a copy of the License at
          http://www.apache.org/licenses/LICENSE-2.0
     Unless required by applicable law or agreed to in writing, software
     distributed under the License is distributed on an "AS IS" BASIS,
     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     See the License for the specific language governing permissions and
     limitations under the License.
-->
<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:background="@color/tan_background"
    android:orientation="vertical"
    tools:context="com.example.android.miwok.MainActivity">

    <TextView
        android:id="@+id/numbers"
        style="@style/CategoryStyle"
        android:background="@color/category_numbers"
        android:text="@string/category_numbers" />

    <TextView
        android:id="@+id/family"
        style="@style/CategoryStyle"
        android:background="@color/category_family"
        android:text="@string/category_family" />

    <TextView
        android:id="@+id/colors"
        style="@style/CategoryStyle"
        android:background="@color/category_colors"
        android:text="@string/category_colors" />

    <TextView
        android:id="@+id/phrases"
        style="@style/CategoryStyle"
        android:background="@color/category_phrases"
        android:text="@string/category_phrases" />

</LinearLayout>
/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.example.android.miwok;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Set the content of the activity to use the activity_main.xml layout file
        setContentView(R.layout.activity_main);
    }
}

Linear Regression

what decision needs to be made?
what information do we need?

tickets per customer per week
past average predict future average
determine the right analytical approach

average number, number of employees, value of contract, industry

linear regression model
-number of employees and number of ticket
– y = mx + b (slope and y-intercept)

calculate a linear equation
y = 0.1833x – 11.055