Note - Double Click to Copy Code Contact Us!
Posts

Online Shopping Android Application using Java

Tech Doubility
Online Shopping Android Application using Java


XML CODE-
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="vertical"
  6. android:padding="16dp"
  7. tools:context=".MainActivity">
  8.  
  9. <!-- Place your UI components here -->
  10.  
  11. </LinearLayout>
  12.  
Java Code
  1. import android.os.Bundle;
  2. import android.support.v7.app.AppCompatActivity;
  3.  
  4. public class MainActivity extends AppCompatActivity {
  5.  
  6. @Override
  7. protected void onCreate(Bundle savedInstanceState) {
  8. super.onCreate(savedInstanceState);
  9. setContentView(R.layout.activity_main);
  10.  
  11. // Initialize UI components and handle user interactions here
  12. }
  13. }
  14.  
Product Item Layout (list_item_product.xml): XML Code
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:layout_width="match_parent"
  3. android:layout_height="wrap_content"
  4. android:orientation="horizontal"
  5. android:padding="8dp">
  6.  
  7. <ImageView
  8. android:id="@+id/product_image"
  9. android:layout_width="64dp"
  10. android:layout_height="64dp"
  11. android:src="@drawable/product_placeholder" />
  12.  
  13. <TextView
  14. android:id="@+id/product_name"
  15. android:layout_width="wrap_content"
  16. android:layout_height="wrap_content"
  17. android:text="Product Name"
  18. android:textSize="16sp" />
  19.  
  20. <TextView
  21. android:id="@+id/product_price"
  22. android:layout_width="wrap_content"
  23. android:layout_height="wrap_content"
  24. android:text="Product Price"
  25. android:textSize="16sp"
  26. android:layout_marginStart="8dp" />
  27.  
  28. </LinearLayout>
  29.  
Java code (Product List Activity)
  1. import android.os.Bundle;
  2. import android.support.v7.app.AppCompatActivity;
  3. import android.widget.ListView;
  4.  
  5. import java.util.ArrayList;
  6. import java.util.List;
  7.  
  8. public class ProductListActivity extends AppCompatActivity {
  9.  
  10. private ListView productList;
  11. private ProductListAdapter adapter;
  12.  
  13. @Override
  14. protected void onCreate(Bundle savedInstanceState) {
  15. super.onCreate(savedInstanceState);
  16. setContentView(R.layout.activity_product_list);
  17.  
  18. productList = findViewById(R.id.product_list);
  19. adapter = new ProductListAdapter(this, getProductList());
  20. productList.setAdapter(adapter);
  21. }
  22.  
  23. private List<Product> getProductList() {
  24. List<Product> products = new ArrayList<>();
  25.  
  26. // Add your products here or fetch them from an API/database
  27.  
  28. return products;
  29. }
  30. }
  31.  
Java Code (Product List Adapter)
  1. import android.content.Context;
  2. import android.view.LayoutInflater;
  3. import android.view.View;
  4. import android.view.ViewGroup;
  5. import android.widget.ArrayAdapter;
  6. import android.widget.ImageView;
  7. import android.widget.TextView;
  8.  
  9. import java.util.List;
  10.  
  11. public class ProductListAdapter extends ArrayAdapter<Product> {
  12.  
  13. private LayoutInflater inflater;
  14.  
  15. public ProductListAdapter(Context context, List<Product> products) {
  16. super(context, 0, products);
  17. inflater = LayoutInflater.from(context);
  18. }
  19.  
  20. @Override
  21. public View getView(int position, View convertView, ViewGroup parent) {
  22. if (convertView == null) {
  23. convertView = inflater.inflate(R.layout.list_item_product, parent, false);
  24. }
  25.  
  26. Product product = getItem(position);
  27.  
  28. ImageView productImage = convertView.findViewById(R.id.product_image);
  29. TextView productName = convertView.findViewById(R.id.product_name);
  30. TextView product = convertView.findViewById(R.id.product_price);
  31. // Set product details to the views
return convertView
  1. }
  2. }
  3.  

Java code ((Product Model (Product.java)):
  1. public class Product {
  2. private String name;
  3. private double price;
  4. // Add additional fields as needed
  5.  
  6. public Product(String name, double price) {
  7. this.name = name;
  8. this.price = price;
  9. }
  10.  
  11. // Add getter and setter methods
  12. }
  13.  
Please note that this is a simplified example, and you'll need to modify and expand it to suit your specific needs. Additionally, you'll need to provide your own product images and handle user interactions like clicking on a product item to view details or adding items to the shopping cart.

Remember to update the package names and resource IDs in the code to match your project's structure.

Post a Comment

Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.
Site is Blocked
Sorry! This site is not available in your country.