public void decrement(View view){ quantity = quantity - 1; displayQuantity(quantity); } public void submitOrder(View view){ CheckBox = whippedCreamCheckBox = (CheckBox) findViewById(R.id.whipped_cream_checkbox); boolean hasWhippedCream = whippedCreamCheckBox.isChecked(); Log.v("MainActivity", "Has whipped cream; " + hasWhippedCream); int price = calculatePrice(); String priceMessage = createOrderSummary(price, hasWhippedCream); displayMessage(priceMEssage); } private int calculatePrice(){ return quantity * 5; } private String createOrderSummary(int price, boolean addWhippedCream){ String priceMessage = "Name: Lyla the Labyrinth"; priceMessage += "\nQuantity: " + quantity; priceMessage += "\nTotal: $" + price; priceMessage += "\nThank you!"; return priceMessage; }
java data types
java data types
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
boolean: true and false
public void decrement(View view){ quantity = quantity - 1; displayQuantity(quantity); } public void submitOrder(View view){ int price = calculatePrice(); log.v("MainActivity", "The price is " + price); String priceMessage = createOrderSummary(price); displayMessage(priceMessage); }
public void decrement(View view){ quantity = quantity - 1; displayQuantity(quantity); } public void submitOrder(View view){ CheckBox = whippedCreamCheckBox = (CheckBox) findViewById(R.id.whipped_cream_checkbox); int price = calculatePrice(); String priceMessage = createOrderSummary(price); displayMessage(priceMEssage); }
checkbox
public class MyActivity extends Activity { protected void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.content_layout_id); final CheckBox checkBox = (CheckBox) findViewById(R.id.checkbox_id); if (checkBox.isChecked()) { checkBox.setChecked(false); } } }
Add or remove parameter
public void submitOrder(View view){ int price = quantity * 5; String priceMessage = "Total: $" + price; priceMessage = priceMessage + "\nThank you!"; displayMessage(priceMessage); calculatePrice(quantity, 10); } private void calculatePrice(int quantity, int pricePerCup){ int price = quantity * pricePerCup; }
java keywords
abstract continue for new switch
assert*** default goto* package synchronized
boolean do if private this
break double implements protected throw
byte else import public throws
case enum**** instanceof return transient
catch extends int short try
char final interface static void
class finally long strictfp** volatile
const* float native super while
common android views
https://s3-us-west-1.amazonaws.com/udacity-content/PDFs/Common+Android+Views+Cheat+Sheet+(1).pdf
The stages
Step1: select the views
-TextView:2
-ImageView:0
-Button:3
Step2: position views
Use a LinearLayout because it is a column of views.
Step3: style views
unread emails
int unreadEmails = “10”; -> quotes should be string.
int unreadEmails = 10; -> should be camel case
intunreadEmails = 10; -> needed space and this one is definitely problem
integer number; computer recognize int
int int; variable name cannot be a java keyword
int integer;
String currentlyPlaying = “Pinocchio – Penguin Prison”;
string currentlyPlaying = “Fantasy – Alina Baraz & Galimatias”;
String currentlyPlaying = “Call Me Up – Chromeo”
String-name = “Katherine”
String “name” = “Kunal”;
String name on profile = “Lyla”;
int weekday = 5;
int weekend = 9;
int optimalHours = 7*8;
int actualHours = weekday;
actualHours = actualHours + weekend*2;
int solution = optimalHours – actualHours;
display(solution)
solution:-33
public class MainActivity extends AppCompactActivity{ public void voteDacicola(View view){ int dacicolaVotes = 0; dacicolaVotes = dacicolaVotes + 1; } public void votePepcity(View view){ int pepcityVote; pepcityVotes = pepcityVotes + 1; } public void showMeVotes(View view){ display(dacicolaVotes + "vs. " + pepcityVotes); } }
String concatenation
“I need” + “2 cups of coffee” + “on Monday”
package com.example.android.justjava; import android.os.Bundle; import android.support.v7.app.ActionBarActivity; import android.view.View; import android.widget.TextView; import java.text.NumberFormat; public class MainActivity extends ActionBarActivity { int quantity = 2; @Override protected void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void increment(View view){ quantity = quantity + 1; display(quantity); } public void decrement(View view){ quantity = quantity - 1; display(quantity); } public void submitOrder(View view){ int price = quantity * 5; String priceMessage = "That would be $" + price + "please."; priceMessage = priceMessage + "\nThank you!"; displayMessage(priceMessage); } /** * This method displays the given text on the screen. */ private void displayMessage(String message) { TextView priceTextView = (TextView) findViewById(R.id.price_text_view); priceTextView.setText(message); } private void display(int number){ TextView quantityTextView = (TextView) findViewById( R.id.quantity_text_view); quantityTextView.setText("" + number); } private void displayPrice(int number){ TextView priceTextView = (TextView) findViewById(R.id.price_text_view); priceTextView.setText(NumberFormat.getCurrencyInstance().format(number)); } }
<LinearLayout xmlns:android="http://schemas.android." xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical" android:paddingLeft="@dimen/activity_horizontal" android:paddingRight="@dimen/activity_horizontal" android:paddingTop="@dimen/activity_vertical" android:orientation="vertical" tools:context=".MainActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="16dp" android:text="Quantity" android:textAllCaps="true" /> <LinearLayout android:layout_width="mach_parent" android:layout_height="wrapcontent" android:orientation="horizontal"> <Button android:layout_height="48dp" android:layout_width="48dp" android:text="-" android:onClick="decrement" /> <TextView android:id="@+id/quantity_text_view" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingLeft="8dp" android:paddingRight="8dp" android:text="0" android:textSize="16sp" adnroid:textColor="@android:color/black" /> <Button android:layout_height="48dp" android:layout_width="48dp" android:text="+" android:onClick="increment" /> </LinearLayout> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="16dp" android:text="Price" android:textAllCaps="true" /> <TextView android:id="@+id/price_text_view" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="16dp" android:text="$0" android:textSize="16sp" adnroid:textColor="@android:color/black" /> <Button android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_marginTop="16dp" android:text="Order" android:onClick="submitOrder" /> </LinearLayout>
how to build layout
select views
・same as before(3 buttons, 4 textviews)
position views
・header followed by row of buttons/text, followed by header, text, button.
・can’t just use vertical LinearLayout, maybe RelativeLayout would work
style views
Nested viewgroups
https://gist.github.com/anonymous/256752a04db14a3947a8
public void submitOrder(View view){ String priceMessage = "Free"; displayMessage(priceMessage); }
variable name java
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/variables.html
java escape character
https://docs.oracle.com/javase/tutorial/java/data/characters.html
public void submitOrder(View view){ String priceMessage = "She sed \"1 dollar\""; displayMessage(priceMessage); }
Fix order button
package com.example.android.justjava; import android.os.Bundle; import android.support.v7.app.ActionBarActivity; import android.view.View; import android.widget.TextView; import java.text.NumberFormat; public class MainActivity extends ActionBarActivity { int quantity = 2; @Override protected void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void increment(View view){ quantity = quantity + 1; display(quantity); } public void decrement(View view){ quantity = quantity - 1; display(quantity); } public void submitOrder(View view){ displayPrice(quantity * 5); } private void display(int number){ TextView quantityTextView = (TextView) findViewById( R.id.quantity_text_view); quantityTextView.setText("" + number); } private void displayPrice(int number){ TextView priceTextView = (TextView) findViewById(R.id.price_text_view); priceTextView.setText(NumberFormat.getCurrencyInstance().format(number)); } }
<LinearLayout xmlns:android="http://schemas.android." xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical" android:paddingLeft="@dimen/activity_horizontal" android:paddingRight="@dimen/activity_horizontal" android:paddingTop="@dimen/activity_vertical" android:orientation="vertical" tools:context=".MainActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="16dp" android:text="Quantity" android:textAllCaps="true" /> <Button android:layout_height="48dp" android:layout_width="48dp" android:text="+" android:onClick="increment" /> <TextView android:id="@+id/quantity_text_view" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="0" android:textSize="16sp" adnroid:textColor="@android:color/black" /> <Button android:layout_height="48dp" android:layout_width="48dp" android:text="-" android:onClick="decrement" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="16dp" android:text="Price" android:textAllCaps="true" /> <TextView android:id="@+id/price_text_view" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="16dp" android:text="$0" android:textSize="16sp" adnroid:textColor="@android:color/black" /> <Button android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_marginTop="16dp" android:text="Order" android:onClick="submitOrder" /> </LinearLayout>
create and use variables
variable name java
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/variables.html
data type variable name = initial value
int numberOfCoffees = 2;
display(numberOfCoffees);
display(numberOfCoffees * 5);
DDMS stands for Dalvik Debug Monitor Server, and is a tool in Android to help you debug your app.
https://developer.android.com/studio/profile/ddms.html?utm_source=udacity&utm_medium=course&utm_campaign=android_basics
Debug, Crashes, Compile time error, Runtime error, System log, Stacktrace
debugging android studio
https://developer.android.com/studio/debug/index.html
int quantity = 2;
quantity = 3;
quantity = 4;
quantity = 5;
int quantity = 2;
quantity = quantity + 1;
Modify the button
Main Activity
package com.example.android.justjava; import android.os.Bundle; import android.support.v7.app.ActionBarActivity; import android.view.View; import android.widget.TextView; public class MainActivity extends ActionBarActivity { @Override protected void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void submitOrder(View view){ display(1); } private void display(int number){ TextView quantityTextView = (TextView) findViewById( R.id.quantity_text_view); quantityTextView.setText("" + number); } @Override public boolean onCreateOptionsMenu(Menu menu){ // Inflate the menu; this adds items to the action getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item){ int id = item.getItemId(); if ( id == R.id.action_settings){ } } }
getting paste error
1. read the error message
2. compare to working code
3. undo
4. ask for help
git hub gist
private void displayPrice(int number){ TextView priceTextView = (TextView) findViewById(R.id.price_text_view); priceTextView.setText(NumberFormat.getCurrencyInstance().format(number)); }