Commit c60b7ee8 authored by Kaelan Willauer's avatar Kaelan Willauer

homework 2

parent d9d74709
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project version="4"> <project version="4">
<component name="GradleMigrationSettings" migrationVersion="1" />
<component name="GradleSettings"> <component name="GradleSettings">
<option name="linkedExternalProjectsSettings"> <option name="linkedExternalProjectsSettings">
<GradleProjectSettings> <GradleProjectSettings>
......
...@@ -3,12 +3,18 @@ ...@@ -3,12 +3,18 @@
package com.zybooks.pizzaparty; package com.zybooks.pizzaparty;
import android.os.Bundle; import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View; import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText; import android.widget.EditText;
import android.widget.RadioGroup; import android.widget.RadioGroup;
import android.widget.TextView; import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatActivity;
import android.util.Log; import android.util.Log;
import android.widget.Spinner;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity { public class MainActivity extends AppCompatActivity {
...@@ -16,7 +22,7 @@ public class MainActivity extends AppCompatActivity { ...@@ -16,7 +22,7 @@ public class MainActivity extends AppCompatActivity {
private EditText mNumAttendEditText; private EditText mNumAttendEditText;
private TextView mNumPizzasTextView; private TextView mNumPizzasTextView;
private RadioGroup mHowHungryRadioGroup; private Spinner mHowHungrySpinner;
private final static String TAG = "MainActivity"; private final static String TAG = "MainActivity";
...@@ -30,7 +36,44 @@ public class MainActivity extends AppCompatActivity { ...@@ -30,7 +36,44 @@ public class MainActivity extends AppCompatActivity {
// Assign the widgets to fields // Assign the widgets to fields
mNumAttendEditText = findViewById(R.id.attendEditText); mNumAttendEditText = findViewById(R.id.attendEditText);
mNumPizzasTextView = findViewById(R.id.answerTextView); mNumPizzasTextView = findViewById(R.id.answerTextView);
mHowHungryRadioGroup = findViewById(R.id.hungryRadioGroup); mHowHungrySpinner = findViewById(R.id.hunger_spinner);
// Watch for changes to number attending
mNumAttendEditText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
mNumPizzasTextView.setText("");
}
});
// set up the hunger spinner
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.hunger_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mHowHungrySpinner.setAdapter(adapter);
mHowHungrySpinner.setSelection(0,false);
mHowHungrySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String item = (String)parent.getItemAtPosition(position);
Toast.makeText(MainActivity.this, item, Toast.LENGTH_SHORT).show();
mNumPizzasTextView.setText("");
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
} }
public void calculateClick(View view) { public void calculateClick(View view) {
...@@ -40,18 +83,17 @@ public class MainActivity extends AppCompatActivity { ...@@ -40,18 +83,17 @@ public class MainActivity extends AppCompatActivity {
try { try {
String numAttendStr = mNumAttendEditText.getText().toString(); String numAttendStr = mNumAttendEditText.getText().toString();
numAttend = Integer.parseInt(numAttendStr); numAttend = Integer.parseInt(numAttendStr);
} } catch (NumberFormatException ex) {
catch (NumberFormatException ex) {
numAttend = 0; numAttend = 0;
} }
// Get hunger level selection // Get hunger level selection
int checkedId = mHowHungryRadioGroup.getCheckedRadioButtonId(); int checkedId = mHowHungrySpinner.getId(); // I didn't know what to do after updating to mHowHungrySpinner
PizzaCalculator.HungerLevel hungerLevel = PizzaCalculator.HungerLevel.RAVENOUS; PizzaCalculator.HungerLevel hungerLevel = PizzaCalculator.HungerLevel.RAVENOUS;
if (checkedId == R.id.lightRadioButton) { if (checkedId == R.id.hunger_spinner) {
hungerLevel = PizzaCalculator.HungerLevel.LIGHT; hungerLevel = PizzaCalculator.HungerLevel.LIGHT;
} }
else if (checkedId == R.id.mediumRadioButton) { else if (checkedId == R.id.hunger_spinner) {
hungerLevel = PizzaCalculator.HungerLevel.MEDIUM; hungerLevel = PizzaCalculator.HungerLevel.MEDIUM;
} }
......
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" <androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main" android:id="@+id/activity_main"
android:background="@drawable/pizza_background"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="16dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="16dp"
tools:context="com.zybooks.pizzaparty.MainActivity"> tools:context="com.zybooks.pizzaparty.MainActivity">
<TextView <TextView
android:id="@+id/numberLabel"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:labelFor="@id/attendEditText"
android:text="@string/number_of_people" android:text="@string/number_of_people"
android:textSize="@dimen/label_text_size" android:textSize="@dimen/label_text_size"
android:labelFor="@id/attendEditText" /> app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText <EditText
android:id="@+id/attendEditText" android:id="@+id/attendEditText"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:inputType="number" android:inputType="number"
android:layout_marginLeft="5dp"
android:ems="5" android:ems="5"
android:importantForAutofill="no" android:importantForAutofill="no"
android:hint="@string/_10" /> android:hint="@string/_10"
app:layout_constraintLeft_toRightOf="@id/numberLabel"
app:layout_constraintBaseline_toBaselineOf="@id/numberLabel"/>
<TextView <TextView
android:id="@+id/hungryLabel"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginTop="20dp" android:layout_marginTop="25dp"
android:text="@string/how_hungry" android:text="@string/how_hungry"
android:textSize="24sp" android:textSize="24sp"
android:labelFor="@id/hungryRadioGroup" /> app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@id/numberLabel"
android:labelFor="@id/hunger_spinner" />
<RadioGroup <Spinner
android:id="@+id/hungryRadioGroup" android:id="@+id/hunger_spinner"
android:layout_width="fill_parent" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:orientation="vertical"> app:layout_constraintLeft_toRightOf="@id/hungryLabel"
<RadioButton app:layout_constraintBaseline_toBaselineOf="@id/hungryLabel"/>
android:id="@+id/lightRadioButton"
android:text="@string/light"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<RadioButton
android:id="@+id/mediumRadioButton"
android:text="@string/medium"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true" />
<RadioButton
android:id="@+id/ravenousRadioButton"
android:text="@string/ravenous"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RadioGroup>
<TextView <TextView
android:id="@+id/answerTextView" android:id="@+id/answerTextView"
...@@ -65,7 +56,10 @@ ...@@ -65,7 +56,10 @@
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginTop="20dp" android:layout_marginTop="20dp"
android:textSize="24sp"/> android:textSize="24sp"
app:layout_constraintLeft_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/hunger_spinner"
android:layout_marginLeft="5dp"/>
<Button <Button
android:id="@+id/calcButton" android:id="@+id/calcButton"
...@@ -73,5 +67,8 @@ ...@@ -73,5 +67,8 @@
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginTop="20dp" android:layout_marginTop="20dp"
android:onClick="calculateClick" /> android:onClick="calculateClick"
</LinearLayout> app:layout_constraintLeft_toLeftOf="parent"
\ No newline at end of file app:layout_constraintTop_toBottomOf="@id/answerTextView"
style="@style/CalcButton"/>
</androidx.constraintlayout.widget.ConstraintLayout>
\ No newline at end of file
...@@ -8,4 +8,5 @@ ...@@ -8,4 +8,5 @@
<color name="black">#FF000000</color> <color name="black">#FF000000</color>
<color name="white">#FFFFFFFF</color> <color name="white">#FFFFFFFF</color>
<color name="my_green">#11BB11</color> <color name="my_green">#11BB11</color>
<color name="gray">#AAA</color>
</resources> </resources>
\ No newline at end of file
...@@ -8,4 +8,9 @@ ...@@ -8,4 +8,9 @@
<string name="ravenous">Ravenous</string> <string name="ravenous">Ravenous</string>
<string name="total_pizzas">Total pizzas: %d</string> <string name="total_pizzas">Total pizzas: %d</string>
<string name="calculate">Calculate</string> <string name="calculate">Calculate</string>
<string-array name="hunger_array">
<item>Light</item>
<item>Medium</item>
<item>Ravenous</item>
</string-array>
</resources> </resources>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="CalcButton">
<item name="android:textSize">24sp</item>
<item name="android:textColor">@color/gray</item>
</style>
</resources>
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment