Monday, February 7, 2011

Android Shopping Cart Tutorial

In this tutorial you will learn how to create a simple app that features several products which can be placed in a shopping cart. You can then view the contents of the shopping cart and remove items that you no longer want.


Update - after following this tutorial, be sure to check out part 2 and also check out part 3.


Creating a Shopping Cart for Android

A common problem that arises when developing Android applications is figuring out how to pass information from Activity to Activity within the Android application. This problem can be solved in two ways. For passing small amounts of information, you can put extras in the Intent that gets passed along to create the activity. For managing a larger, more complex amount of information the use of static variables is recommended.

Through this tutorial we will create a simple shopping cart. At the end of the tutorial, there are several suggestions for improving the shopping cart. Some of these suggestions may be included in a future tutorial.

Screenshot of the finished Shopping Cart


Listed in the graphic below is an outline of how the activities and layouts are connected together. This tutorial will feature three different activities and several different layouts. The layout files are the xml files listed in blue.

The product adapter class is used in both the CatalogActivity and the ShoppingCartActivity to adapt data for the list views.


Activites and Layouts of the Shopping Cart

Step 1. Create the layouts for the app.

This shopping cart tutorial will involve several different layouts.
  • Catalog
  • Product Details
  • Shopping Cart
When the app starts, you will be immediately presented with the Catalog layout. From the catalog layout you can click on a product to view more details about it, and from there, you can add the product to your cart.

After adding the product to your cart, you will be taken back to the Catalog, where you can view details for other products or proceed to view your shopping cart.

The Shopping Cart layout will include the option to remove items from your cart, and to "Proceed to Checkout."

Since this is an advanced tutorial, we will not focus on the details of creating these layouts. A more detailed tutorial on creating layouts can be found here.

catalog.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical" android:layout_width="fill_parent"
 android:layout_height="fill_parent" android:background="#ffffff">

 <TextView android:id="@+id/TextView01" android:layout_width="wrap_content"
  android:layout_height="wrap_content" android:textColor="#000000"
  android:textSize="24dip" android:layout_margin="5dip" android:text="Product Catalog"></TextView>
 <ListView android:layout_height="wrap_content"
  android:layout_weight="1" android:id="@+id/ListViewCatalog"
  android:layout_width="fill_parent" android:background="#ffffff"
  android:clickable="true" android:cacheColorHint="#ffffff">
 </ListView>
 <Button android:layout_width="wrap_content"
  android:layout_height="wrap_content" android:layout_margin="5dip"
  android:layout_gravity="right" android:id="@+id/ButtonViewCart"
  android:text="View Shopping Cart"></Button>
</LinearLayout>


productdetails.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_height="fill_parent" android:layout_width="fill_parent"
 android:background="#ffffff" android:orientation="vertical">
 <LinearLayout android:layout_width="wrap_content"
  android:layout_height="wrap_content" android:id="@+id/LinearLayoutHeader"
  android:orientation="horizontal">
  <ImageView android:layout_width="wrap_content"
   android:layout_height="wrap_content" android:id="@+id/ImageViewProduct"
   android:adjustViewBounds="true" android:scaleType="fitXY"
   android:src="@drawable/deadoralive" android:layout_margin="5dip"></ImageView>
  <TextView android:layout_width="wrap_content"
   android:layout_height="wrap_content" android:id="@+id/TextViewProductTitle"
   android:layout_gravity="center" android:layout_margin="5dip"
   android:textSize="26dip" android:text="Dead or Alive"
   android:textColor="#000000"></TextView>

 </LinearLayout>
 <TextView android:layout_height="wrap_content" android:id="@+id/TextViewProductDetails"
  android:layout_width="fill_parent" android:layout_margin="5dip"
  android:layout_weight="1" android:textColor="#000000" android:text="Product description"></TextView>
 <Button android:layout_width="wrap_content"
  android:layout_height="wrap_content" android:layout_margin="5dip"
  android:layout_gravity="right" android:id="@+id/ButtonAddToCart"
  android:text="Add to Cart"></Button>
</LinearLayout>

shoppingcart.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical" android:layout_height="fill_parent"
 android:layout_width="fill_parent" android:background="#ffffff">

 <TextView android:id="@+id/TextView01" android:layout_width="wrap_content"
  android:layout_height="wrap_content" android:textColor="#000000"
  android:textSize="24dip" android:layout_margin="5dip" android:text="Shopping Cart"></TextView>
 <ListView android:layout_height="wrap_content"
  android:layout_weight="1" android:id="@+id/ListViewCatalog"
  android:layout_width="fill_parent" android:background="#ffffff"
  android:cacheColorHint="#ffffff" android:clickable="true"
  android:choiceMode="multipleChoice">

 </ListView>
 <LinearLayout android:id="@+id/LinearLayout01"
  android:layout_width="wrap_content" android:layout_height="wrap_content"
  android:orientation="horizontal" android:layout_gravity="right"
  android:layout_margin="5dip">
  <Button android:layout_width="wrap_content"
   android:layout_height="wrap_content" android:id="@+id/ButtonRemoveFromCart"
   android:text="Remove from Cart"></Button>
  <Button android:id="@+id/Button02" android:layout_width="wrap_content"
   android:layout_height="wrap_content" android:text="Proceed to Checkout"></Button>
 </LinearLayout>


</LinearLayout>

Step 2. Create the Product Class

Everything in this app revolves around the product class. The product class does not contain any of its own methods and really is just more of a container for other objects that describe the product including a title, an image, and a description.

package com.dreamdom.tutorials.shoppingcart;

import android.graphics.drawable.Drawable;

public class Product {

 public String title;
 public Drawable productImage;
 public String description;
 public double price;
 public boolean selected;

 public Product(String title, Drawable productImage, String description,
   double price) {
  this.title = title;
  this.productImage = productImage;
  this.description = description;
  this.price = price;
 }

}

Step 3. Create the ShoppingCartHelper class

The ShoppingCartHelper class is designed to hold information about all the products in the catalog, and all the products that are in the user's shopping cart.

In this tutorial, our catalog is hard coded in, so the list of products, images, and descriptions will always be the same. This could be fine for your app if you sell a very limited number of items, don't run into inventory issues and don't need to frequently update your catalog.

In this example, lets say that we are selling books. I took some pictures of a couple books laying around that we can use in this tutorial.
Switch by Chip Heath and Dan Heath

Dead or Alive by Tom Clancy with Grant Blackwood

Watchmen by Alan Moore and Dave Gibbons

Make sure to download these images, and place them in your drawable folder. Make sure not to name the image of the book "switch" "switch.png" This is because the name is used in the generated class R, and switch is a java keyword.

For a more advanced shopping cart, you may wish to download an xml file containing catalog information, and parse out the xml information to dynamically update the catalog when the user runs the application.

ShoppingCartHelper.java
package com.dreamdom.tutorials.shoppingcart;

import java.util.List;
import java.util.Vector;

import android.content.res.Resources;

public class ShoppingCartHelper {
 
 public static final String PRODUCT_INDEX = "PRODUCT_INDEX";
 
 private static List<Product> catalog;
 private static List<Product> cart;
 
 public static List<Product> getCatalog(Resources res){
  if(catalog == null) {
   catalog = new Vector<Product>();
   catalog.add(new Product("Dead or Alive", res
     .getDrawable(R.drawable.deadoralive),
     "Dead or Alive by Tom Clancy with Grant Blackwood", 29.99));
   catalog.add(new Product("Switch", res
     .getDrawable(R.drawable.switchbook),
     "Switch by Chip Heath and Dan Heath", 24.99));
   catalog.add(new Product("Watchmen", res
     .getDrawable(R.drawable.watchmen),
     "Watchmen by Alan Moore and Dave Gibbons", 14.99));
  }
  
  return catalog;
 }
 
 public static List<Product> getCart() {
  if(cart == null) {
   cart = new Vector<Product>();
  }
  
  return cart;
 }

}


By using static variables and static methods in the ShoppingCartHelper Class we allow any activity to access this catalog and shopping cart data, allowing us to easily and seamlessly access the same data in different activities in out application.

Step 4. Create the Item Layout

The Item layout represents how one product will be displayed in a list view in both the Catalog and Shopping Cart activities. It will show a thumbnail of the product, a title for the product, and optionally a checkbox.

item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_height="wrap_content" android:orientation="horizontal"
 android:layout_width="fill_parent" android:id="@+id/LinearLayoutItem">
 <ImageView android:layout_margin="5dip" android:id="@+id/ImageViewItem"
  android:layout_height="wrap_content" android:layout_width="100dip"></ImageView>
 <TextView android:layout_width="wrap_content"
  android:layout_height="wrap_content" android:layout_gravity="center"
  android:layout_margin="5dip" android:id="@+id/TextViewItem"
  android:textSize="26dip" android:text="Book Title" android:textColor="#000000"
  android:minLines="2" android:maxWidth="150dip"></TextView>
 <TextView android:id="@+id/TextView01" android:layout_width="wrap_content"
  android:layout_height="wrap_content" android:layout_weight="1"></TextView>
 <CheckBox android:layout_height="wrap_content"
  android:layout_margin="5dip" android:id="@+id/CheckBoxSelected"
  android:focusable="false" android:clickable="false"
  android:layout_gravity="center" android:layout_width="wrap_content"></CheckBox>
</LinearLayout>


Step 5. Create the ProductAdapter class

The ProductAdapter class is designed to be attached to a listview, and populate the data for the different products that will be in a list view. It uses the Item layout to populate the data in the list view.

The constructor for the ProductAdapter class also takes a boolean to determine if you want the checkbox to appear in the list or not. We only want the checkbox to appear in the Shopping Cart activity, so you can select multiple items in your cart and remove them if you wish.

Creating Adapters and using ListViews can be somewhat confusing, and I am definitely planning on doing a full detailed tutorial on that in the future.

ProductAdapter.java
package com.dreamdom.tutorials.shoppingcart;

import java.util.List;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

public class ProductAdapter extends BaseAdapter {
 
 private List<Product> mProductList;
 private LayoutInflater mInflater;
 private boolean mShowCheckbox;
 
 public ProductAdapter(List<Product> list, LayoutInflater inflater, boolean showCheckbox) {
  mProductList = list;
  mInflater = inflater;
  mShowCheckbox = showCheckbox;
 }

 @Override
 public int getCount() {
  return mProductList.size();
 }

 @Override
 public Object getItem(int position) {
  return mProductList.get(position);
 }

 @Override
 public long getItemId(int position) {
  return position;
 }

 @Override
 public View getView(int position, View convertView, ViewGroup parent) {
  final ViewItem item;
  
  if (convertView == null) {
   convertView = mInflater.inflate(R.layout.item,
     null);
   item = new ViewItem();
   
   item.productImageView = (ImageView) convertView
   .findViewById(R.id.ImageViewItem);
   
   item.productTitle = (TextView) convertView.findViewById(R.id.TextViewItem);
   
   item.productCheckbox = (CheckBox) convertView.findViewById(R.id.CheckBoxSelected);
   
   convertView.setTag(item);
  } else {
   item = (ViewItem) convertView.getTag();
  }
  
  Product curProduct = mProductList.get(position);
  
  item.productImageView.setImageDrawable(curProduct.productImage);
  item.productTitle.setText(curProduct.title);
  
  if(!mShowCheckbox) {
   item.productCheckbox.setVisibility(View.GONE);
  } else {
   if(curProduct.selected == true)
    item.productCheckbox.setChecked(true);
   else
    item.productCheckbox.setChecked(false);
  }
  
  
  return convertView;
 }
 
 
 private class ViewItem {
  ImageView productImageView;
  TextView productTitle;
  CheckBox productCheckbox;
 }

}

Step 6. Create the CatalogActivity

In this step we will look at several snippets of code individually. Don't worry about where to add them to your project--the complete CatalogActivity.java file is listed at the end of the step.

The Catalog Activity will display all the available products from the catalog. The Catalog Activity uses the ShoppingCartHelper to accomplish this.

This snippet demonstrates how to get a reference to the catalog of items.

// Obtain a reference to the product catalog
mProductList = ShoppingCartHelper.getCatalog(getResources());

When you click on an item in the catalog activity, we want it to take you to the ProductDetails Activity.

The ProductDetails Activity will be designed to be a generalized activity that can display information about any product. Since it is designed in this manner, it needs a way to figure out what product it should display details about. This is accomplished by passing along extras with the Intent that starts the activity.

Use Intent.putExtra to pass simple information between activities. In this case we will pass the index of the selected product.

Intent productDetailsIntent = new Intent(getBaseContext(),ProductDetailsActivity.class);
productDetailsIntent.putExtra(ShoppingCartHelper.PRODUCT_INDEX, position);
startActivity(productDetailsIntent);

Listed below is the full source for the Catalog Activity.

CatalogActivity.java
package com.dreamdom.tutorials.shoppingcart;

import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

public class CatalogActivity extends Activity {

 private List<Product> mProductList;

 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.catalog);

  // Obtain a reference to the product catalog
  mProductList = ShoppingCartHelper.getCatalog(getResources());
  
  // Create the list
  ListView listViewCatalog = (ListView) findViewById(R.id.ListViewCatalog);
  listViewCatalog.setAdapter(new ProductAdapter(mProductList, getLayoutInflater(), false));
  
  listViewCatalog.setOnItemClickListener(new OnItemClickListener() {

   @Override
   public void onItemClick(AdapterView<?> parent, View view, int position,
     long id) {
    Intent productDetailsIntent = new Intent(getBaseContext(),ProductDetailsActivity.class);
    productDetailsIntent.putExtra(ShoppingCartHelper.PRODUCT_INDEX, position);
    startActivity(productDetailsIntent);
   }
  });
  
  Button viewShoppingCart = (Button) findViewById(R.id.ButtonViewCart);
  viewShoppingCart.setOnClickListener(new OnClickListener() {
   
   @Override
   public void onClick(View v) {
    Intent viewShoppingCartIntent = new Intent(getBaseContext(), ShoppingCartActivity.class);
    startActivity(viewShoppingCartIntent);
   }
  });

 }
}

Step 7. Create the ProductDetails Activity

In the product details activity we will get the intent that started the activity, and extra the "extra" information that was passed along with the Intent to determine what product to display details about.

This is how to extract the extra information out of the Intent.
int productIndex = getIntent().getExtras().getInt(ShoppingCartHelper.PRODUCT_INDEX);
final Product selectedProduct = catalog.get(productIndex);

Warning!
For the sake of keeping this tutorial more straightforward and not 100 pages long we are not doing error checking in this step. In reality, a well designed program should handle cases where getExtras returns null, or when trying to get a specifically named extra returns null. In these situations a well designed app should fail gracefully. It is not always safe to assume that the Intent extras will be present.

The full source for the ProductDetails Activity is listed below.

ProductDetailsActivity.java
package com.dreamdom.tutorials.shoppingcart;

import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class ProductDetailsActivity extends Activity {
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  
  super.onCreate(savedInstanceState);
  setContentView(R.layout.productdetails);
  
  List<Product> catalog = ShoppingCartHelper.getCatalog(getResources());
  final List<Product> cart = ShoppingCartHelper.getCart();
  
  int productIndex = getIntent().getExtras().getInt(ShoppingCartHelper.PRODUCT_INDEX);
  final Product selectedProduct = catalog.get(productIndex);
  
  // Set the proper image and text
  ImageView productImageView = (ImageView) findViewById(R.id.ImageViewProduct);
  productImageView.setImageDrawable(selectedProduct.productImage);
  TextView productTitleTextView = (TextView) findViewById(R.id.TextViewProductTitle);
  productTitleTextView.setText(selectedProduct.title);
  TextView productDetailsTextView = (TextView) findViewById(R.id.TextViewProductDetails);
  productDetailsTextView.setText(selectedProduct.description);
  
  Button addToCartButton = (Button) findViewById(R.id.ButtonAddToCart);
  addToCartButton.setOnClickListener(new OnClickListener() {
   
   @Override
   public void onClick(View v) {
    
    
    cart.add(selectedProduct);
    finish();
   }
  });
  
  // Disable the add to cart button if the item is already in the cart
  if(cart.contains(selectedProduct)) {
   addToCartButton.setEnabled(false);
   addToCartButton.setText("Item in Cart");
  }
 }
 
}


Screenshot of what the product details activity looks like in the final product.


Step 8. Finally, create the ShoppingCart Activity

Some code snippets are highlighted below. The full source for the ShoppingCartActivity.java is listed at the end of the step.

The shopping cart activity will display all the items in the user's shopping cart. It will also display checkboxes next to the items, allowing the user to select multiple items at once and remove the items from the shopping cart if they so desire.

Just like the other activities, the ShoppingCartActivity uses the ShoppingCartHelper class to update the shopping cart

mCartList = ShoppingCartHelper.getCart();

The product flag includes a "selected" boolean that we use to determine whether a checkbox should be selected in the shopping cart or not.

The code snippet below will run when the shopping cart activity first starts, and is designed to make sure none of the items are checked.

// Make sure to clear the selections
for(int i=0; i<mCartList.size(); i++) {
 mCartList.get(i).selected = false;
}

When the "Remove from Cart" button is pressed, all the checked items will be removed from the cart.

Button removeButton = (Button) findViewById(R.id.ButtonRemoveFromCart);
removeButton.setOnClickListener(new OnClickListener() {
 @Override
 public void onClick(View v) {
  // Loop through and remove all the products that are selected
  // Loop backwards so that the remove works correctly
  for(int i=mCartList.size()-1; i>=0; i--) {
   
   if(mCartList.get(i).selected) {
    mCartList.remove(i);
   }
  }
  mProductAdapter.notifyDataSetChanged();
 }
});

Our layout also includes a "Proceed to Checkout" button that could have functionality added to interact with an API to process credit card transactions.

The full code for the ShoppingCartActivity is listed below.

ShoppingCartActivity.java
package com.dreamdom.tutorials.shoppingcart;

import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

public class ShoppingCartActivity extends Activity {
 
 private List<Product> mCartList;
 private ProductAdapter mProductAdapter;
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.shoppingcart);
  
  mCartList = ShoppingCartHelper.getCart();
  
  // Make sure to clear the selections
  for(int i=0; i<mCartList.size(); i++) {
   mCartList.get(i).selected = false;
  }
  
  // Create the list
  final ListView listViewCatalog = (ListView) findViewById(R.id.ListViewCatalog);
  mProductAdapter = new ProductAdapter(mCartList, getLayoutInflater(), true);
  listViewCatalog.setAdapter(mProductAdapter);
  
  listViewCatalog.setOnItemClickListener(new OnItemClickListener() {

   @Override
   public void onItemClick(AdapterView<?> parent, View view, int position,
     long id) {
    
    Product selectedProduct = mCartList.get(position);
    if(selectedProduct.selected == true)
     selectedProduct.selected = false;
    else
     selectedProduct.selected = true;
    
    mProductAdapter.notifyDataSetInvalidated();
    
   }
  });
  
  Button removeButton = (Button) findViewById(R.id.ButtonRemoveFromCart);
  removeButton.setOnClickListener(new OnClickListener() {
   @Override
   public void onClick(View v) {
    // Loop through and remove all the products that are selected
    // Loop backwards so that the remove works correctly
    for(int i=mCartList.size()-1; i>=0; i--) {
     
     if(mCartList.get(i).selected) {
      mCartList.remove(i);
     }
    }
    mProductAdapter.notifyDataSetChanged();
   }
  });
  
 }

}


Try Out the App
 

Remember to update the application manifest before running the app.
 
Once you get the app working, try going between the activities and adding items to the shopping cart, and removing them. If you have any questions or problems feel free to ask in the comments section of this post.

Another screenshot of the finished app

Some Things to Think About

This tutorial represents an introduction to creating a shopping cart for Android. There are a number of ways that this application could be improved
  • Include options to display user reviews
  • Include option to handle quantities other than one
  • Add more items to the catalog
  • Dynamically download catalog information
  • Persist shopping cart data
Some more information on that last point. In this tutorial the ShoppingCartHelper will dynamically recreate the catalog if it is null, and dynamically recreate the ShoppingCart if it is null. However, if a user adds items to the shopping cart, exits the program, or the program is killed by the Android OS, when the program starts again the cart will be empty. It is generally a good practice to try to save user information, such as items in the shopping cart, since the Android OS has the ability to kill processes in order to free up memory for other processes.

Concluding Notes

Remember, when you are designing a program that there are often many different ways to approach and solve the problem. There isn't always a right answer, but some approaches may be faster or more flexible. This tutorial probably doesn't include everything you want or need in a shopping cart, but use it as a starting point for creating one suited to your needs.

This tutorial was originally requested by a blog reader. If you questions of your own, or a request for a tutorial, don't hesitate to comment and ask!

Update - after following this tutorial, be sure to check out part 2.

------------------------------------------------------------

We create these tutorials in our free time. If you like what you see please consider buying us a cup of coffee so we can keep creating useful material. Click on the image below to make a donation via Paypal.

137 comments:

  1. Great write up! Will do this tutorial soon. Will you be improving this shopping cart?

    Cheers!

    ReplyDelete
  2. Thanks for the comment Te!

    I plan on revisiting the shopping cart and adding some more features, but don't have a definite date of when this will be, as I'm currently very busy.

    ReplyDelete
  3. hiii.. In your shopping cart example,i want edittext on the view Shopping cart page only,when i place,and i change, i am not able to remove from cart please help me its very urgentt...

    ReplyDelete
  4. Hi Mitesh,

    I guess it is about time to write a follow up tutorial on this shopping cart. I will include this feature in it.

    Thanks.

    ReplyDelete
  5. when I want to change this online shopping cart (via internet) what
    Can I change to each class java android.

    ReplyDelete
  6. Hey montas,

    Thanks for the comment. I am currently working on part two of the shopping cart tutorial, where you can edit the quantity of items.

    I will then start writing a tutorial demonstrating how you can add actual payments.

    Thanks for the comments! It helps me know that people are interested.

    ReplyDelete
  7. hi BusinessIsGood,
    i tried with your tutorial in my application for restaurant but i have some problems can you help me? :(
    my problem is in the cart shopping duplicate my meal(plat) so when I cliqued in item of meal 1 and put him in cart shopping and I cliqued in item 2 of meal so, I get in cart shopping 2 item with same name.
    if you like I will give you my code to help me.

    ReplyDelete
  8. A follow up tutorial has been posted. This tutorial modifies the shopping cart to handle quantities of products. Let me know if this helps.

    ReplyDelete
    Replies
    1. Hi is it possible to teach me how your shopping cart can be dynamically changed with an external database?

      Delete
  9. Awesome! I'll be looking forward to your payment method tutorial.

    ReplyDelete
  10. Hi,
    I am new to the android apps development and this tutorial provided a good start for me. I followed this tutorial and created all layout xml files under res folder and created all class files under src folder. I was wondering how to update the application manifest file and MainActivity.java file accordingly to run this app. Appreciate, if you can provide me the complete steps for updating manifest file and MainActivity.java file. If possible please send me complete source code zip file for this tutorial. I really want this app to implement. Please kindly help me with this. Looking forward to hear from you soon. Thanks.

    ReplyDelete
    Replies
    1. Hi,
      I am new to the android apps development and this tutorial provided a good start for me. I followed this tutorial and created all layout xml files under res folder and created all class files under src folder. I was wondering how to update the application manifest file and MainActivity.java file accordingly to run this app. Appreciate, if you can provide me the complete steps for updating manifest file and MainActivity.java file. If possible please send me complete source code zip file for this tutorial. I really want this app to implement. Please kindly help me with this. Looking forward to hear from you soon. Thanks.

      Delete
  11. Hi,I am new to the android apps development and this tutorial provided a good start for me. I followed this tutorial and created all layout xml files under res folder and created all class files under src folder. I was wondering how to update the application manifest file and MainActivity.java file accordingly to run this app. Appreciate, if you can provide me the complete steps for updating manifest file and MainActivity.java file. If possible please send me complete source code zip file for this tutorial. I really want this app to implement. Please kindly help me with this. Looking forward to hear from you soon.
    Thanks,

    ReplyDelete
  12. This comment has been removed by the author.

    ReplyDelete
  13. Thanks for this tutorial. I have a problem, it will not do anything, can you post what is supposed to be in the manifest file?

    Thanks

    ReplyDelete
  14. Awesome! i was looking a such a tutorial, where i can get the src code for this..

    ReplyDelete
  15. hi BusinessIsGood :)
    how are u ? fine?
    can u have any example for iphone Shopping Cart Tutorial ?

    ReplyDelete
  16. This comment has been removed by the author.

    ReplyDelete
  17. wheeww.. im so confuse.. i have follow all the step correctly.. and no error occurs .. but when i run it said the program stopped unexpectedly.. i wonder what happened.. any help?

    ReplyDelete
  18. btw.. what is it means by "Remember to update the application manifest before running the app."??
    and how to update?

    ReplyDelete
  19. I think this sample is Great!

    I was wondering if its posible to have an options menu with each item showing a different category set of books.

    I've tried to do so,but with no success (i'm new to android)..
    When i changed the array of products,the previous selections gone off!

    ReplyDelete
  20. Hi! I was wondering how can I update the manifest? what do I have to put?
    please I really need this :(

    ReplyDelete
    Replies
    1. hi ve u bin able to update the manifest

      Delete
  21. Great Tutorial I had searched so many tutorials but i dint get this much of(Clear) information........
    Thank you

    ReplyDelete
  22. Hi, i was wondering how do i update the AndroidManifest

    ReplyDelete
  23. Android app manifest is:
    < application android:icon="@drawable/icon" android:label="@string/app_name">
    < activity android:name=".CatalogActivity"
    android:label="@string/app_name">
    < intent-filter>
    < action android:name="android.intent.action.MAIN" />
    < category android:name="android.intent.category.LAUNCHER" />
    < / intent-filter>
    < / activity>
    < activity android:name="ProductDetailsActivity">< /activity>
    < activity android:name="ShoppingCartActivity">< /activity>

    < /application>

    ReplyDelete
  24. I tried to use your code directly without any modifications. But I am getting compiler errors in @override tags used by you. For example in ProductAdapter.java class, you have used extends BaseAdapter, which doesnt have a definition of getCount(). And when i use @override tag, it gives me error because of it. But when i try using SimpleAdapter instead of BaseAdapter, it gives me error in ProductAdapter Constructor.

    Any assistance with this issue will be highly appreciated.

    Thanks in Advance
    Ashish

    ReplyDelete
  25. Ashish,

    You can simply delete the @override tags. Often times they are added by Eclipse when you autocomplete methods, and are not strictly necessary as they only provide extra information to the compiler.

    You might be getting compiler errors based on what version of the JDK you are using. It is not truly overriding a method, but implementing a method from the interface adapter.

    http://developer.android.com/reference/android/widget/Adapter.html

    ReplyDelete
  26. Great Tutorial!! This is exactly what we needed!!

    ReplyDelete
  27. hi it's nice tutorial, i get problem, i can't combine the shopping cart to my application, i made my application catalog list with xml, can you help me how to combine that? thank you

    ReplyDelete
  28. Awesome tutorial, works great. thanks for sharing!!! :)

    ReplyDelete
  29. hi,i have copied this tutorial as it is..but i am encountering following errors
    1- On R.id.(something) that it cannot be resolved.
    2- In productdetails.xml file
    android:src="@drawable/deadoralive"
    is showing error
    "Error: No resource found that matches the
    given name (at 'src' with value
    '@drawable/deadoralive')."

    I am newbie to android, can any one help me out that how to resolve these issues?
    I will be really very grateful for this kind favor.

    ReplyDelete
  30. Thanks for the great tut. It just what I needed for my dissertation.

    ReplyDelete
  31. @ Anonymous, you need to create a drawable file in the 'res' folder to store the image of 'deadoralive' which you have to copy from here and drag it into the drawable folder.

    ReplyDelete
  32. I am getting errors on certain public method like this one. @Override
    public int getCount() {
    return mProductList.size();
    }

    It tells me to remove @Override. Can someone advise what might be the problem.

    ReplyDelete
    Replies
    1. Hi Simtha,
      If you see a couple of comments above we give a solution. But here it is again:

      You can simply delete the @override tags. Often times they are added by Eclipse when you autocomplete methods, and are not strictly necessary as they only provide extra information to the compiler.

      You might be getting compiler errors based on what version of the JDK you are using. It is not truly overriding a method, but implementing a method from the interface adapter.

      http://developer.android.com/reference/android/widget/Adapter.html

      Delete
  33. @jill, Thanks alot!! it resolved the issue.

    ReplyDelete
  34. I find a lot of @Override errors are due to compiling in the wrong compliance level.

    Under Project | Properties | Java Compiler
    There is a "Compiler compliance level:" that defaults to 1.5

    Change this to 1.6 and the @Overrides will work properly.

    For my version of eclipse, i had to change the default compliance level by unchecking the "enable project specific settings" in that same window and selecting the "configure workspace settings" and changing that compliance level to 1.6 (from 1.7 which did not seem to work with android)

    That caused all future projects to have the correct compliance level.

    ReplyDelete
    Replies
    1. Thank you for helping answer the questions :)
      This is a very good recommendation!

      Delete
  35. ty for the tutorial. it is simple and easy to follow but i face a problem..i am able to compile and run it but the problem is the image of the books do not appear..

    ReplyDelete
    Replies
    1. Hi! Did you download the images of the books and place them in the correct place?

      "Make sure to download these images, and place them in your drawable folder. Make sure not to name the image of the book "switch" "switch.png" This is because the name is used in the generated class R, and switch is a java keyword."

      Also, Jill responded to a similar question in the comments above:
      "@ Anonymous, you need to create a drawable file in the 'res' folder to store the image of 'deadoralive' which you have to copy from here and drag it into the drawable folder."

      Hope that helps :)

      Delete
    2. I did download few images but I change the name

      Delete
  36. hi! the tutorial is just perfect. it is not hard to understand. I am able to run it but only the 1st page ( catalog page) . what ever items i click no effect. can i ask what you mean by updating the android manifest xml.

    ReplyDelete
    Replies
    1. Hi,
      We made a post on how to update the android manifest.
      You can find the answer in this post:
      http://www.androiddom.com/2012/02/how-to-modify-androidmanifest.html

      Delete
    2. thnkx a lot for replying so fast.

      Delete
  37. i am a beginner in android programming. I need to create shopping cart application combined with budget. I have follow your tutorial step by step and update the manifest file and able to run without error found. The problem is no matter what I click in the first page of the application like the item or the button, it keep popping out the application has stopped unexpectedly. I am wondering what is the cause of it and how to solve it

    ReplyDelete
    Replies
    1. Hi Ed,
      We are going to need a little bit more details in order to help you on this one. Why don't you write us an email to theandroiddom[at]gmail[dot]com and tell us what tool are you using and which Android SDK version and then we can try pin pointing that error. We are a little bit busy these next couple of days but as soon as we have time we'll help you.

      Delete
    2. thnkz. I just send an email to you

      Delete
    3. Ed, I just checked our email and we haven't received anything.

      Delete
    4. really? I have sent twice to this email theandroiddom@gmail.com

      Delete
  38. I just sent again using hotmail this time. Before that , I was using yahoo to send, perhaps gone to junk

    ReplyDelete
    Replies
    1. Hi Ed! We got it. Sorry for the delay. We'll get to it as soon as we can.

      Delete
  39. i notice that inside the program there is double price but i never see the price of the items appear. Can I ask why? If i want to make the price of the item to appear, how to do that?

    ReplyDelete
    Replies
    1. Hi! We are working on part 3 of the tutorial where will address this question :).

      Delete
    2. really? around when will the part 3 appear?

      Delete
    3. Hi! It should be online in the next couple days.

      Delete
    4. Hey Sil
      Can you please tell me how to get title of all books in shopping cart when we press proceed to checkout button
      please help
      thank you

      Delete
  40. Hey.. how do I calculate the total price ?

    ReplyDelete
    Replies
    1. hmm i also want to know how. I know normally is totalprice =+ price

      Delete
    2. Hi! Thanks for your question. We are working on the tutorial part III that will answer this question. Stay tuned.

      Delete
  41. Wow great stuff. Part 1 really has helped me understand what is going on with different layouts and activities. My only suggestions would be to post your code in a way that has the syntax highlighted and also post the Manifest for people who are not as familiar with android. Dont mean to complain at all because this is one of the best tutorials i have found!

    ReplyDelete
  42. Nice tutorial v clear explaination. will the tutorial part III coming out this week?

    ReplyDelete
    Replies
    1. Hi, this tutorial is superb perfect for me i have run it but i don't want to apply on listview and i removed catalog Activity... i mean i have created different activities in this project and each activity has Add to Cart button when i click on button its performing the Add to cart functionality but after that i want to continue my Add to cart process means when i click another activity button then session shoulde be maintain...any help

      Delete
  43. anyone send me the full coding i facing some error ....

    ReplyDelete
  44. anyone send me the full coding i facing some error ....tzerkang@gmail.com

    ReplyDelete
  45. hmm i have tried this and the second part but I dun get it why doesn't the price appear and I would like to ask if I want to get the list of item from shopping mall database online instead of hard coded in, what are the code that I should change and it is hard to do it?

    ReplyDelete
  46. hi great guide!

    any tutorial for using xml file containing catalog information for dynamic product catalog?

    ReplyDelete
  47. Hello

    I have a problem in the generation of the R class, I created the drawable folder in the 'res' folder and copy the images but still does not work.

    Anyone can help me

    thank you

    ReplyDelete
    Replies
    1. Did u change the names ? of images? The names shud b same as used in code & all in lowercase and shud b full name like image.png along with the extension

      Delete
    2. I have not changed the name of the images. It's all right

      Delete
  48. Hi, this tutorial is superb perfect for me i have run it but i don't want to apply on listview and i removed catalog Activity... i mean i have created different activities in this project and each activity has Add to Cart button when i click on button its performing the Add to cart functionality but after that i want to continue my Add to cart process means when i click another activity button then session shoulde be maintain...any help

    ReplyDelete
  49. Dear Sir

    I want to get images and product details from server... please help me how i can use your code to get this ???

    ReplyDelete
  50. Hi,

    I tried this tutorial for my app, It works great. But only thing is when i log-out or close and again use the app and check out whats in cart then there are no products in the cart.

    Please can you help me out with the code to retain products that are added earlier in the cart even when i use the app next time.
    I need this code for my app at the earliest. Please reply soon.

    ReplyDelete
  51. Thank you so much.....you solved my problem........:)

    ReplyDelete
  52. i have created drawable folder in res folder i copied d images bt still its nt working can anyone help me please..i need it urgent

    ReplyDelete
  53. Y ITS SHOWING ERROR??? PLEASE HELP ME

    ReplyDelete
  54. i can't download this app.....i am android trainy..
    plz give link to download this aap

    ReplyDelete
  55. how to direct proceed to checkkout button to a different layout

    ReplyDelete
  56. For some new android developers, this is the androidmanifest that works...

    NTB: "shoppingcart" is the project name, you should replace this with your project name

    --------------------------------------------------------------------





















    ReplyDelete
  57. hi BusinessIsGood,
    may i know your email address so that i can get help when doing your tutorials?
    my email is waisoon_vanguard@hotmail.com
    with regards,
    student in help

    ReplyDelete
  58. i have done exactly as you have told, and updated the android manifest, but i cannot run it,it gives error like "sorry prorgam stopped unexpectedly force close" plz help me , my emailid = haq.rakibul@gmail.com

    ReplyDelete
  59. hi, i am encountering an error
    android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.shoppingcart/com.example.shoppingcart.ShoppingCartActivity}; have you declared this activity in your AndroidManifest.xml?

    and i have mentioned it in my android manifest file.. plzz help

    ReplyDelete
  60. Love your shopping cart example. Works great but I was wondering how I can open the productdetails.xml as a dialog when a selection is made in the listviewcatalog. A dialog seems to give the repeated product details more visual transition. Thanks.

    ReplyDelete
  61. This comment has been removed by the author.

    ReplyDelete
  62. Hey Im also create shopping cart example/. but i dnt know how to add items in my cart. If you dont mid. please help me to add items to cart. when i was clicking the items

    ReplyDelete
  63. Dear Admin,
    Kindly add "update quantity" field, so that qty * cost = Bill Amount in this tutorial

    ReplyDelete
  64. Dear Admin,
    Thank You sir .. every thing is clearly defined.

    ReplyDelete
  65. This comment has been removed by the author.

    ReplyDelete
  66. how to retrieve the product by using sqlite or external database in webserver? Please help thanks.

    ReplyDelete
  67. i have done this application but unfornately unable to open the application... i have changed the andriodmainfest but unable to open. please help thanks....
    with regards
    rajdeep

    ReplyDelete
  68. THANK YOU ! works like a charm !

    ReplyDelete
  69. hi, how are u sir ? first i want to thank you for this great tut :)
    i just want to ask u what if i want to connect my shopping cart to MYSQL database which i already have for my website?

    ReplyDelete
    Replies
    1. having d same prob ... i wanna connect wid my DB bt can not :(

      Delete
  70. I don't have any error in the code but when I run the application in emulator I get this error "unfortunately app has stopped"

    ReplyDelete
  71. This comment has been removed by the author.

    ReplyDelete
  72. I want to retrieve the product list from the database which section of the code will i have to modify to suit this functionality

    ReplyDelete
  73. Top 10 Marketplace (websites) where you can buy android app source code.

    ReplyDelete
  74. i copy pest all the codes correctly but still fails to run in just starting... and what to changes to be made in menifest?

    ReplyDelete
    Replies
    1. insert all the activities on ur androidmanifest.xml bro

      Delete
    2. Can you show the exact menifest file

      Delete
  75. This comment has been removed by the author.

    ReplyDelete
  76. many thanks to u dude. this help me so much. thankkssss :D

    ReplyDelete
  77. i'm new in android .. and thanks to this tutorial it's very helpful to me. i want to ask how can i get all the data in the shopping cart ? like title and qty input by the user .. and show them in other activity

    ReplyDelete
  78. This comment has been removed by the author.

    ReplyDelete
  79. can you give me a tutorial video ? because it is easier for me, iam newer

    ReplyDelete
  80. to my email nguyenphongphu2013@gmail.com
    thanks

    ReplyDelete
  81. This comment has been removed by the author.

    ReplyDelete
  82. This comment has been removed by the author.

    ReplyDelete
  83. Thanks for sharing..
    www.epulsewebinfo.com

    ReplyDelete
  84. Can u help me to implement remove from cart feature

    ReplyDelete
  85. The remove button does not work. When I select items and press it, it just unselects those items instead of removing them. Could you please help me fix it?

    ReplyDelete
  86. pls tell me how to update the android manifest and activity main

    ReplyDelete
  87. thank a lot... i exactly needed this...tried many ways, but failed...hope this works

    ReplyDelete
  88. IS this app has a status of online android app ??

    ReplyDelete
  89. IS this app has a status of online android app ??

    ReplyDelete
  90. Hii..!!
    This is a wonderful example , please make the code downloadable ..
    Thanks in advance..!!

    ReplyDelete
  91. hey i m getting app:predexdebug error i dont know what to do

    ReplyDelete
  92. can u help ? http://stackoverflow.com/questions/32363261/update-a-listview-using-sharedpreference

    ReplyDelete
  93. how to android onlineshopping application img list view

    ReplyDelete
  94. GREAT JOB SIR,
    But can I have one REQUEST sir. Can you continue the project with the function of "PROCEED TO CHECKOUT". pLEASE I need your help. Part 4 PLsssss

    ReplyDelete
  95. I am getting error foll errors :
    1)Error:The content of elements must consist of well-formed character data or markup.
    2)Error:Cannot read packageName from C:\Users\aditi_nayak\AndroidStudioProjects\catalog\app\src\main\AndroidManifest.xml


    please help.

    ReplyDelete
  96. what about proceed to checkout code????I want that code

    ReplyDelete
  97. what about proceed to checkout code????I want that code

    ReplyDelete
  98. Sir how to replace image with Image url?

    ReplyDelete
  99. How can I change the size of one section in the list view? There's a lot of extra space and I want to remove it.

    ReplyDelete
  100. i want to create a check order page.where all the details that will be in the cart will be available...how to do that?

    ReplyDelete
  101. U jus saved me sir . . . .Thnx a lot. . . .Awesome Tutorial

    ReplyDelete
  102. sir, your tutorial is very help full to me...thank you soo much..

    ReplyDelete
  103. i need shippingMethod Cart how can i solve the promblem of android

    ReplyDelete