package com.example.android.miwok;
public class Word {
private String mDefaultTranslation;
private String mMiworkTranslation;
private int mImageResourceId;
public String getDefaultTranslation(){
return mDefaultTranslation;
}
public String getMiwokTranslation() { return mMiwokTranslation; }
public int getImageResourceId(){
return mImageResourceId;
}
}
Category: Android
Prioritize code change
strategry 1: do the riskiest part first (to demonstrate proof of concept)
strategry 2: implement what you can based on the available data, Use placeholder data
<ImageView android:width="wrap_content" android:height="wrap_content" android:src="@mipmap/ic_launcher" />
Density Buckets
-mdpi(medium) ~160dpi
-hdpi(high) ~240dpi
-xhdpi(extra-high) ~320dpi
-xxhdpi(extra-extra-high) ~480dpi
more pixel fit in the screen
getView method
package com.example.android.miwok;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
public class WordAdapter {
@Override
public View getView(int position, View conertView, ViewGroup parent){
return super.getView(position, converView, parent);
}
}
<?xml version="1.0" encoding="utf-8"?> <ListView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/list" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.android.miwok.NumbersActivity"/>
public class ColorsActivity extends AppCompactActivity {
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContextView(R.layout.word_list);
}
}
custom ArrayAdapter
package com.example.android.miwok;
public class Word {
private String mDefaultTranslation;
private String mMiwokTranslation;
public Word(String defaultTranslation, String miwokTranslation){
mDefaultTranslation = defaultTranslation;
mMiwokTranslation = miwokTranslation;
}
public String getDefaultTranslation(){
return mDefaultTranslation;
}
public String getMiwokTranslation(){
return mMiwokTranslation;
}
}
ArrayAdapter
https://developer.android.com/reference/android/widget/ArrayAdapter.html?utm_source=udacity&utm_medium=course&utm_campaign=android_basics
import androis.os.Bundle;
import android.support.v7.app.AppCompactActivity;
import android.widget.LinearLayout;
import android.widget.TextView;
import java.util.ArrayList;
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<Word> words = new ArrayList<Word>();
words.add(new Word("one", "lutti"));
words.add(new Word("two", "otiiko"));
words.add(new Word("three", "tolookosu"));
words.add(new Word("four", "oyyisa"));
words.add(new Word("five", "massokka"));
words.add(new Word("six", "temmokka"));
words.add(new Word("seven", "kenekaku"));
words.add(new Word("eight", "kawinta"));
words.add(new Word("nine", "wo'e"));
words.add(new Word("ten", "na'aacha"));
ArrayAdapter<Word> itemsAdapter = new ArrayAdapter<Word>(this, android.R.layout.list_item, words);
GridView listView = (GridView) findViewById(R.id.list);
listView.setAdapter(itemsAdapter);
}
}
View Recycling
Linear layout: warning memory is limited
ListView + Array adapter
Create more rows on-demand by reusing views in the scrap pile
Scroll to see more at the top of the list
ArrayAdapter<String> itemsAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, words); ListView listView = (ListView) findViewById(R.id.list); listView.setAdapter(itemsAdapter);
concrete class, abstract class, interface
Spinner(drop-down menu) -> ArrayAdapter
<?xml version="1.0" encoding="utf-8"?> <GridView xmlns:android="http://schemas.android.com/res/android" android:layout_width="match_parent" android:lyout_height="match_parent" android:paddingBottom="16dp" android:paddingLeft="16dp" android:paddingRight="16dp" android:paddingTop="16dp" android:numColumns="2" android:orientation="vertical" android:id="@+id/list" tools:context="com.example.android.miwok.NumbersActivity"> </GridView>
Loop
while loop
setup counter variable, condition, update counter variable
int index = 0;
while(index < 4){
Log.v("NumbersActivity",
"Index: " + index + "
value: " + words[index]);
index++;
}
for loop
for(int index = 0; index < words.size(); index++){
log.v("NumbersActivity",
"Index: " + index +
"Value: " + words.get(index));
}
import androis.os.Bundle;
import android.support.v7.app.AppCompactActivity;
import android.widget.LinearLayout;
import android.widget.TextView;
import java.util.ArrayList;
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 words = new ArrayList();
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");
LinearLayout rootView = (LinearLayout)findViewById(R.id.rootView);
for (int index = 0; index < words.size(); index++)
{
TextView wordView = new TextView(this);
wordView.setText(words.get(index));
rootView.addView(wordView);
index++;
}
}
}
While Loop
while (condition) {
Instruction
Update counter variable
}
int count = 0;
while(count < 3){
playSound();
count = count + 1;
}
[java]
int index = 0;
while(index < 4){
Log.v("NumbersActivity",
"Index: " + index + "
value: " + words[index]);
index++;
}
[/java]
NumbersActivity.java
[java]
import androis.os.Bundle;
import android.support.v7.app.AppCompactActivity;
import android.widget.LinearLayout;
import android.widget.TextView;
import java.util.ArrayList;
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
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”);
LinearLayout rootView = (LinearLayout)findViewById(R.id.rootView);
int index = 0;
while (index < words.size()){
TextView wordView = new TextView(this);
wordView.setText(words.get(index));
rootView.addView(wordView);
index++;
}
}
}[/java]
for (Setup counter variable; Condition; Update counter variable){
Instructions
}
Add Text Views
Add text view to the Layout
activity_numbers.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/res/android" android:layout_width="match_parent" android:lyout_height="match_parent" android:paddingBottom="16dp" android:paddingLeft="16dp" android:paddingRight="16dp" android:paddingTop="16dp" android:orientation="vertical" android:id="@+id/rootView" tools:context="com.example.android.miwok.NumbersActivity"> </LinearLayout>
android:textView class
https://developer.android.com/reference/android/widget/TextView.html
/.../
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");
LinearLayout rootView = (LinearLayout)findViewById(R.id.rootView);
TextView wordView = new TextView(this);
wordView.setText(words.get(0));
rootView.addView(wordView);
TextView wordView2 = new TextView(this);
wordView2.setText(words.get(1));
rootView.addView(wordView2);
TextView wordView3 = new TextView(this);
wordView3.setText(words.get(2));
rootView.addView(wordView3);
}
}
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]);
}
}