Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
improved app
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:text="Quantity"
android:textAllCaps="true"/>
<LinearLayout
android:orientation="horizontal"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:gravity="center"
android:layout_width="48dp"
android:layout_height="48dp"
android:onClick="subtract"
android:text="-"
/>
<TextView
android:id="@+id/current_guess"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_gravity="center_vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@android:color/black"
android:textSize="16sp"/>
<Button
android:gravity="center"
android:layout_width="48dp"
android:layout_height="48dp"
android:onClick="add"
android:text="+"/>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
android:text="Price"
android:textAllCaps="true"/>
<TextView
android:id="@+id/priceTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
android:text="$0"
android:textColor="@android:color/black"
android:textSize="16sp"/>
</LinearLayout>
package com.example.yitzi.mathapp;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
import java.text.NumberFormat;
public class MainActivity extends AppCompatActivity {
int numberOfCoffees = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
display(numberOfCoffees);
}
public void subtract(View view) {
if (numberOfCoffees > 0) {
numberOfCoffees = numberOfCoffees - 1;
display(numberOfCoffees);
displayPrice(numberOfCoffees * 5);
}
}
public void add(View view){
numberOfCoffees = numberOfCoffees + 1;
display(numberOfCoffees);
displayPrice(numberOfCoffees * 5);
}
/**
* This method displays the given quantity value on the screen.
*/
private void display(int number) {
TextView quantityTextView = findViewById(R.id.current_guess);
quantityTextView.setText("" + number);
}
private void displayPrice(int number) {
TextView priceTextView = findViewById(R.id.priceTextView);
priceTextView.setText(NumberFormat.getCurrencyInstance().format(number));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment