Create your own Android eCommerce App!
Table of Contents
- Introduction
- Adding the Add to Cart Button
- Managing the Cart List
- Getting the Time for the Purchase
- Storing the Products in the Cart List
- Creating the Admin View
- Testing the App
- Conclusion
Introduction
In this video tutorial, we will be adding functionality to allow users to add products to their cart list. We will start by creating an "Add to Cart" button and then proceed to manage the cart list. We will also learn how to get the time of the purchase, store the products in the cart list, Create an admin view, and test the app. So, let's get started!
Adding the Add to Cart Button
To add the "Add to Cart" button, we need to remove the existing floating action button from our activity and replace it with a simple button. The new button should have a match parent width, be colored primary dark, and have a margin of 15dp from all sides. The button should also be aligned to the parent bottom. We will set an ID for this button to give it a unique property. In our onCreate
method, we will set a click listener for this button.
Managing the Cart List
To manage the cart list, we will create a method that will be triggered when the "Add to Cart" button is clicked. In this method, we will first get the Current time of the purchase using the Calendar
class. We will store the current date and time in a variable. Next, we will create a database reference and a HashMap to store the product information. The HashMap will contain the product ID, name, price, quantity, and discount. We will use the database reference to store this information in the Firebase database. We will also store the product information under a specific user and their phone number for easy identification.
Getting the Time for the Purchase
To get the time for the purchase, we will use the Calendar
class to retrieve the current date and time. We will format the date to include the month, day, and year. We will also format the time to include the hour, minute, and AM/PM indicator. We will store the date and time in separate STRING variables.
Storing the Products in the Cart List
Once we have the product information, including the product ID, name, price, quantity, and discount, we will create a database reference and store the information using a HashMap. We will store the data under a specific user and their phone number, as well as create a unique ID for the product. We will use the updateChildren
method to update the database with the new data. If the task is successful, we will display a message to the user and redirect them to the home page.
Creating the Admin View
To create the admin view, we will store the same product information under a separate node called "admin view". This will allow the admin to identify the specific user and their products in the cart list. We will use the same HashMap and database reference to store this information. If the task is successful, we will display a message to the admin.
Testing the App
To test our app, we will add some products to the cart list. We will add a product and specify the quantity. We will then check the Firebase database to ensure that the product information is stored correctly under the user and admin view.
Conclusion
In this tutorial, we learned how to add an "Add to Cart" button, manage the cart list, get the time for the purchase, and store the products in the Firebase database. We also created an admin view and tested the app. By implementing these functionalities, users can now add products to their cart list and proceed to place final orders. Stay tuned for the next tutorial where we will create a catalyst activity to display the cart list products and allow users to place their final orders.
Highlights
- Adding functionality to allow users to add products to cart list
- Creating a user-friendly "Add to Cart" button
- Managing the cart list and getting the time of purchase
- Storing product information in Firebase database
- Creating an admin view for easier product identification
- Testing the app for proper functionality
FAQ
Q: Can users add multiple products to their cart list?
A: Yes, users can add multiple products to their cart list.
Q: Can admins identify which user added which products to their cart list?
A: Yes, admins can view the user-specific cart lists and identify the products added by each user.
Q: Can users modify the quantity or remove products from the cart list?
A: Yes, users can modify the quantity or remove products from their cart list.
Q: Is it possible to place a final order directly from the cart list?
A: No, users need to proceed to the catalyst activity to place their final orders.
Q: Will the app display any messages upon successful addition to the cart list?
A: Yes, both users and admins will receive messages upon successful addition to the cart list.