title: Want to add condition on my username field
body: <p>I want to add the condition on the user name field.
  If the user did not enter his name, a toast message should be displayed on the screen "Please enter your name"</p>
  
  <p>Where should I put the if statement in my java code ?</p>
  
  <p>Can anybody help me with this ?</p>
  
  <p>This is my java code below:</p>
  
  <pre><code>package com.infinitystone.mani.justjava;
  
  import android.os.Bundle;
  import android.support.v7.app.AppCompatActivity;
  import android.view.View;
  import android.widget.CheckBox;
  import android.widget.EditText;
  import android.widget.TextView;
  import android.widget.Toast;
  
  /**
   * 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 + button is clicked.
       */
  
      public void increment(View view) {
          if (quantity &gt;= 10){
              Toast.makeText(getApplicationContext(), "Only 10 cups per order",
                      Toast.LENGTH_SHORT).show();
              return;
          }
          quantity = quantity + 1;
          displayQuantity(quantity);
      }
  
      /**
       * This method is called when the - button is clicked.
       */
  
      public void decrement(View view) {
          if (quantity&lt;=0){
              Toast.makeText(this, "Dude! are you drunk ?",
                      Toast.LENGTH_SHORT).show();
              return;
          }
          quantity = quantity - 1;
          displayQuantity(quantity);
      }
  
      /**
       * This method is called when the order button is clicked.
       */
  
      public void submitOrder(View view) {
          if (quantity == 0){
              Toast.makeText(this, "How many bottles you had ?",
                      Toast.LENGTH_SHORT).show();
              return;
          }
              EditText userName = (EditText) findViewById(R.id.name_edit_text);
              String name = userName.getText().toString();
              CheckBox whippedCream = (CheckBox) findViewById(R.id.checkbox_cream);
              boolean hasWhippedCream = whippedCream.isChecked();
              CheckBox chocolate = (CheckBox) findViewById(R.id.checkbox_chocolate);
              boolean hasChocolate = chocolate.isChecked();
              int finalPrice = calculatePrice(hasWhippedCream, hasChocolate);
              String priceMessage = createOrderSummary(finalPrice, hasWhippedCream, hasChocolate, name);
              displayMessage(priceMessage);
      }
  
      /**
       * Calculates the price of the order.
       *
       * @param addWhippedCream tells if the user wants to add whipped cream
       * @param addChocolate tells if the user wants to add chocolate
       * @return total price of the order
       */
      private int calculatePrice(boolean addWhippedCream, boolean addChocolate) {
          int basePrice = 5;
          if (addWhippedCream){
              basePrice = basePrice + 1;
          }
  
          if (addChocolate){
              basePrice = basePrice +2;
          }
          return quantity * basePrice;
      }
  
      /**
       * Creates the summary of the order
       *
       * @param price
       * @param addWhippedCream
       * @param addChocolate
       * @param addUserName
       * @return
       */
      private String createOrderSummary(int price,
                                        boolean addWhippedCream,
                                        boolean addChocolate,
                                        String addUserName) {
          String orderSummary = "Name: " + addUserName;
          orderSummary += "\nQuantity: " + quantity + " cups";
          orderSummary += "\nWhipped cream added: " + addWhippedCream;
          orderSummary += "\nChocolate added: " + addChocolate;
          orderSummary += "\nTotal = $" + price;
          orderSummary += "\nThank You!";
          return orderSummary;
      }
  
      /**
       * 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);
      }
  }
  </code></pre>
  
  <p>And here's my XML code for the layout</p>
  
  <pre><code>&lt;ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      tools:context=".MainActivity"&gt;
  
      &lt;LinearLayout
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:orientation="vertical"&gt;
  
          &lt;EditText
              android:id="@+id/name_edit_text"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:layout_marginLeft="16dp"
              android:layout_marginRight="16dp"
              android:layout_marginTop="16dp"
              android:hint="Name"
              android:inputType="textCapWords" /&gt;
  
          &lt;TextView
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_marginLeft="16dp"
              android:layout_marginTop="8dp"
              android:text="toppings"
              android:textAllCaps="true" /&gt;
  
          &lt;CheckBox
              android:id="@+id/checkbox_cream"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_marginLeft="16dp"
              android:layout_marginTop="8dp"
              android:paddingLeft="18dp"
              android:text="Whipped Cream"
              android:textSize="16sp" /&gt;
  
          &lt;CheckBox
              android:id="@+id/checkbox_chocolate"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_marginLeft="16dp"
              android:layout_marginTop="8dp"
              android:paddingLeft="18dp"
              android:text="Chocolate"
              android:textSize="16sp" /&gt;
  
          &lt;View
              android:layout_width="match_parent"
              android:layout_height="1dp"
              android:layout_marginLeft="16dp"
              android:layout_marginRight="168dp"
              android:layout_marginTop="8dp"
              android:background="@android:color/darker_gray"&gt;
  
          &lt;/View&gt;
  
          &lt;TextView
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_marginLeft="16dp"
              android:layout_marginTop="8dp"
              android:text="Quantity"
              android:textAllCaps="true" /&gt;
  
          &lt;LinearLayout
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_marginLeft="16dp"
              android:layout_marginTop="8dp"
              android:orientation="horizontal"&gt;
  
              &lt;Button
                  android:layout_width="48dp"
                  android:layout_height="48dp"
                  android:onClick="decrement"
                  android:text="-" /&gt;
  
              &lt;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" /&gt;
  
              &lt;Button
                  android:layout_width="48dp"
                  android:layout_height="48dp"
                  android:onClick="increment"
                  android:text="+" /&gt;
  
          &lt;/LinearLayout&gt;
  
          &lt;View
              android:layout_width="match_parent"
              android:layout_height="1dp"
              android:layout_marginLeft="16dp"
              android:layout_marginRight="168dp"
              android:layout_marginTop="8dp"
              android:background="@android:color/darker_gray"&gt;
  
          &lt;/View&gt;
  
          &lt;TextView
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_marginLeft="16dp"
              android:layout_marginTop="8dp"
              android:text="order summary"
              android:textAllCaps="true" /&gt;
  
          &lt;TextView
              android:id="@+id/order_summary_text_view"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_marginLeft="16dp"
              android:layout_marginTop="8dp"
              android:text="$0"
              android:textColor="@android:color/black"
              android:textSize="16sp" /&gt;
  
          &lt;Button
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_marginLeft="16dp"
              android:layout_marginTop="8dp"
              android:onClick="submitOrder"
              android:text="Order" /&gt;
  
      &lt;/LinearLayout&gt;
  &lt;/ScrollView&gt;
  </code></pre>