XML CODE-
Java Code
<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" android:padding="16dp" tools:context=".MainActivity"> <!-- Place your UI components here --> </LinearLayout>
Product Item Layout (list_item_product.xml): XML Code
import android.os.Bundle; import android.support.v7.app.AppCompatActivity; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Initialize UI components and handle user interactions here } }
Java code (Product List Activity)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:padding="8dp"> <ImageView android:id="@+id/product_image" android:layout_width="64dp" android:layout_height="64dp" android:src="@drawable/product_placeholder" /> <TextView android:id="@+id/product_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Product Name" android:textSize="16sp" /> <TextView android:id="@+id/product_price" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Product Price" android:textSize="16sp" android:layout_marginStart="8dp" /> </LinearLayout>
Java Code (Product List Adapter)
import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.widget.ListView; import java.util.ArrayList; import java.util.List; public class ProductListActivity extends AppCompatActivity { private ListView productList; private ProductListAdapter adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_product_list); productList = findViewById(R.id.product_list); adapter = new ProductListAdapter(this, getProductList()); productList.setAdapter(adapter); } private List<Product> getProductList() { List<Product> products = new ArrayList<>(); // Add your products here or fetch them from an API/database return products; } }
import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.ImageView; import android.widget.TextView; import java.util.List; public class ProductListAdapter extends ArrayAdapter<Product> { private LayoutInflater inflater; public ProductListAdapter(Context context, List<Product> products) { super(context, 0, products); inflater = LayoutInflater.from(context); } @Override public View getView(int position, View convertView, ViewGroup parent) { if (convertView == null) { convertView = inflater.inflate(R.layout.list_item_product, parent, false); } Product product = getItem(position); ImageView productImage = convertView.findViewById(R.id.product_image); TextView productName = convertView.findViewById(R.id.product_name); TextView product = convertView.findViewById(R.id.product_price); // Set product details to the viewsreturn convertView
} }
public class Product { private String name; private double price; // Add additional fields as needed public Product(String name, double price) { this.name = name; this.price = price; } // Add getter and setter methods }
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.