android shopping cart source code :
Add to Cart And Display in Recyclerview :
in this project we will show when user click selected product add to cart button, product data save in the cart section. after the user clicks the cart icon, the user can see the selected item lists display in RecyclerView.
- we will fetch data from the server, using Api ,with the help of Retrofit library and show that data on recycler Views .
Step 1 - Create a retrofit instance :
This Java class is used to send requests to an API. We specify the URL that contains the data required and use the
Retrofit Builder
class.package com.rajesh.newpnproject.Network;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
public class RetrofitClient2 {
public static final String BASE_URL = "https://pastebin.com/";
private static Retrofit retrofit = null;
public static Retrofit getClient() {
if (retrofit == null){
retrofit = new Retrofit.Builder().baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}
}
Step 2 – Define the endpoints:
Endpoints usually are defined inside an Interface class. An endpoint refers to the path where information is obtained. Our endpoint is ‘raw/jiEvzswz’. Since our aim is to obtain information from the API, we will be using the@GET
annotation since we are making a Get request.package com.rajesh.newpnproject.InterFace;
import com.rajesh.newpnproject.DeshBordModel.Food;
import retrofit2.Call;
import retrofit2.http.GET;
public interface RetrofitInterface {
@GET("raw/jiEvzswz")
abstract public Call<Food> getFoods();
}Step 3 - Create a model class:
Next, we will create a model class that will contain the objects from the JSON,AndStore all data :package com.rajesh.newpnproject.DeshBordModel;
import com.google.gson.annotations.SerializedName;
import java.util.List;
public class Food {
@SerializedName("success")
private boolean success;
@SerializedName("message")
private String message;
@SerializedName("popular_food_count")
private int popularFoodCount;
@SerializedName("regular_food_count")
private int regularFoodCount;
@SerializedName("popular_food")
private List<GeneralFood> popularFood = null;
@SerializedName("regular_food")
private List<GeneralFood> regularFood = null;
public boolean isSuccess() {
return success;
}
public void setSuccess(boolean success) {
this.success = success;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public int getPopularFoodCount() {
return popularFoodCount;
}
public void setPopularFoodCount(int popularFoodCount) {
this.popularFoodCount = popularFoodCount;
}
public int getRegularFoodCount() {
return regularFoodCount;
}
public void setRegularFoodCount(int regularFoodCount) {
this.regularFoodCount = regularFoodCount;
}
public List<GeneralFood> getPopularFood() {
return popularFood;
}
public void setPopularFood(List<GeneralFood> popularFood) {
this.popularFood = popularFood;
}
public List<GeneralFood> getRegularFood() {
return regularFood;
}
public void setRegularFood(List<GeneralFood> regularFood) {
this.regularFood = regularFood;
}
}Step 4 - Create a model class Again:
Here We Store Data which is come in the form of List ,we show that data on recycler Views:package com.rajesh.newpnproject.DeshBordModel;
import com.google.gson.annotations.SerializedName;
public class GeneralFood {
@SerializedName("id")
private int id;
@SerializedName("title")
private String title;
@SerializedName("price")
private double price;
@SerializedName("description")
private String description;
@SerializedName("image")
private String image;
@SerializedName("rating")
private int rating;
@SerializedName("calory")
private int calory;
@SerializedName("fat")
private int fat;
@SerializedName("sodium")
private int sodium;
@SerializedName("protein")
private int protein;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
public int getRating() {
return rating;
}
public void setRating(int rating) {
this.rating = rating;
}
public int getCalory() {
return calory;
}
public void setCalory(int calory) {
this.calory = calory;
}
public int getFat() {
return fat;
}
public void setFat(int fat) {
this.fat = fat;
}
public int getSodium() {
return sodium;
}
public void setSodium(int sodium) {
this.sodium = sodium;
}
public int getProtein() {
return protein;
}
public void setProtein(int protein) {
this.protein = protein;
}
}Step 5- Create a Adapter class:
package com.rajesh.newpnproject.DeshBordAdapter;
import static com.rajesh.newpnproject.codingActivity.AddItemsOnCartActivity.cartFoods;
import static com.rajesh.newpnproject.codingActivity.AddItemsOnCartActivity.cartUpdate;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.rajesh.newpnproject.DeshBordModel.GeneralFood;
import com.rajesh.newpnproject.R;
import com.rajesh.newpnproject.codingActivity.Details;
import com.squareup.picasso.Picasso;
import java.util.List;
public class VerticalAdapter extends RecyclerView.Adapter<VerticalAdapter.VerticalViewHolder> {
private List<GeneralFood> regularFoods;
private Context context;
public static class VerticalViewHolder extends RecyclerView.ViewHolder{
LinearLayout verticalLayout;
TextView regularTitle;
TextView regularPrice;
ImageView regularImage;
ImageButton regularPlus;
public VerticalViewHolder(View itemView) {
super(itemView);
verticalLayout = itemView.findViewById(R.id.vertical_parent_layout);
regularTitle = itemView.findViewById(R.id.regular_food_title);
regularImage = itemView.findViewById(R.id.regular_food_pc);
regularPrice = itemView.findViewById(R.id.regular_food_price);
regularPlus = itemView.findViewById(R.id.regular_food_plus);
}
}
public VerticalAdapter(List<GeneralFood> regularFoods,
int vertical_recyclerview, Context context){
this.context = context;
this.regularFoods = regularFoods;
}
@NonNull
@Override
public VerticalAdapter.VerticalViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.recyclerview_vertical, parent, false);
return new VerticalViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull final VerticalAdapter.VerticalViewHolder holder,
@SuppressLint("RecyclerView") final int position) {
holder.regularTitle.setText(regularFoods.get(position).getTitle());
holder.regularPrice.setText((Double.toString((regularFoods.get(position).getPrice()))) + " Taka");
Picasso.get().load(regularFoods.get(position).getImage()).fit().into(holder.regularImage);
holder.regularPlus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
cartFoods.add(regularFoods.get(position));
cartUpdate();
}});
holder.verticalLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(context, Details.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra("foodTitle", regularFoods.get(position).getTitle());
intent.putExtra("foodPrice", regularFoods.get(position).getPrice());
intent.putExtra("foodCalories", regularFoods.get(position).getCalory());
intent.putExtra("foodDescription", regularFoods.get(position).getDescription());
intent.putExtra("foodFat", regularFoods.get(position).getFat());
intent.putExtra("foodSodium", regularFoods.get(position).getSodium());
intent.putExtra("foodPotassium", regularFoods.get(position).getProtein());
intent.putExtra("foodImage",regularFoods.get(position).getImage());
context.startActivity(intent);
}
});}
@Override
public int getItemCount() {
return regularFoods.size();
}
}Step 6– Designing the UI for VerticalAdapter :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/vertical_parent_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="@+id/regular_food_pc"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_centerHorizontal="true"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="20dp"
android:src="@mipmap/ic_launcher" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_marginTop="10dp"
android:orientation="horizontal">
<LinearLayout
android:id="@+id/linear_regular"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:orientation="vertical">
<TextView
android:id="@+id/regular_food_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Title"
android:textColor="@android:color/black"
android:textSize="14sp" />
<TextView
android:id="@+id/regular_food_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Price"
android:textColor="@android:color/black"
android:textSize="14sp" />
</LinearLayout>
<ImageButton
android:id="@+id/regular_food_plus"
android:layout_width="25dp"
android:layout_height="25dp"
android:src="@drawable/ic_plus_blue"
android:background="@android:color/transparent"
android:clickable="true"
android:layout_marginRight="10dp"/>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
</LinearLayout>Step 7- Create a Adapter class Again:
package com.rajesh.newpnproject.DeshBordAdapter;
import static com.rajesh.newpnproject.codingActivity.AddItemsOnCartActivity.cartFoods;
import static com.rajesh.newpnproject.codingActivity.AddItemsOnCartActivity.cartUpdate;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.rajesh.newpnproject.DeshBordModel.GeneralFood;
import com.rajesh.newpnproject.R;
import com.rajesh.newpnproject.codingActivity.Details;
import com.squareup.picasso.Picasso;
import java.util.List;
public class HorizontalAdapter extends RecyclerView.Adapter<HorizontalAdapter.HorizontalViewHolder>{
private List<GeneralFood> popularFoods;
private Context context;
public static class HorizontalViewHolder extends RecyclerView.ViewHolder{
LinearLayout horizontalLayout;
ImageView popularImage;
TextView popularTitle;
TextView popularPrice;
ImageButton popularPlus;
public HorizontalViewHolder(View itemView) {
super(itemView);
horizontalLayout = itemView.findViewById(R.id.horizontal_parent_layout);
popularImage = itemView.findViewById(R.id.popular_food_pc);
popularPrice = itemView.findViewById(R.id.popular_food_price);
popularTitle = itemView.findViewById(R.id.popular_food_title);
popularPlus = itemView.findViewById(R.id.popular_food_plus);
}
}
public HorizontalAdapter(List<GeneralFood> popularFoods,
int horizontal_recyclerview, Context context){
this.context = context;
this.popularFoods = popularFoods;
}
@NonNull
@Override
public HorizontalAdapter.HorizontalViewHolder
onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.recyclerview_horizontal, parent, false);
return new HorizontalViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull HorizontalAdapter.HorizontalViewHolder holder,
@SuppressLint("RecyclerView") final int position) {
holder.popularTitle.setText(popularFoods.get(position).getTitle());
holder.popularPrice.setText((Double.toString((popularFoods.get(position).getPrice()))) + " Taka");
Picasso.get().load(popularFoods.get(position).getImage()).fit().into(holder.popularImage);
holder.popularPlus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
cartFoods.add(popularFoods.get(position));
cartUpdate();
}});
holder.horizontalLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(context, Details.class);
intent.putExtra("foodTitle", popularFoods.get(position).getTitle());
intent.putExtra("foodPrice", popularFoods.get(position).getPrice());
intent.putExtra("foodCalories", popularFoods.get(position).getCalory());
intent.putExtra("foodDescription", popularFoods.get(position).getDescription());
intent.putExtra("foodFat", popularFoods.get(position).getFat());
intent.putExtra("foodSodium", popularFoods.get(position).getSodium());
intent.putExtra("foodPotassium", popularFoods.get(position).getProtein());
intent.putExtra("foodImage",popularFoods.get(position).getImage());
context.startActivity(intent);
}
});
}
@Override
public int getItemCount() {
return popularFoods.size();
}
}Step 8– Designing the UI for HorizontalAdapter:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/horizontal_parent_layout">
<ImageView
android:layout_width="120dp"
android:layout_height="120dp"
android:src="@drawable/ic_launcher_background"
android:id="@+id/popular_food_pc"
android:layout_marginTop="15dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="10dp"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Title"
android:layout_marginLeft="15dp"
android:layout_marginTop="10dp"
android:id="@+id/popular_food_title"
android:textColor="@android:color/black"
android:textSize="8sp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Price"
android:layout_marginLeft="15dp"
android:layout_marginBottom="15dp"
android:textSize="8sp"
android:textColor="@android:color/black"
android:id="@+id/popular_food_price"/>
</LinearLayout>
<ImageButton
android:layout_width="20dp"
android:layout_height="20dp"
android:src="@drawable/ic_plus_blue"
android:id="@+id/popular_food_plus"
android:background="@android:color/transparent"
android:layout_marginRight="15dp"
android:clickable="true"
android:layout_gravity="center" />
</LinearLayout>
</RelativeLayout>
</LinearLayout>
Step 9– Create Main Activity class :
package com.rajesh.newpnproject.codingActivity;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.core.view.MenuItemCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.rajesh.newpnproject.DeshBordAdapter.HorizontalAdapter;
import com.rajesh.newpnproject.DeshBordAdapter.VerticalAdapter;
import com.rajesh.newpnproject.DeshBordModel.Food;
import com.rajesh.newpnproject.DeshBordModel.GeneralFood;
import com.rajesh.newpnproject.InterFace.RetrofitInterface;
import com.rajesh.newpnproject.MainActivity;
import com.rajesh.newpnproject.Network.RetrofitClient;
import com.rajesh.newpnproject.Network.RetrofitClient2;
import com.rajesh.newpnproject.R;
import java.util.ArrayList;
import java.util.List;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
public class AddItemsOnCartActivity extends AppCompatActivity {
RecyclerView recyclerViewHorizontal;
RecyclerView recyclerViewVertical;
public static TextView tv;
public static List<GeneralFood> cartFoods = new ArrayList<>();
public Toolbar toolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_items_on_cart);
toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("Foodish");
toolbar.setTitleTextColor(0xFFFFFFFF);
cartUpdate();
recyclerViewHorizontal = findViewById(R.id.horizontal_recyclerview);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this,
LinearLayoutManager.HORIZONTAL, false);
recyclerViewHorizontal.setLayoutManager(linearLayoutManager);
recyclerViewVertical = findViewById(R.id.vertical_recyclerview);
LinearLayoutManager linearLayoutManager2 = new LinearLayoutManager(this,
LinearLayoutManager.VERTICAL, false);
recyclerViewVertical.setLayoutManager(linearLayoutManager2);
RetrofitInterface retrofitService = RetrofitClient2.getClient().create(RetrofitInterface.class);
Call<Food> call = retrofitService.getFoods();
call.enqueue(new Callback<Food>() {
@Override
public void onResponse(Call<Food> call, Response<Food> response) {
List<GeneralFood> popularFoods = response.body().getPopularFood();
recyclerViewHorizontal.setAdapter(new HorizontalAdapter(popularFoods,
R.layout.recyclerview_horizontal, AddItemsOnCartActivity.this));
List<GeneralFood> regularFoods = response.body().getRegularFood();
recyclerViewVertical.setNestedScrollingEnabled(false);
recyclerViewVertical.setAdapter(new VerticalAdapter(regularFoods,
R.layout.recyclerview_vertical, getApplicationContext()));
}
@Override
public void onFailure(Call<Food> call, Throwable t) {
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
cartUpdate();
MenuItem item = menu.findItem(R.id.action_addcart);
MenuItemCompat.setActionView(item, R.layout.cart_count_layout);
RelativeLayout notifCount = (RelativeLayout) MenuItemCompat.getActionView(item);
tv = notifCount.findViewById(R.id.hotlist_hot);
View view = notifCount.findViewById(R.id.hotlist_bell);
cartUpdate();
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent myIntent = new Intent(AddItemsOnCartActivity.this,
CartActivity.class);
startActivity(myIntent);
}});
return true;
}
public static void cartUpdate() {
if (tv != null && cartFoods != null)
tv.setText(Integer.toString(cartFoods.size()));
}
@Override
protected void onResume() {
invalidateOptionsMenu();
super.onResume();
}
@Override
public void onBackPressed() {
finish();
}
}Step 10– Create Main Activity.XML:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<include
android:id="@+id/toolbar"
layout="@layout/toolbar" />
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="10dp"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="10dp"
android:layout_marginTop="65dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:text="Popular"
android:textColor="@android:color/black"
android:textSize="20sp" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/horizontal_recyclerview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
</androidx.recyclerview.widget.RecyclerView>
</LinearLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:text="Regular"
android:textColor="@android:color/black"
android:textSize="20sp" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/vertical_recyclerview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginBottom="10dp">
</androidx.recyclerview.widget.RecyclerView>
</LinearLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
</androidx.core.widget.NestedScrollView>
</FrameLayout>Step 11– Create Main Menu :
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/order_button"
android:title="Order"
app:showAsAction="always" />
</menu>Step 12– Create confirmation Menu Again:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_addcart"
android:title="Cart"
app:actionLayout="@layout/cart_count_layout"
app:showAsAction="always"
/>
</menu>Now we want show our data in any Activity after click add to cart Button :Step 1– Create Cart Activity.java:
package com.rajesh.newpnproject.codingActivity;
import static com.rajesh.newpnproject.codingActivity.AddItemsOnCartActivity.cartFoods;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.rajesh.newpnproject.DeshBordAdapter.CartAdapter;
import com.rajesh.newpnproject.DeshBordModel.GeneralFood;
import com.rajesh.newpnproject.R;
import java.util.List;
public class CartActivity extends AppCompatActivity {
RecyclerView recyclerviewCart;
static TextView cartPrice;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cart);
Toolbar mToolbar = findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
getSupportActionBar().setTitle("Food Cart");
mToolbar.setTitleTextColor(0xFFFFFFFF);
mToolbar.setNavigationIcon(R.drawable.back_to_home_button);
mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
CartActivity.super.onBackPressed();
}
});
cartPrice = findViewById(R.id.cart_price);
cartPrice.setText(Double.toString(grandTotal(cartFoods)));
recyclerviewCart = findViewById(R.id.cart_recyclerview);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this,
LinearLayoutManager.VERTICAL, false);
recyclerviewCart.setLayoutManager(linearLayoutManager);
recyclerviewCart.setNestedScrollingEnabled(false);
recyclerviewCart.setAdapter(new CartAdapter(cartFoods,this));
// recyclerviewCart.setAdapter(new CartAdapter(cartFoods,));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_confirmation, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.order_button){
Intent intent = new Intent(CartActivity.this,
OrderActivity.class);
startActivity(intent);
}
return super.onOptionsItemSelected(item);
}
public static int grandTotal(List<GeneralFood> cartFoods){
int totalPrice = 0;
for(int i = 0 ; i < cartFoods.size(); i++) {
totalPrice += cartFoods.get(i).getPrice();
}
return totalPrice;
}
public static void priceAdjust(){
cartPrice.setText(Double.toString(grandTotal(cartFoods)));
}
}Step 2– Create Cart Activity.XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
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:orientation="vertical">
<include
android:id="@+id/toolbar"
layout="@layout/toolbar" />
<androidx.core.widget.NestedScrollView
android:layout_height="match_parent"
android:layout_width="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/cart_recyclerview">
</androidx.recyclerview.widget.RecyclerView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:gravity="center"
android:layout_marginBottom="20dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Total Price: "
android:textColor="@android:color/black"
android:textSize="19sp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/cart_price"
android:textSize="20sp"
android:textColor="@android:color/black"/>
</LinearLayout>
</LinearLayout>
</androidx.core.widget.NestedScrollView>
</LinearLayout>Step 3– Create Adapter :
package com.rajesh.newpnproject.DeshBordAdapter;
import static com.rajesh.newpnproject.codingActivity.AddItemsOnCartActivity.cartFoods;
import static com.rajesh.newpnproject.codingActivity.AddItemsOnCartActivity.cartUpdate;
import static com.rajesh.newpnproject.codingActivity.CartActivity.grandTotal;
import static com.rajesh.newpnproject.codingActivity.CartActivity.priceAdjust;
import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.RecyclerView;
import com.rajesh.newpnproject.DeshBordModel.GeneralFood;
import com.rajesh.newpnproject.MainActivity;
import com.rajesh.newpnproject.R;
import com.rajesh.newpnproject.codingActivity.AddItemsOnCartActivity;
import com.squareup.picasso.Picasso;
import java.util.List;
public class CartAdapter extends RecyclerView.Adapter<CartAdapter.CartViewHolder> {
private Context context;
private CartAdapter mCartAdapter;
@NonNull
@Override
public CartAdapter.CartViewHolder onCreateViewHolder
(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recyclerview_cart,
parent, false);
return new CartViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull final CartViewHolder holder,
@SuppressLint("RecyclerView") final int position) {
holder.cartTitle.setText(cartFoods.get(position).getTitle());
holder.cartPrice.setText((Double.toString((cartFoods.get(position).getPrice()))) + " Taka");
Picasso.get().load(cartFoods.get(position).getImage()).fit().into(holder.cartPicture);
holder.cartDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
GeneralFood item = cartFoods.get(position);
cartFoods.remove(item);
notifyItemRemoved(position);
notifyItemRangeChanged(position, cartFoods.size());
grandTotal(cartFoods);
priceAdjust();
cartUpdate();
}});
}
@Override
public int getItemCount() {
return cartFoods.size();
}
public class CartViewHolder extends RecyclerView.ViewHolder {
ImageView cartPicture;
TextView cartTitle;
TextView cartPrice;
CardView cartParentLayout;
ImageButton cartDelete;
public CartViewHolder(View itemView) {
super(itemView);
cartPicture = itemView.findViewById(R.id.cart_food_pic);
cartTitle = itemView.findViewById(R.id.cart_food_title);
cartPrice = itemView.findViewById(R.id.cart_food_price);
cartParentLayout = itemView.findViewById(R.id.cart_parent_layout);
cartDelete = itemView.findViewById(R.id.cart_food_delete);
}
}
public CartAdapter(List<GeneralFood> cartFoods, Context context){
this.context = context;
AddItemsOnCartActivity.cartFoods = cartFoods;
}
}Step 4– Create Layout for Adapter:
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/cart_parent_layout"
android:layout_margin="15dp">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/horizontal_parent_layout">
<ImageView
android:layout_width="match_parent"
android:layout_height="200dp"
android:src="@drawable/ic_launcher_background"
android:id="@+id/cart_food_pic"
android:layout_marginTop="15dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="10dp"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Title: "
android:layout_marginTop="10dp"
android:textColor="@android:color/black"
android:textSize="13sp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CartFoodTitle"
android:layout_marginTop="10dp"
android:id="@+id/cart_food_title"
android:textColor="@android:color/black"
android:textSize="13sp"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Price: "
android:layout_marginBottom="15dp"
android:textSize="13sp"
android:textColor="@android:color/black" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CartFoodPrice"
android:id="@+id/cart_food_price"
android:layout_marginBottom="15dp"
android:textSize="13sp"
android:textColor="@android:color/black" />
</LinearLayout>
</LinearLayout>
<ImageButton
android:layout_width="20dp"
android:layout_height="20dp"
android:src="@drawable/ic_delete_black_24dp"
android:id="@+id/cart_food_delete"
android:background="@android:color/transparent"
android:layout_marginRight="25dp"
android:clickable="true"
android:layout_gravity="center" />
</LinearLayout>
</RelativeLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>Step 5– Create Activity for show cart Details :
data come here from Main Activity by using intent we send and receive data :package com.rajesh.newpnproject.codingActivity;
import static com.rajesh.newpnproject.codingActivity.AddItemsOnCartActivity.cartFoods;
import static com.rajesh.newpnproject.codingActivity.AddItemsOnCartActivity.cartUpdate;
import static com.rajesh.newpnproject.codingActivity.AddItemsOnCartActivity.tv;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.core.view.MenuItemCompat;
import com.rajesh.newpnproject.DeshBordModel.GeneralFood;
import com.rajesh.newpnproject.R;
import com.squareup.picasso.Picasso;
public class Details extends AppCompatActivity {
GeneralFood mFood = new GeneralFood();
TextView foodTitle, foodPrice, foodCalories, foodDescription, foodFat, foodSodium, foodProtein;
ImageView foodImage;
ImageButton foodDetailsPlus;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_details);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("Details");
toolbar.setTitleTextColor(0xFFFFFFFF);
toolbar.setNavigationIcon(R.drawable.back_to_home_button);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Details.super.onBackPressed();
tv.setText(Integer.toString(cartFoods.size()));
}
});
foodTitle = findViewById(R.id.food_title);
foodPrice = findViewById(R.id.food_price);
foodCalories = findViewById(R.id.food_calories);
foodDescription = findViewById(R.id.food_description);
foodFat = findViewById(R.id.food_fat);
foodSodium = findViewById(R.id.food_sodium);
foodProtein = findViewById(R.id.food_protein);
foodImage = findViewById(R.id.food_image);
foodDetailsPlus = findViewById(R.id.regular_food_plus_details);
foodTitle.setText(getIntent().getStringExtra("foodTitle"));
foodPrice.setText(Double.toString(getIntent().getDoubleExtra("foodPrice", 0)));
foodCalories.setText(Integer.toString(getIntent().getIntExtra("foodCalories", 0)));
foodDescription.setText(getIntent().getStringExtra("foodDescription"));
Picasso.get().load(getIntent().getStringExtra("foodImage")).fit().into(foodImage);
foodFat.setText(Integer.toString(getIntent().getIntExtra("foodFat", 0)));
foodSodium.setText(Integer.toString(getIntent().getIntExtra("foodSodium", 0)));
foodProtein.setText(Integer.toString(getIntent().getIntExtra("foodProtein", 0)));
mFood.setTitle(getIntent().getStringExtra("foodTitle"));
mFood.setCalory((getIntent().getIntExtra("foodCalories", 0)));
mFood.setPrice((getIntent().getDoubleExtra("foodPrice", 0)));
mFood.setDescription((getIntent().getStringExtra("foodDescription")));
mFood.setImage((getIntent().getStringExtra("foodImage")));
mFood.setFat((getIntent().getIntExtra("foodFat", 0)));
mFood.setSodium((getIntent().getIntExtra("foodSodium", 0)));
mFood.setProtein((getIntent().getIntExtra("foodProtein", 0)));
mFood.setId(0);
mFood.setRating(0);
foodDetailsPlus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
cartFoods.add(mFood);
tv.setText(Integer.toString(cartFoods.size()));
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
MenuItem item = menu.findItem(R.id.action_addcart);
MenuItemCompat.setActionView(item, R.layout.cart_count_layout);
RelativeLayout notifCount = (RelativeLayout) MenuItemCompat.getActionView(item);
View view = notifCount.findViewById(R.id.hotlist_bell);
tv = notifCount.findViewById(R.id.hotlist_hot);
cartUpdate();
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent myIntent = new Intent(Details.this, CartActivity.class);
startActivity(myIntent);
}});
return true;
}
}Step 6– Create Activity Details 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">
<include
android:id="@+id/toolbar"
layout="@layout/toolbar" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="230dp">
<ImageView
android:id="@+id/food_image"
android:layout_width="match_parent"
android:layout_height="205dp"
android:src="@drawable/ic_launcher_background" />
<ImageButton
android:id="@+id/regular_food_plus_details"
android:layout_width="55dp"
android:layout_height="55dp"
android:background="@android:color/transparent"
android:clickable="true"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:src="@drawable/ic_plus_blue"
android:layout_alignParentRight="true" />
</RelativeLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/food_image"
android:layout_marginLeft="10dp"
android:layout_marginTop="8dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Title: "
android:textColor="@android:color/black" />
<TextView
android:id="@+id/food_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="FoodTitle"
android:textColor="@android:color/black" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="8dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Price: "
android:textColor="@android:color/black" />
<TextView
android:id="@+id/food_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="FoodPrice"
android:textColor="@android:color/black" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="8dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Calories: "
android:textColor="@android:color/black" />
<TextView
android:id="@+id/food_calories"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="FoodCalories"
android:textColor="@android:color/black" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="8dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Description: "
android:textColor="@android:color/black" />
<TextView
android:id="@+id/food_description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="FoodDescription"
android:textColor="@android:color/black"
android:layout_marginRight="10dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="8dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fat: "
android:textColor="@android:color/black" />
<TextView
android:id="@+id/food_fat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="FoodFat"
android:textColor="@android:color/black" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="8dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sodium: "
android:textColor="@android:color/black" />
<TextView
android:id="@+id/food_sodium"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="FoodSodium"
android:textColor="@android:color/black" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="10dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Protein: "
android:textColor="@android:color/black" />
<TextView
android:id="@+id/food_protein"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="FoodProtein"
android:textColor="@android:color/black" />
</LinearLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>conclusion :
we show our data on recyclerViews in list form, when user click any items send that data ,
Another Activity with the help of intent :
here we give option for add to cart ,we give 1 ImageButton ,when user l