Like Us on Facebook

How to Create a Shopping Cart Application in Android

Hello everyone, as per specific demand I am going to share  how to create a simple Shopping cart Application in Android ,it basically give you concept how to create Shopping Cart  application  front end. Later in upcomming post I will explain complete Realtime Shopping Cart tutorial using database.

Read More
How to create Mobile Application of a Website
How to make Barcode Scanning Application in Android Studio
How to Integrate Payment Gateway to the Android Application
How to create your first Android game

Prerequisites:
  • JDK 6.0 or above
  • Android Studio

Steps to follow: 
  1. Create a New Android Studio project name CartExample
  2. Select minimum  API level 16 so it will run 82.6% of Android device that are Active on google Play then click next.
  3. Select Blank Activity and your Activity Name is MainActivity click next > click finish.
  4. Inside MainActivity  write the following code;
 
public class MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final LinearLayout layout = (LinearLayout)findViewById(R.id.linearMain);
final Button btn = (Button)findViewById(R.id.second);
final Controller ct = (Controller) getApplicationContext();
ModelProducts products = null;
for(int i= 1; i<=7;i++){
int Price = 15+ i;
products = new ModelProducts("Product Item" +i, "Description"+i, Price);
ct.setProducts(products);
}
int productsize = ct.getProductArraylistsize();
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
for (int j=0;j< productsize;j++){
String pName = ct.getProducts(j).getProductName();
int pPrice = ct.getProducts(j).getProductPrice();
LinearLayout la = new LinearLayout(this);
la.setOrientation(LinearLayout.HORIZONTAL);
TextView tv = new TextView(this);
tv.setText(" "+pName+" ");
la.addView(tv);
TextView tv1 = new TextView(this);
tv1.setText("$"+pPrice+" ");
la.addView(tv1);
final Button btn1 = new Button(this);
btn1.setId(j+1);
btn1.setText("Add to Cart");
btn1.setLayoutParams(params);
final int index = j;
btn1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Log.i("TAG", "index:"+index);
ModelProducts productsObject = ct.getProducts(index);
if(!ct.getCart().CheckProductInCart(productsObject)){
btn1.setText("Item Added");
ct.getCart().setProducts(productsObject);
Toast.makeText(getApplicationContext(), "New CartSize:" +ct.getCart().getCartsize(),Toast.LENGTH_LONG).show(); 
}else{
Toast.makeText(getApplicationContext(), "Products"+(index+1)+"Already Added",Toast.LENGTH_LONG ).show();
}
}
});
la.addView(btn1);
layout.addView(la);
}
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent in = new Intent(getBaseContext(),Screen2.class);
startActivity(in);
}
});
}
}


5. Now it will ask you to create a new Class  Controller.java .  write the folowing code inside it.
 
public class Controller extends Application{
private ArrayList<ModelProducts> myproducts = new ArrayList<ModelProducts>();
private ModelCart myCart = new ModelCart();
public ModelProducts getProducts(int pPosition){
return myproducts.get(pPosition);
}
public void  setProducts(ModelProducts products){
myproducts.add(products);
}
public ModelCart getCart(){
return myCart;
}
public int  getProductArraylistsize(){
return myproducts.size();
}
}
 
 
6. You  will ask  to create a new Class  ModelProducts.java . write the folowing code inside it.
 
public class ModelProducts {
private String productName;
private String productDesc;
private int productPrice;
public ModelProducts(String productName,String productDesc,int productPrice){
this.productName = productName;
this.productDesc = productDesc;
this.productPrice = productPrice;
}

public String getProductName(){
return productName;
}

public String getProductDesc(){
return productDesc;
}

public int getProductPrice(){
return productPrice;
}
}



7. After writing the  code  under  Controller.java  you will be asked to create ModelCart.java . write the following code inside it.

public class ModelCart {
private ArrayList<ModelProducts> cartItems = new ArrayList<ModelProducts>();
public ModelProducts getProducts(int position){
return cartItems.get(position);
}
public void setProducts(ModelProducts Products){
cartItems.add(Products);
}
public int getCartsize(){

return cartItems.size();
}

public boolean CheckProductInCart(ModelProducts aproduct){
return cartItems.contains(aproduct);
}
}


8. Now Create a new Class Screen1.java  And write the following  Code inside  it.
 
 public class Screen1 extends Activity{
 /* (non-Javadoc)
 * @see android.app.Activity#onCreate(android.os.Bundle)
*/
 
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.screen1);
TextView showCartContent = (TextView)findViewById(R.id.showcart);
final Button btn3 = (Button)findViewById(R.id.third);
final Controller ct = (Controller)getApplicationContext();
final int CartSize = ct.getCart().getCartsize();
String show = "";
if(CartSize >0){
for(int i=0;i<CartSize;i++){
String pName = ct.getCart().getProducts(i).getProductName();
int  pPrice = ct.getCart().getProducts(i).getProductPrice();
String pDisc = ct.getCart().getProducts(i).getProductDesc();
show += "Product Name:"+pName+" "+"Price : "+pPrice+""+"Discription : "+pDisc+""+   

 "-----------------------------------";
}
} else
show = "There is no Items in Shopping Cart";
showCartContent.setText(show);
btn3.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
if(CartSize >0){
Intent  i = new Intent(getBaseContext(),Screen2.class)
startActivity(i);
}else {

Toast.makeText(getApplicationContext(), "Shopping Cart is Empty", Toast.LENGTH_LONG).show()
}
});
}

/* (non-Javadoc)
* @see android.app.Activity#onDestroy() */

@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
}
}


9. And  also create   class Screen2.Java  and write the following code inside  it.
 
public class Screen2 extends Activity{
/* (non-Javadoc)
* @see android.app.Activity#onCreate(android.os.Bundle) */

@Override
protected void onCreate(Bundle savedInstanceState) {

// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.screen2);
TextView showCartContent = (TextView)findViewById(R.id.showcart);
final Controller ct = (Controller)getApplicationContext();
final int CartSize = ct.getCart().getCartsize();
String show = "";
for(int i=0;i<CartSize;i++){
String pName = ct.getCart().getProducts(i).getProductName();
int  pPrice = ct.getCart().getProducts(i).getProductPrice();
String pDisc = ct.getCart().getProducts(i).getProductDesc();
show += "Product Name:"+pName+" "+"Price : "+pPrice+""+"Discription : "+pDisc+""+    "-----------------------------------";
}
showCartContent.setText(show);
}
}
 

10. And  below are the required xml files  for project.
     main.xml
 
<LinearLayout 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"
 
android:orientation="vertical"
>

<TextView
 
android:layout_width="wrap_content"
 
android:layout_height="wrap_content"
 
android:layout_centerHorizontal="true"
 
android:layout_centerVertical=
"true"
 
android:textColor="#898989"
 
android:textStyle="bold"
 
android:textSize="18sp"
 
android:text="Shopping Cart Example" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/linearMain"
>
</LinearLayout>  
<Button android:id="@+id/second"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Checkout"
/>
</LinearLayout>


screen1.xml
 
<LinearLayout 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"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#898989"
android:textStyle="bold"
android:textSize="16sp"
android:text="Shopping Cart Content" />
<TextView
android:id="@+id/showCart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
/>
<Button android:id="@+id/third"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Payment"
/>


screen2.xml
 
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical" >
<TextView
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:textColor="#988989"
 android:textStyle="bold"
 android:textSize="18sp"
 android:text="Thank you for purchasing product"
/>

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

</LinearLayout>

Posted By UandBlog

UandBlog is a Global Leading source of Finance, Health, Lifestyle, Technology, Gaming and Programing and other information, it is a fastest growing blog for all , for latest post or information like us on facebook , follow on Twitter, Google+ and Pinterest.

Comments ( 4 )

Himanshu Patni:
if you are using intent to Screen2.class in mainActivity then you will never read at Screen1 this is not the code you used in your running apk file in emulator.
Admin:
ohh,, may be
Rajeshwari Biradar:
Thank you very much
Admin:
thanx
Rajeshwari Biradar:
Thank you very much
Admin:
thanx
Jayaraj Narayanan P:
Hi I want complete Realtime Shopping Cart tutorial using database.
Admin:
i will make it soon

    Leave a comment..

    Click to login.

    Subscribe Latest Information

    Most Popular Posts

    You May Like Also