Skip to content

Instantly share code, notes, and snippets.

@udacityandroid
Last active December 22, 2023 08:49
Show Gist options
  • Star 72 You must be signed in to star a gist
  • Fork 48 You must be signed in to fork a gist
  • Save udacityandroid/609dbb5370ba0665955a to your computer and use it in GitHub Desktop.
Save udacityandroid/609dbb5370ba0665955a to your computer and use it in GitHub Desktop.
Android for Beginners : Add the Chocolate Topping Checkbox Solution Java
package com.example.android.justjava;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;
/**
* This app displays an order form to order coffee.
*/
public class MainActivity extends AppCompatActivity {
int quantity = 2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
/**
* This method is called when the plus button is clicked.
*/
public void increment(View view) {
quantity = quantity + 1;
displayQuantity(quantity);
}
/**
* This method is called when the minus button is clicked.
*/
public void decrement(View view) {
quantity = quantity - 1;
displayQuantity(quantity);
}
/**
* This method is called when the order button is clicked.
*/
public void submitOrder(View view) {
// Figure out if the user wants whipped cream topping
CheckBox whippedCreamCheckBox = (CheckBox) findViewById(R.id.whipped_cream_checkbox);
boolean hasWhippedCream = whippedCreamCheckBox.isChecked();
// Figure out if the user wants chocolate topping
CheckBox chocolateCheckBox = (CheckBox) findViewById(R.id.chocolate_checkbox);
boolean hasChocolate = chocolateCheckBox.isChecked();
// Calculate the price
int price = calculatePrice();
// Display the order summary on the screen
String message = createOrderSummary(price, hasWhippedCream, hasChocolate);
displayMessage(message);
}
/**
* Calculates the price of the order.
*
* @return total price
*/
private int calculatePrice() {
return quantity * 5;
}
/**
* Create summary of the order.
*
* @param price of the order
* @param addWhippedCream is whether or not to add whipped cream to the coffee
* @param addChocolate is whether or not to add chocolate to the coffee
* @return text summary
*/
private String createOrderSummary(int price, boolean addWhippedCream, boolean addChocolate) {
String priceMessage = "Name: Lyla the Labyrinth";
priceMessage += "\nAdd whipped cream? " + addWhippedCream;
priceMessage += "\nAdd chocolate? " + addChocolate;
priceMessage += "\nQuantity: " + quantity;
priceMessage += "\nTotal: $" + price;
priceMessage += "\nThank you!";
return priceMessage;
}
/**
* This method displays the given quantity value on the screen.
*/
private void displayQuantity(int numberOfCoffees) {
TextView quantityTextView = (TextView) findViewById(
R.id.quantity_text_view);
quantityTextView.setText("" + numberOfCoffees);
}
/**
* This method displays the given text on the screen.
*/
private void displayMessage(String message) {
TextView orderSummaryTextView = (TextView) findViewById(R.id.order_summary_text_view);
orderSummaryTextView.setText(message);
}
}
@OldWolf55
Copy link

And this is the XML code

<LinearLayout

xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="16dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="16dp">



<TextView

    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="16dp"
    android:text="Toppings"
    android:textAllCaps="true"
    android:textSize="16sp" />

<CheckBox
    android:id="@+id/whipped_cream_checkbox"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingLeft="24dp"
    android:text="Whipped cream"
    android:textSize="16sp"
    android:onClick="onCheckboxClicked"/>

<CheckBox
    android:id="@+id/chocolate_checkbox"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingLeft="24dp"
    android:text="Chocolate"
    android:textSize="16sp"
    android:onClick="onChocolateClicked"
    />

<TextView

    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="16dp"
    android:layout_marginTop="16dp"
    android:text="Quantity"
    android:textAllCaps="true"
    android:textSize="16sp" />

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"

    >

    <Button
        android:layout_width="48dp"
        android:layout_height="48dp"
        android:onClick="submitDecrement"
        android:text="-"
        android:textSize="16sp" />


    <TextView
        android:id="@+id/quantity_text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:text="0"
        android:textColor="@android:color/black"
        android:textSize="16sp" />

    <Button
        android:layout_width="48dp"
        android:layout_height="48dp"
        android:onClick="submitIncrement"
        android:text="+"
        android:textSize="16sp" />


</LinearLayout>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="16dp"
    android:text="Order Summary"
    android:textAllCaps="true"
    android:textSize="16sp" />

<TextView
    android:id="@+id/order_summary_text_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="16dp"
    android:text="$0"
    android:textColor="@android:color/black"
    android:textSize="16sp" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="16dp"
    android:onClick="submitOrder"
    android:text="Order"
    android:textSize="15sp" />

@mixspark
Copy link

android:onClick="onCheckboxClicked"
android:onClick="onChocolateClicked"

i believe we do not need them my friend oldwolf55

@Wreia
Copy link

Wreia commented Jan 31, 2018

Android Studio shows me this Error: "Error:(49, 9) error: cannot find symbol class CheckBox" How could I solve this problem?

package com.example.android.justjava;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import java.text.NumberFormat;

/**

  • This app displays an order form to order coffee.
    /
    public class MainActivity extends Activity {
    int quantity = 0;
    @OverRide
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    }
    /
    *

    • Calculates the price of the order.
    • @param quantity is the number of cups of coffee ordered
      */
      private void calculatePrice(int quantity) {
      int price = quantity * 5;
      }

    /**

    • This method is called when the order button is clicked.
      /
      public void increment(View view) {
      quantity = quantity + 1;
      displayQuantity(quantity);
      }
      private String createOrderSummary(int price, boolean hasWhippedCream, boolean hasChocolate) {
      String priceMessage = "Name: Wreia" ;
      priceMessage += priceMessage + "\nAdd Whipped Cream? " + hasWhippedCream;
      priceMessage += priceMessage + "\nAdd Chocolate? " + hasChocolate;
      priceMessage += priceMessage + "\nQuantity: " + quantity;
      priceMessage += priceMessage + "\nTotal: $" + price;
      priceMessage += priceMessage + "\n Thank you!";
      return priceMessage;
      }
      private int calculatePrice() {
      int price = quantity * 5;
      return price;
      }
      public void submitOrder(View view) {
      CheckBox chocolateCheckBox = (CheckBox) findViewById(R.id.chocolate_checkbox);
      CheckBox whippedCreamCheckBox = (CheckBox) findViewById(R.id.whipped_cream_checkbox);
      boolean hasChocolate = chocolateCheckBox.isChecked();
      boolean hasWhippedCream = whippedCreamCheckBox.isChecked();
      int price = calculatePrice();
      String priceMessage = createOrderSummary(price, hasWhippedCream, hasChocolate);
      displayMessage(priceMessage);
      }
      /
      *
    • This method is called when the order button is clicked.
      */
      public void decrement(View view) {
      quantity = quantity - 1;
      displayQuantity(quantity);
      }

    /**

    • This method displays the given quantity value on the screen.
      /
      private void displayQuantity(int number) {
      TextView quantityTextView = (TextView) findViewById(R.id.quantity_text_view);
      quantityTextView.setText("" + number);
      }
      /
      *
    • This method displays the given text on the screen.
      */
      private void displayMessage(String message) {
      TextView orderSummaryTextView = (TextView) findViewById(R.id.order_summary_text_view);
      orderSummaryTextView.setText(message);
      }
      }

@Maxwe1
Copy link

Maxwe1 commented Feb 8, 2018

All good here.

@yassalah
Copy link

Done.. perfect work.

@wbadry
Copy link

wbadry commented Feb 21, 2018

This is my version with notification

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/holo_green_dark">

    <LinearLayout xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"

        android:orientation="vertical"
        tools:context="com.example.android.justjava.MainActivity">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="16dp"
            android:layout_marginTop="16dp"
            android:paddingBottom="16dp"
            android:text="TOPPINGS"
            android:textColor="@android:color/white" />

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <CheckBox
                android:id="@+id/whipped_checkbox"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginBottom="16dp"
                android:layout_marginLeft="16dp"
                android:buttonTint="@android:color/white"
                android:paddingLeft="24dp"
                android:text="Whipped Cream"
                android:textColor="@android:color/white"
                android:textSize="24sp" />

            <CheckBox
                android:id="@+id/choco_checkbox"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginBottom="16dp"
                android:layout_marginLeft="16dp"
                android:buttonTint="@android:color/white"
                android:paddingLeft="24dp"
                android:text="Chocolate"
                android:textColor="@android:color/white"
                android:textSize="24sp" />
        </LinearLayout>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="16dp"
            android:paddingBottom="16dp"
            android:text="QUANTITY"
            android:textColor="@android:color/white" />

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <Button
                android:layout_width="48dp"
                android:layout_height="48dp"
                android:layout_marginLeft="16dp"
                android:background="@android:color/white"
                android:onClick="decrement"
                android:text="-" />

            <TextView
                android:id="@+id/quantity_text_view"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"

                android:paddingBottom="16dp"
                android:paddingLeft="8dp"
                android:paddingRight="8dp"
                android:paddingTop="16dp"
                android:text="2"
                android:textColor="@android:color/white"
                android:textSize="16sp" />

            <Button
                android:layout_width="48dp"
                android:layout_height="48dp"
                android:background="@android:color/white"
                android:onClick="increment"
                android:text="+" />

        </LinearLayout>


        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="16dp"
            android:paddingTop="16dp"
            android:text="ORDER SUMMARY"
            android:textColor="@android:color/white" />

        <TextView
            android:id="@+id/ordrer_summary__text_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="16dp"
            android:paddingBottom="16dp"
            android:paddingTop="16dp"
            android:text="$10"

            android:textColor="@android:color/white"
            android:textSize="16sp" />


        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="16dp"
            android:background="@android:color/white"
            android:onClick="submitOrder"
            android:text="ORDER" />

    </LinearLayout>
</ScrollView>
package com.example.android.justjava;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

    int quantity = 2, pricePerCup = 5;


    /**
     * This app displays an order form to order coffee.
     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        View des = new TextView(getApplicationContext());
        des.setVisibility(View.GONE);
    }

    /**
     * This method is called when the order button is clicked.
     */
    public void submitOrder(View view) {
        createOrderSummary();
        Toast toast = Toast.makeText(getApplicationContext(), "Thank You!", Toast.LENGTH_SHORT);
        toast.show();
    }

    /**
     * Calculates the price of the order.
     *
     * @param quantity    is the number of cups of coffee ordered
     * @param pricePerCup is the price of the cup
     */
    private int calculatePrice(int quantity, int pricePerCup) {
        return quantity * pricePerCup;
    }

    /**
     * Creates an order summary
     */
    private void createOrderSummary() {

        CheckBox isWhipped = findViewById(R.id.whipped_checkbox);
        CheckBox isChocolate = findViewById(R.id.choco_checkbox);

        int totalPrice = calculatePrice(quantity, pricePerCup);
        String report = "Name : Waleed El-Badry" + "\n" +
                "Add Whipped Cream? " + isWhipped.isChecked() + "\n" +
                "Add Chocolate? " + isChocolate.isChecked() + "\n" +
                "Quantity : " + quantity + "\n" +
                "Price : $" + totalPrice;

        TextView reportTextView = (TextView) findViewById(R.id.ordrer_summary__text_view);
        reportTextView.setText("" + report);

    }

    /**
     * This method is called when increment button is clicked
     */
    public void decrement(View view) {
        if (quantity <= 0)
            quantity = 0;
        else
            quantity -= 1;
        displayMessage(quantity);
    }

    /**
     * This method is called when decrement button is clicked
     */
    public void increment(View view) {
        quantity += 1;
        displayMessage(quantity);

    }


    /**
     * This method displays the given quantity value on the screen.
     */
    private void displayMessage(int value) {
        TextView quantityTextView = findViewById(R.id.quantity_text_view);
        quantityTextView.setText("" + value);
    }

    /**
     * THis method displays the given price value on the screen
     */
    private void displayPrice(int number) {
        String price = "Total of $" + number + "\n" + "Thanks!";
        displayMessage(price);
    }

    /**
     * Testing string method
     */
    private void displayMessage(String msg) {
        TextView msgTextView = findViewById(R.id.ordrer_summary__text_view);
        msgTextView.setText(msg);
    }

}

screenshot_1519246084

@Alimamasy
Copy link

Thank for providing the whole Java file

@abdahma01
Copy link

2

@AbdooRaf3
Copy link

package com.example.android.justjava;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;

/**

  • This app displays an order form to order coffee.
    */
    public class MainActivity extends AppCompatActivity {

    int quantity_value;

    @OverRide
    protected void onCreate ( Bundle savedInstanceState ) {
    super.onCreate ( savedInstanceState );
    setContentView ( R.layout.activity_main );
    display_quantity ( quantity_value );
    }

    public void submit_Order ( View view ) {
    calculatePrise ();
    displayMessage ( createOrderSummary () );
    }

    private void display_quantity ( int number ) {
    final TextView quantityTextView = findViewById ( R.id.quantity_text_view );
    String quantity_text_view = "" + number;
    quantityTextView.setText ( quantity_text_view );
    }

    public void increment ( View view ) {
    quantity_value = quantity_value + 1;
    display_quantity ( quantity_value );
    }

    public void decrement ( View view ) {
    quantity_value = quantity_value - 1;
    display_quantity ( quantity_value );
    }

    private int calculatePrise () {
    return quantity_value * 5;
    }

    private void displayMessage ( String message ) {
    TextView orderSummaryTextView = findViewById ( R.id.order_summary_text_view );
    orderSummaryTextView.setText ( message );
    }

    private String createOrderSummary () {
    CheckBox whipped_cream_CheckBox = findViewById ( R.id.whipped_cream_CheckBox );
    final boolean checked_cream = whipped_cream_CheckBox.isChecked ();

     CheckBox chocolate_CheckBox = findViewById ( R.id.chocolate_CheckBox );
     final boolean checked_chocolate = chocolate_CheckBox.isChecked ();
    
     String summaryText = "Name: Sama Soft";
     summaryText += "\nAdd Whipped Cream: " + checked_cream;
     summaryText += "\nAdd chocolate: " + checked_chocolate;
     summaryText += "\nQuantity: " + quantity_value;
     summaryText += "\nTotal Price: $" + calculatePrise ();
     summaryText += "\nThank you!";
     return summaryText;
    

    }
    }

@AbdooRaf3
Copy link

<android.support.constraint.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"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.android.justjava.MainActivity">


    <TextView
        android:id="@+id/text_view_topping"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="16dp"
        android:text="@string/topping"
        android:textAlignment="center"
        android:textAllCaps="true"
        android:textColor="@android:color/black"
        android:textSize="16sp" />

    <CheckBox
        android:id="@+id/whipped_cream_CheckBox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="16dp"
        android:text="@string/whipped_cream"
        android:textSize="16sp" />

    <CheckBox
        android:id="@+id/chocolate_CheckBox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="16dp"
        android:text="@string/chocolate"
        android:textSize="16sp" />


    <TextView
        android:id="@+id/text_view_quantity"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="16dp"
        android:text="@string/quantity"
        android:textAlignment="center"
        android:textAllCaps="true"
        android:textColor="@android:color/black"
        android:textSize="16sp" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <Button
            android:id="@+id/button_increment"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="increment"
            android:padding="16dp"
            android:text="@string/increment"
            tools:ignore="OnClick"
            tools:layout_editor_absoluteX="0dp"
            tools:layout_editor_absoluteY="168dp" />

        <TextView
            android:id="@+id/quantity_text_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:padding="16dp"
            android:text="@string/_0"
            android:textAlignment="center"
            android:textColor="#000000"
            android:textSize="16sp" />

        <Button
            android:id="@+id/button_decrement"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="decrement"
            android:text="@string/decrement"
            tools:ignore="OnClick" />
    </LinearLayout>

    <TextView
        android:id="@+id/price_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="16dp"
        android:text="@string/order_summary"
        android:textAlignment="center"
        android:textAllCaps="true"
        android:textColor="@android:color/black"
        android:textSize="16sp" />

    <TextView
        android:id="@+id/order_summary_text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="16dp"
        android:text="@string/_0_00"
        android:textColor="#000000"
        android:textSize="16sp" />

    <Button
        android:id="@+id/button_order"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="submit_Order"
        android:padding="16dp"
        android:text="@string/order"
        tools:ignore="OnClick" />

</LinearLayout>

@kenehiz
Copy link

kenehiz commented Mar 2, 2018

My android studio always flag these words red saying "unable to resolve." "whippedCreamCheckBox;" and "chocolateCheckBox;" under sumbmitOrder (View view). All I did is to add these lines below to the "public class MakingAppInteractive extends AppCompatActivity" and problem solved.
private CheckedTextView whippedCreamCheckBox;
private CheckedTextView chocolateCheckBox;

I just hope it wont be a problem in the future!

@kenehiz
Copy link

kenehiz commented Mar 2, 2018

now there is a problem because the app just crahed

@badrddinb
Copy link

Thanks

@louayeldin
Copy link

Done

@ahmedalfalahi
Copy link

thanks alot

@teebonez28
Copy link

How come when I click on the whipped cream, the order summary says true for both whipped cream and chocolate and when I click on just chocolate, both are false?
package com.example.android.justjava;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;

/**

  • This app displays an order form to order coffee.
    */
    public class MainActivity extends AppCompatActivity {

    int quantity = 2;

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

    /**

    • This method is called when the plus button is clicked.
      */
      public void increment(View view) {
      quantity = quantity + 1;
      displayQuantity(quantity);
      }

    /**

    • This method is called when the minus button is clicked.
      */
      public void decrement(View view) {
      quantity = quantity - 1;
      displayQuantity(quantity);
      }

    /**

    • This method is called when the order button is clicked.
      */
      public void submitOrder(View view) {
      // Figure out if the user wants whipped cream topping
      CheckBox whippedCreamCheckbox = (CheckBox) findViewById(R.id.whipped_cream_checkbox);
      boolean hasWhippedCream = whippedCreamCheckbox.isChecked();

      // Figure out if the user wants chocolate topping
      CheckBox chocolateCheckbox = (CheckBox) findViewById(R.id.chocolate_checkbox);
      boolean hasChocolate = chocolateCheckbox.isChecked();

      // Calculate the price
      int price = calculatePrice();

      // Display the order summary on the screen
      String priceMessage = createOrderSummary(price, hasWhippedCream, hasChocolate);
      displayMessage(priceMessage);

    }

    /**

    • Calculates the price of the order.

    • @return total price
      */
      private int calculatePrice() {

      return quantity * 5;
      }

    /**

    • Create summary of the order.
    • @param price of the order
    • @param addWhippedCream is whether or not the user wants whipped cream topping
    • @param addChocolate is whether or not the user wants chocolate topping
    • @return text summary
      /
      private String createOrderSummary(int price, boolean addWhippedCream, boolean addChocolate) {
      String priceMessage = "Name: Cristina";
      priceMessage += "\nAdd whipped cream? " + addWhippedCream;
      priceMessage += "\nAdd chocolate? " + addWhippedCream;
      priceMessage += "\nQuantity: " + quantity;
      priceMessage += "\nTotal: $" + price;
      priceMessage += "\nThank you!";
      return priceMessage;
      }
      /
      *
    • This method displays the given quantity value on the screen;
      */
      private void displayQuantity(int numberOfCoffees) {
      TextView quantityTextView = (TextView) findViewById(R.id.quantity_text_view);
      quantityTextView.setText("" + numberOfCoffees);
      }

    /**

    • This method displays the given text on the screen.
      */
      private void displayMessage(String message) {
      TextView orderSummaryTextView = (TextView) findViewById(R.id.order_summary_text_view);
      orderSummaryTextView.setText(message);
      }

}

@Chendricks5
Copy link

<LinearLayout xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"

    android:orientation="vertical"
    tools:context="com.example.android.justjava.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="16dp"
        android:paddingBottom="16dp"
        android:text="TOPPINGS"
        android:textColor="@android:color/white" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <CheckBox
            android:id="@+id/whipped_checkbox"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="16dp"
            android:layout_marginLeft="16dp"
            android:buttonTint="@android:color/white"
            android:paddingLeft="24dp"
            android:text="Whipped Cream"
            android:textColor="@android:color/white"
            android:textSize="24sp" />

        <CheckBox
            android:id="@+id/choco_checkbox"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="16dp"
            android:layout_marginLeft="16dp"
            android:buttonTint="@android:color/white"
            android:paddingLeft="24dp"
            android:text="Chocolate"
            android:textColor="@android:color/white"
            android:textSize="24sp" />
    </LinearLayout>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="16dp"
        android:paddingBottom="16dp"
        android:text="QUANTITY"
        android:textColor="@android:color/white" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <Button
            android:layout_width="48dp"
            android:layout_height="48dp"
            android:layout_marginLeft="16dp"
            android:background="@android:color/black"
            android:onClick="decrement"
            android:text="-" />

        <TextView
            android:id="@+id/quantity_text_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"

            android:paddingBottom="16dp"
            android:paddingLeft="8dp"
            android:paddingRight="8dp"
            android:paddingTop="16dp"
            android:text="2"
            android:textColor="@android:color/white"
            android:textSize="16sp" />

        <Button
            android:layout_width="48dp"
            android:layout_height="48dp"
            android:background="@android:color/Black"
            android:onClick="increment"
            android:text="+" />

    </LinearLayout>


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="16dp"
        android:paddingTop="16dp"
        android:text="ORDER SUMMARY"
        android:textColor="@android:color/white" />

    <TextView
        android:id="@+id/ordrer_summary__text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="16dp"
        android:paddingBottom="16dp"
        android:paddingTop="16dp"
        android:text="$10"

        android:textColor="@android:color/white"
        android:textSize="16sp" />


    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="16dp"
        android:background="@android:color/Black"
        android:onClick="submitOrder"
        android:text="ORDER" />

</LinearLayout>

@Brutus-Tinyfoot
Copy link

Brutus-Tinyfoot commented Jun 5, 2018

You know when you get it 'wrong' but it still seems to work? I didn't use any 'findviewByID for either check box. The following seems to work using onClick in the XML file to get to java:

boolean hasWhippedCream = false;
boolean hasChocolate = false;

...........
public void clickCheckbox(View view){
hasWhippedCream = ((CheckBox) view).isChecked();
}
public void clickChocCheckbox(View view){
hasChocolate = ((CheckBox) view).isChecked();
}

So has Android Studio been updated so it doesn't need that it doesn't need to find the display object, or is my method simply different?

@bardeesennab
Copy link

MAIINACTIVITY
package com.example.android.justjava;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
int quantity = 0;

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

private void calculatePrice(int quantity) {
    int price = quantity * 5;
}

private int calculatePrice() {
    int price = quantity * 5;
    return price;
}

private void displayQuantity(int number) {
    TextView quantityTextView = (TextView) findViewById(R.id.quantity_text_view);
    quantityTextView.setText("" + number);
}

public void increment(View view) {

    quantity = quantity + 1;

    displayQuantity(quantity);

}

public void decrement(View view) {

    quantity = quantity - 1;
    displayQuantity(quantity);

}

public String createOrderSummary(int price, boolean addwhippedcream, boolean addChocolate) {
    String pricemessage = "Name:  khathrene";
    pricemessage = pricemessage + "\nAdd whippedcream ?  " + addwhippedcream;
    pricemessage = pricemessage + "\nAdd chocolate? " + addChocolate;
    pricemessage = pricemessage + "\nQuantity:  " + quantity;
    pricemessage = pricemessage + "\ntotal:$  " + price;
    pricemessage = pricemessage + "\n Thank you";
    return pricemessage;
}


public void submitOrder(View view) {
    CheckBox whippedcreamCheckBox = (CheckBox) findViewById(R.id.whipped_cream_checkbox);
    boolean haswhippedcream = whippedcreamCheckBox.isChecked();
    CheckBox chocolateCheckBox = (CheckBox) findViewById(R.id.chocolate_checkbox);
    boolean haschocolate = chocolateCheckBox.isChecked();
    int price = calculatePrice();
    String pricemessage = createOrderSummary(price, haswhippedcream, haschocolate);
    displayMessage(pricemessage);


}


/**
 * This method displays the given text on the screen.
 */
private void displayMessage(String message) {
    TextView ordersummaryTextView = (TextView) findViewById(R.id.order_summary_text_view);
    ordersummaryTextView.setText(message);
}

}

@oashrafouad
Copy link

oashrafouad commented Jun 15, 2018

Couldn't we just create the two CheckBoxes for the whipped cream and chocolate in the createOrderSummary method? That way we wouldn't be adding any extra input parameters to the method createOrderSummary cuz the CheckBox and booleans are initiated within the method.
I found this as an easier way to add the CheckBox without messing in the submitOrder method too and just dealing with the createOrderSummary method only. The method would look like this:

private String createOrderSummary(int price) { boolean hasWhippedCream = ((CheckBox) findViewById(R.id.whipped_cream_checkbox)).isChecked(); boolean hasChocolate = ((CheckBox) findViewById(R.id.chooclate_checkbox)).isChecked(); String priceMessage = "Name: Omar Ashraf"; priceMessage += "\nAdd whipped cream? " + hasWhippedCream; priceMessage += "\n Add chocolate? " + hasChocolate; priceMessage += "\nQuantity: " + quantity; priceMessage += "\nTotal: $" + price; priceMessage += "\nThank you!"; return priceMessage; }

Also couldn't we create the boolean variable for the chocolate CheckBox in just one line like this?:

boolean hasChooclate =((CheckBox) findViewById(R.id.chooclate_checkbox)).isChecked();

Thank you.

@HabibManjotha
Copy link

It's wonderful... I have completed it without seeing the solution.

@orwa-te
Copy link

orwa-te commented Jun 25, 2018

Easy task but motivational

@adejuwonk1
Copy link

This code is a life saviour for a begginner. I have been wondering how i would debug the error in my code after adjusting few lines. but copy and pasting this code did the job

@mohameddss
Copy link

/**

  • IMPORTANT: Make sure you are using the correct package name.
  • This example uses the package name:
  • package com.example.android.justjava
  • If you get an error when copying this code into Android studio, update it to match teh package name found
  • in the project's AndroidManifest.xml file.
    **/

package com.example.android.copyjustjava;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;

import com.example.android.copyjustjava.R;

import java.text.NumberFormat;

/**

  • This app displays an order form to order coffee.
    */
    public class MainActivity extends AppCompatActivity {

    int quantity = 0;

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

    /**

    • This method is called when the order button is clicked.
    • String message = "Total :" + "$" + (quantity * 5);
      displayMessage(message);

    او

    • int Price = quantity * 5;
      String message = "Total :" + "$" + Price;
      displayMessage(message);
      */

    public void submitOrder(View view) {

     CheckBox whippedCreamCheckBox = (CheckBox) findViewById(R.id.whipped_cream_checkbox);
     boolean hasWhippedCream = whippedCreamCheckBox.isChecked();
     CheckBox chocolateCheckBox = findViewById(R.id.chocolate_checkbox);
     boolean hasChocolate = chocolateCheckBox.isChecked();
    
    
    
     int Price = quantity * 5;
     String priceMessage = "Name: Mohamed salah gabr";
     priceMessage += "\nAdd whipped cream? "+ hasWhippedCream;
     priceMessage += "\nAdd Chocolate?  " + hasChocolate;
     priceMessage += "\nQuntity: " + quantity;
     priceMessage += "\nTotal: $" + Price;
     priceMessage +=  "\nThank You!";
     displayMessage(priceMessage);
    

    }
    /**

    • This method is called when the plus button is clicked.
      */
      public void increment(View view) {
      quantity = quantity + 1;
      display(quantity);

    }
    /**

    • This method is called when the minus button is clicked.
      */
      public void decrement(View view) {
      quantity = quantity - 1;
      display(quantity);

    }

    /**

    • This method displays the given price on the screen.
      */
      private void displayPrice(int number) {
      TextView priceTextView = (TextView) findViewById(R.id.order_summary_text_view);
      priceTextView.setText(NumberFormat.getCurrencyInstance().format(number));
      }

    /**

    • This method displays the given quantity value on the screen.
      */
      private void display(int number) {
      TextView quantityTextView = (TextView) findViewById(R.id.quantity_text_view);
      quantityTextView.setText("" + number);
      }

    /**

    • This method displays the given text on the screen.
      */
      private void displayMessage(String message) {
      TextView orderSummaryTextView = (TextView) findViewById(R.id.order_summary_text_view);
      orderSummaryTextView.setText(message);
      }
      }

@YassineOuardini
Copy link

package com.example.myapplication;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;

/**

  • This app displays an order form to order coffee.
    */
    public class MainActivity extends AppCompatActivity {
    int quantity = 2;
    @OverRide
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    }

    /**

    • This method is called when the order button is clicked.
      */
      public void submitOrder(View view) {
      CheckBox whippercreamcheckbox = (CheckBox) findViewById(R.id.Whipped_Cream_checkbox);
      CheckBox chocolat = (CheckBox) findViewById(R.id.Whipped_Chocolat_checkbox);
      boolean hasWhpperCream = whippercreamcheckbox.isChecked();
      boolean haschocolat = chocolat.isChecked();

      calculatePrice(quantity);
      displayMessage(creatOrderSummary(hasWhpperCream, haschocolat));
      }

      private String creatOrderSummary(Boolean hasWhpperCream, Boolean haschocolat){
          int price = calculatePrice(quantity);
      
          String priceMessage  = "add whepperCream ? "+hasWhpperCream + "\nadd whepperChocolat ? "+haschocolat + "\nQuantity : "+quantity ;
          priceMessage = priceMessage + "\nTotal: $ " + price ;
          priceMessage = priceMessage + "\nThank you!";
      
               return priceMessage;
      }
      

    private int calculatePrice(int quantity){
    int price = quantity *5 ;
    return price ;
    }

    public void increment(View view) {
    quantity =quantity +1;
    display(quantity);
    }
    public void decrement(View view) {
    quantity=quantity -1;
    display(quantity);
    }

    /**

    • This method displays the given quantity value on the screen.
      */
      private void display(int number) {
      TextView quantityTextView = (TextView) findViewById(R.id.quantity_text_view);
      quantityTextView.setText("" + number);
      }

    private void displayMessage (String number) {
    int x = calculatePrice(quantity);

     TextView order_summaryTextView = (TextView) findViewById(R.id.order_summary_text_view);
     order_summaryTextView.setText("Name : Yassine Ouardini\n" + number);
     if (x > 50)
         order_summaryTextView.setTextColor(Color.RED);
     else
         order_summaryTextView.setTextColor(Color.BLACK);
     }
    

    }

@YassineOuardini
Copy link

package com.example.myapplication;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;

/**

  • This app displays an order form to order coffee.
    */
    public class MainActivity extends AppCompatActivity {
    int quantity = 2;
    @OverRide
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    }

    /**

    • This method is called when the order button is clicked.
      */
      public void submitOrder(View view) {
      CheckBox whippercreamcheckbox = (CheckBox) findViewById(R.id.Whipped_Cream_checkbox);
      CheckBox chocolat = (CheckBox) findViewById(R.id.Whipped_Chocolat_checkbox);
      boolean hasWhpperCream = whippercreamcheckbox.isChecked();
      boolean haschocolat = chocolat.isChecked();

      calculatePrice(quantity);
      displayMessage(creatOrderSummary(hasWhpperCream, haschocolat));
      }

      private String creatOrderSummary(Boolean hasWhpperCream, Boolean haschocolat){
          int price = calculatePrice(quantity);
      
          String priceMessage  = "add whepperCream ? "+hasWhpperCream + "\nadd whepperChocolat ? "+haschocolat + "\nQuantity : "+quantity ;
          priceMessage = priceMessage + "\nTotal: $ " + price ;
          priceMessage = priceMessage + "\nThank you!";
      
               return priceMessage;
      }
      

    private int calculatePrice(int quantity){
    int price = quantity *5 ;
    return price ;
    }

    public void increment(View view) {
    quantity =quantity +1;
    display(quantity);
    }
    public void decrement(View view) {
    quantity=quantity -1;
    display(quantity);
    }

    /**

    • This method displays the given quantity value on the screen.
      */
      private void display(int number) {
      TextView quantityTextView = (TextView) findViewById(R.id.quantity_text_view);
      quantityTextView.setText("" + number);
      }

    private void displayMessage (String number) {
    int x = calculatePrice(quantity);

     TextView order_summaryTextView = (TextView) findViewById(R.id.order_summary_text_view);
     order_summaryTextView.setText("Name : Yassine Ouardini\n" + number);
     if (x > 50)
         order_summaryTextView.setTextColor(Color.RED);
     else
         order_summaryTextView.setTextColor(Color.BLACK);
     }
    

    }

@GianlucaVolpe
Copy link

I had to write this code, otherwise, when clicked on the checkbox I got the message JustJava has stopped. But in the code is not mentioned can someone explain, please?

`public void CheckBox(View view) {
boolean checked = ((CheckBox) view).isChecked();
}

public void CheckBox2(View view) {
    boolean checked = ((CheckBox) view).isChecked();
}`

@omeshkumar65
Copy link

This code also affect the price as per chocolatebox is checked or whippedCreamBox checked

`package com.o.just_java;

import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;

import java.text.NumberFormat;

import androidx.appcompat.app.AppCompatActivity;

/**

  • This app displays an order form to order coffee.
    */
    public class MainActivity extends AppCompatActivity {

    int quantity= 0;
    int price = 5;
    int whippedCreamPrice=0;
    int chocolatePrice=0;

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

    /**

    • This method is called when the order button is clicked.
      */

    public void submitOrder(View view) {
    CheckBox whippedCreamCheckBox = (CheckBox) findViewById(R.id.whippedCreamCheckBox);
    boolean hasWhippedCream= whippedCreamCheckBox.isChecked();

     CheckBox chocolateBox = (CheckBox) findViewById(R.id.chocolateBox);
     boolean hasChocolate= chocolateBox.isChecked();
    
     if(hasWhippedCream == true)
         {
             whippedCreamPrice=2;
         }
         else {
         whippedCreamPrice=0;
         }
     if(hasChocolate ==  true) {
         chocolatePrice=3;
     }
     else {
         chocolatePrice=0;
     }
    
     String priceMessage= "Name: "+"Omesh kumar"+
             "\nQuantity: " +quantity+
             "\nwippedCream: " + hasWhippedCream +
             "\nChocolate: " + hasChocolate +
             "\nTotal $ "+ quantity* (price +whippedCreamPrice +chocolatePrice) +
             "\nThankyou";
     displayMessage(priceMessage);
     // displayPrice(quantity*5);
    

    }

    public void Increment(View view)
    {
    quantity= quantity + 1;
    display(quantity);
    }

    public void Decrement(View view)
    {
    quantity= quantity - 1;
    display(quantity);
    }

    /**

    • This method displays the given quantity value on the screen.
      */
      private void display(int number) {
      TextView quantityTextView = (TextView) findViewById(R.id.quantity_text_view);
      quantityTextView.setText("" + number);
      }

    /**

    • This method displays the given price on the screen.
      */
      private void displayPrice(int number) {
      TextView priceTextView = (TextView) findViewById(R.id.price_text_view);
      priceTextView.setText(NumberFormat.getCurrencyInstance().format(number));
      }
      private void displayMessage(String message) {
      TextView priceTextView = (TextView) findViewById(R.id.price_text_view);
      priceTextView.setText(message);
      }
      }`

@yaz71
Copy link

yaz71 commented Sep 22, 2020

I did it using a different code, still worked :D ! thank you for the code tho !!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment