title: Android Studio calculate total price
body: <p>I wanna ask how can I sum all patty price and display in the total ? The total is in activity_main.xml layout while all the product price is in view_product.xml .
  <a href="https://i.stack.imgur.com/sW7nw.png" rel="nofollow noreferrer">Screenshot of app</a>
  As you can see the grey color text is per patty price , and the right side black text is price after multiply the quantity of patty. I want all the black text to sum up and show at below total.</p>
  <p><code>MainActivity.java</code></p>
  <pre><code>package com.tankarjian.it212n.a2910assingment;
  
  import android.app.Activity;
  import android.os.Bundle;
  import android.widget.BaseAdapter;
  import android.widget.ListView;
  
  import java.util.ArrayList;
  import java.util.Arrays;
  import java.util.List;
  
  public class MainActivity extends Activity {
  
      private Products products;
      private BaseAdapter productsAdapter;
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          products = createInitialProductList();
          productsAdapter = new ProductsAdapter(products, productClickListener, getLayoutInflater());
  
          ListView productsListView = (ListView) findViewById(R.id.products_list);
          productsListView.setAdapter(productsAdapter);
      }
  
      private Products createInitialProductList() {
          return new Products(new ArrayList&lt;&gt;(Arrays.asList(
                  new Product(&quot;Chicken Patty&quot;, 0, 4.00),
                  new Product(&quot;Chicken Special Patty&quot;, 0, 5.00),
                  new Product(&quot;Imported Lamb Patty&quot;, 0, 8.00),
                  new Product(&quot;Imported Lamb Special Patty&quot;, 0, 10.00)
  
          )));
      }
  
      private final ProductClickListener productClickListener = new ProductClickListener() {
          @Override
          public void onMinusClick(Product product) {
              products.removeOneFrom(product);
              productsAdapter.notifyDataSetChanged();
          }
  
          @Override
          public void onPlusClick(Product product) {
              products.addOneTo(product);
              productsAdapter.notifyDataSetChanged();
          }
      };
  
      private static class Products implements ProductDataSet {
  
          private final List&lt;Product&gt; productList;
  
          Products(List&lt;Product&gt; productList) {
              this.productList = productList;
          }
  
          @Override
          public int size() {
              return productList.size();
          }
  
          @Override
          public Product get(int position) {
              return productList.get(position);
          }
  
          @Override
          public long getId(int position) {
              return position;
          }
  
          public void removeOneFrom(Product product) {
              int i = productList.indexOf(product);
              if (i == -1) {
                  throw new IndexOutOfBoundsException();
              }
  
              Product updatedProduct = new Product(product.name, (product.quantity - 1), product.getPrice());
              productList.remove(product);
              productList.add(i, updatedProduct);
          }
  
          public void addOneTo(Product product) {
              int i = productList.indexOf(product);
              if (i == -1) {
                  throw new IndexOutOfBoundsException();
              }
              Product updatedProduct = new Product(product.name, (product.quantity + 1), product.getPrice());
              productList.remove(product);
              productList.add(i, updatedProduct);
          }
      }
  
  }
  </code></pre>
  <p><code>ProductsAdapter.java</code></p>
  <pre><code>package com.tankarjian.it212n.a2910assingment;
  
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.BaseAdapter;
  import android.widget.ImageView;
  import android.widget.TextView;
  
  import java.util.Locale;
  
  class ProductsAdapter extends BaseAdapter {
  
      private final ProductDataSet productDataSet;
      private final ProductClickListener productClickListener;
      private final LayoutInflater layoutInflater;
  
      ProductsAdapter(ProductDataSet productDataSet, ProductClickListener productClickListener, LayoutInflater layoutInflater) {
          this.productDataSet = productDataSet;
          this.productClickListener = productClickListener;
          this.layoutInflater = layoutInflater;
      }
  
      @Override
      public int getCount() {
          return productDataSet.size();
      }
  
      @Override
      public Product getItem(int position) {
          return productDataSet.get(position);
      }
  
      @Override
      public long getItemId(int position) {
          return productDataSet.getId(position);
      }
  
      @Override
      public boolean hasStableIds() {
          return true;
      }
  
      @Override
      public View getView(int position, View convertView, ViewGroup parent) {
          View view = convertView;
          if (view == null) {
              view = createView(parent);
              view.setTag(ViewHolder.from(view));
          }
  
          Product Products =(Product) getItem(position);
  
          ImageView image = (ImageView) view.findViewById(R.id.imageBurgerView);
          image.setImageResource(R.drawable.burger);
          
          TextView text = (TextView) view.findViewById(R.id.singlePrice);
          String price = String.format(Locale.ENGLISH, &quot;%.2f&quot;, Products.getPrice());
          text.setText(price);
  
          TextView text1 = (TextView) view.findViewById(R.id.totalPrice);
          String totalPrice = String.format(Locale.ENGLISH, &quot;%.2f&quot;, (Products.getPrice() * Products.quantity));
          text1.setText(totalPrice);
  
          Product product = productDataSet.get(position);
          ViewHolder viewHolder = (ViewHolder) view.getTag();
          update(viewHolder, product);
          return view;
  
  
      }
  
      private View createView(ViewGroup parent) {
          return layoutInflater.inflate(R.layout.view_product, parent, false);
      }
  
      private void update(ViewHolder viewHolder, final Product product) {
          viewHolder.name.setText(product.name);
          viewHolder.quantity.setText(String.valueOf(product.quantity));
          viewHolder.minus.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  productClickListener.onMinusClick(product);
              }
          });
          viewHolder.plus.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  productClickListener.onPlusClick(product);
              }
          });
      }
  
      private static final class ViewHolder {
          final TextView name;
          final TextView quantity;
          final View minus;
          final View plus;
  
          static ViewHolder from(View view) {
              return new ViewHolder(
                      ((TextView) view.findViewById(R.id.product_name)),
                      ((TextView) view.findViewById(R.id.product_quantity)),
                      view.findViewById(R.id.product_minus),
                      view.findViewById(R.id.product_plus)
  
              );
          }
  
          private ViewHolder(TextView name, TextView quantity, View minus, View plus) {
              this.name = name;
              this.quantity = quantity;
              this.minus = minus;
              this.plus = plus;
          }
      }
  
  }
  </code></pre>
  <p><code>Product.java</code></p>
  <pre><code>package com.tankarjian.it212n.a2910assingment;
  
  class Product {
      final String name;
      final int quantity;
      private double price;
  
      Product(String name, int quantity, Double price) {
          this.name = name;
          this.quantity = quantity;
          this.price = price;
  
      }
      public double getPrice() {return price;}
  }
  </code></pre>
  <p><code>activity_main.xml</code></p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  &lt;androidx.constraintlayout.widget.ConstraintLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
      xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
      xmlns:tools=&quot;http://schemas.android.com/tools&quot;
      android:layout_width=&quot;match_parent&quot;
      android:layout_height=&quot;match_parent&quot;
      android:layout_marginStart=&quot;15dp&quot;
      android:layout_marginTop=&quot;16dp&quot;
      android:layout_marginEnd=&quot;15dp&quot;
      android:layout_marginBottom=&quot;16dp&quot;
      tools:context=&quot;.MainActivity&quot;&gt;
  
      &lt;ListView
          android:id=&quot;@+id/products_list&quot;
          android:layout_width=&quot;0dp&quot;
          android:layout_height=&quot;512dp&quot;
          app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
          app:layout_constraintEnd_toEndOf=&quot;parent&quot;
          app:layout_constraintHorizontal_bias=&quot;0.0&quot;
          app:layout_constraintStart_toStartOf=&quot;parent&quot;
          app:layout_constraintTop_toTopOf=&quot;parent&quot;
          app:layout_constraintVertical_bias=&quot;0.0&quot; /&gt;
  
      &lt;Switch
          android:id=&quot;@+id/switchMember&quot;
          android:layout_width=&quot;wrap_content&quot;
          android:layout_height=&quot;wrap_content&quot;
          android:minHeight=&quot;48dp&quot;
          android:text=&quot;Member&quot;
          app:layout_constraintEnd_toEndOf=&quot;@+id/products_list&quot;
          app:layout_constraintTop_toBottomOf=&quot;@+id/products_list&quot; /&gt;
  
      &lt;TextView
          android:id=&quot;@+id/textView2&quot;
          android:layout_width=&quot;wrap_content&quot;
          android:layout_height=&quot;25dp&quot;
          android:layout_marginStart=&quot;16dp&quot;
          android:text=&quot;Tax &quot;
          android:textColor=&quot;#2A2F49&quot;
          android:textSize=&quot;19sp&quot;
          android:textStyle=&quot;bold|italic&quot;
          app:layout_constraintBottom_toTopOf=&quot;@+id/textView3&quot;
          app:layout_constraintStart_toStartOf=&quot;parent&quot;
          app:layout_constraintTop_toBottomOf=&quot;@+id/products_list&quot;
          app:layout_constraintVertical_bias=&quot;0.92&quot; /&gt;
  
      &lt;TextView
          android:id=&quot;@+id/textView3&quot;
          android:layout_width=&quot;wrap_content&quot;
          android:layout_height=&quot;25dp&quot;
          android:layout_marginBottom=&quot;68dp&quot;
          android:text=&quot;Total &quot;
          android:textColor=&quot;#2A2F49&quot;
          android:textSize=&quot;19sp&quot;
          android:textStyle=&quot;bold|italic&quot;
          app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
          app:layout_constraintStart_toStartOf=&quot;parent&quot; /&gt;
  
      &lt;TextView
          android:id=&quot;@+id/totalTax&quot;
          android:layout_width=&quot;45dp&quot;
          android:layout_height=&quot;wrap_content&quot;
          android:text=&quot;0&quot;
          android:textColor=&quot;#2A2F49&quot;
          android:textSize=&quot;19sp&quot;
          android:textStyle=&quot;bold|italic&quot;
          app:layout_constraintEnd_toEndOf=&quot;parent&quot;
          app:layout_constraintHorizontal_bias=&quot;1.0&quot;
          app:layout_constraintStart_toEndOf=&quot;@+id/textView2&quot;
          app:layout_constraintTop_toTopOf=&quot;@+id/textView7&quot; /&gt;
  
      &lt;TextView
          android:id=&quot;@+id/textView9&quot;
          android:layout_width=&quot;wrap_content&quot;
          android:layout_height=&quot;wrap_content&quot;
          android:layout_marginEnd=&quot;5dp&quot;
          android:text=&quot;RM&quot;
          android:textColor=&quot;#2A2F49&quot;
          android:textSize=&quot;19sp&quot;
          android:textStyle=&quot;bold|italic&quot;
          app:layout_constraintEnd_toStartOf=&quot;@+id/totalTax&quot;
          app:layout_constraintHorizontal_bias=&quot;1.0&quot;
          app:layout_constraintStart_toEndOf=&quot;@+id/textView7&quot;
          app:layout_constraintTop_toTopOf=&quot;@+id/textView7&quot; /&gt;
  
      &lt;TextView
          android:id=&quot;@+id/textView10&quot;
          android:layout_width=&quot;wrap_content&quot;
          android:layout_height=&quot;wrap_content&quot;
          android:layout_marginEnd=&quot;5dp&quot;
          android:text=&quot;RM&quot;
          android:textColor=&quot;#2A2F49&quot;
          android:textSize=&quot;19sp&quot;
          android:textStyle=&quot;bold|italic&quot;
          app:layout_constraintEnd_toStartOf=&quot;@+id/finalTotalPrice&quot;
          app:layout_constraintHorizontal_bias=&quot;1.0&quot;
          app:layout_constraintStart_toEndOf=&quot;@+id/textView8&quot;
          app:layout_constraintTop_toTopOf=&quot;@+id/textView8&quot; /&gt;
  
      &lt;TextView
          android:id=&quot;@+id/finalTotalPrice&quot;
          android:layout_width=&quot;45dp&quot;
          android:layout_height=&quot;wrap_content&quot;
          android:text=&quot;0&quot;
          android:textColor=&quot;#2A2F49&quot;
          android:textSize=&quot;19sp&quot;
          android:textStyle=&quot;bold|italic&quot;
          app:layout_constraintEnd_toEndOf=&quot;parent&quot;
          app:layout_constraintHorizontal_bias=&quot;1.0&quot;
          app:layout_constraintStart_toEndOf=&quot;@+id/textView3&quot;
          app:layout_constraintTop_toTopOf=&quot;@+id/textView8&quot; /&gt;
  
      &lt;TextView
          android:id=&quot;@+id/textView7&quot;
          android:layout_width=&quot;210dp&quot;
          android:layout_height=&quot;25dp&quot;
          android:layout_marginStart=&quot;20dp&quot;
          android:text=&quot;--------------------------------&quot;
          android:textColor=&quot;#2A2F49&quot;
          android:textSize=&quot;19sp&quot;
          android:textStyle=&quot;bold|italic&quot;
          app:layout_constraintEnd_toStartOf=&quot;@+id/totalTax&quot;
          app:layout_constraintHorizontal_bias=&quot;0.0&quot;
          app:layout_constraintStart_toEndOf=&quot;@+id/textView2&quot;
          app:layout_constraintTop_toTopOf=&quot;@+id/textView2&quot; /&gt;
  
      &lt;TextView
          android:id=&quot;@+id/textView8&quot;
          android:layout_width=&quot;210dp&quot;
          android:layout_height=&quot;26dp&quot;
          android:layout_marginStart=&quot;20dp&quot;
          android:text=&quot;--------------------------------&quot;
          android:textColor=&quot;#2A2F49&quot;
          android:textSize=&quot;19sp&quot;
          android:textStyle=&quot;bold|italic&quot;
          app:layout_constraintEnd_toStartOf=&quot;@+id/finalTotalPrice&quot;
          app:layout_constraintHorizontal_bias=&quot;0.08&quot;
          app:layout_constraintStart_toEndOf=&quot;@+id/textView3&quot;
          app:layout_constraintTop_toTopOf=&quot;@+id/textView3&quot; /&gt;
  
  &lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;
  </code></pre>
  <p><code>view_product.xml</code></p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  &lt;LinearLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
      xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
      xmlns:tools=&quot;http://schemas.android.com/tools&quot;
      android:orientation=&quot;vertical&quot;
      android:layout_width=&quot;match_parent&quot;
      android:layout_height=&quot;match_parent&quot;&gt;
  
      &lt;TextView
          android:id=&quot;@+id/product_name&quot;
          android:layout_width=&quot;match_parent&quot;
          android:layout_height=&quot;wrap_content&quot;
          android:textColor=&quot;#FF5722&quot;
          android:textSize=&quot;20sp&quot;
          android:textStyle=&quot;bold|italic&quot; /&gt;
  
      &lt;LinearLayout
          android:layout_width=&quot;match_parent&quot;
          android:layout_height=&quot;wrap_content&quot;
          android:orientation=&quot;horizontal&quot;&gt;
  
          &lt;ImageView
              android:id=&quot;@+id/imageBurgerView&quot;
              android:layout_width=&quot;70dp&quot;
              android:layout_height=&quot;60dp&quot;
              android:layout_marginStart=&quot;10dp&quot;
              android:contentDescription=&quot;@string/todo&quot;
              app:srcCompat=&quot;@drawable/burger&quot; /&gt;
  
          &lt;TextView
              android:id=&quot;@+id/textView4&quot;
              android:layout_width=&quot;wrap_content&quot;
              android:layout_height=&quot;wrap_content&quot;
              android:layout_marginStart=&quot;8dp&quot;
              android:layout_weight=&quot;1&quot;
              android:text=&quot;@string/rm&quot;
              android:textAlignment=&quot;textEnd&quot;
              android:textColor=&quot;#878BA3&quot;
              android:textSize=&quot;16sp&quot;
              android:textStyle=&quot;bold&quot; /&gt;
  
          &lt;TextView
              android:id=&quot;@+id/singlePrice&quot;
              android:layout_width=&quot;108dp&quot;
              android:layout_height=&quot;wrap_content&quot;
              android:layout_marginStart=&quot;3dp&quot;
              android:layout_weight=&quot;1&quot;
              android:text=&quot;@string/textview&quot;
              android:textColor=&quot;#878BA3&quot;
              android:textSize=&quot;16sp&quot;
              android:textStyle=&quot;bold&quot; /&gt;
  
          &lt;TextView
              android:id=&quot;@+id/textView&quot;
              android:layout_width=&quot;wrap_content&quot;
              android:layout_height=&quot;wrap_content&quot;
              android:layout_weight=&quot;1&quot;
              android:text=&quot;@string/rm&quot;
              android:textAlignment=&quot;textEnd&quot;
              android:textColor=&quot;#272E53&quot;
              android:textSize=&quot;16sp&quot;
              android:textStyle=&quot;bold|italic&quot; /&gt;
  
          &lt;TextView
              android:id=&quot;@+id/totalPrice&quot;
              android:layout_width=&quot;45dp&quot;
              android:layout_height=&quot;wrap_content&quot;
              android:layout_weight=&quot;1&quot;
              android:text=&quot;@string/textview&quot;
              android:textAlignment=&quot;textStart&quot;
              android:textColor=&quot;#272E53&quot;
              android:textSize=&quot;16sp&quot;
              android:textStyle=&quot;bold|italic&quot; /&gt;
      &lt;/LinearLayout&gt;
  
      &lt;LinearLayout
          android:layout_width=&quot;116dp&quot;
          android:layout_height=&quot;wrap_content&quot;
          android:orientation=&quot;horizontal&quot;&gt;
  
          &lt;Button
              android:id=&quot;@+id/product_minus&quot;
              android:layout_width=&quot;30dp&quot;
              android:layout_height=&quot;40dp&quot;
              android:text=&quot;-&quot;
              tools:ignore=&quot;TouchTargetSizeCheck&quot; /&gt;
  
          &lt;TextView
              android:id=&quot;@+id/product_quantity&quot;
              android:layout_width=&quot;0dp&quot;
              android:layout_height=&quot;wrap_content&quot;
              android:layout_gravity=&quot;center&quot;
              android:layout_weight=&quot;20&quot;
              android:textAlignment=&quot;center&quot;
              android:textSize=&quot;16sp&quot;
              android:textStyle=&quot;bold&quot; /&gt;
  
          &lt;Button
              android:id=&quot;@+id/product_plus&quot;
              android:layout_width=&quot;30dp&quot;
              android:layout_height=&quot;40dp&quot;
              android:text=&quot;+&quot;
              tools:ignore=&quot;TouchTargetSizeCheck&quot; /&gt;
  
      &lt;/LinearLayout&gt;
  
  
  &lt;/LinearLayout&gt;
  </code></pre>