Music Player App

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">

	<Button
		android:id="@+id/play_button"
		android:text="Play"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content" />

	<Button
		android:id="@+id/pause_button"
		android:text="Pause"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content" />

</LinearLayout>

MainActivity.java

package com.example.android.musicplayer;

import

public class MainActivity extends AppCompactActivity {

	private MediaPlayer mediaPlayer;


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

		mediaPlayer = MediaPlayer.create(this, R.raw.android_trailer);

		Button playButton = (Button)findViewById(R.id.play_button);

		playButton.setOnClickListener(new View.OnClickListener(){
			@Override
			public void onClick(View v){
				mediaPlayer.start();
			}
		});

		Button pauseButton = (Button)findViewById(R.id.play_button);

		playButton.setOnClickListener(new View.OnClickListener(){
			@Override
			public void onClick(View v){
				mediaPlayer.pause();
			}
		});
	}
}

Plan to build the audio feature

android media player
https://developer.android.com/guide/topics/media/mediaplayer.html

Which step to accomplish
– modify list item layout to include a play button
– handle clicking on a list item to play an audio file
– add in all audio files
– modify word class to store audio resource ID
– play correct audio file per word
– visual plish

use media player api in sample app

Providing Constructors

public Bicycle(int startGadence, int startSpeed, int startGear){
	gear = startGear;
	cadence = startCadence;
	speed = startSpeed;
}
<?xml version="1.0" encoding="utf-8"?>
<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="wrap_content"
	android:orientation="horizontal"
	android:padding="16dp">

	<ImageView
		android:id="@+id/image"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:src="@mipmap/ic_launcher" />

	<LinearLayout
		android:id="@+id/text_container"
		android:layout_width="match_parent"
		android:layout_height="match_parent"
		android:orientation="vertical"
		android:paddingLeft="16dp">

		<TextView
			android:id="@+id/miwok_text_view"
			android:layout_width="match_parent"
			android:layout_height="wrap_content"
			tools:text="lutti" />

		<TextView
			android:id="@+id/default_text_view"
			android:layout_width="match_parent"
			android:layout_height="wrap_content"
			tools:text="one" />
	</LinearLayout>
</LinearLayout>

Change view visibility
textView.setVisibility(View.VISIBLE);
textView.setVisibility(View.INVISIBLE);
textView.setVisibility(View.GONE);

Modify Word Class

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;
	}
}

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 = 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);
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 }