RamaSkills
We Build Software That Ships

Creating a Home Page for a Furniture Store App in Kotlin

By RamaSkills

Learn how to design and implement the home page of a furniture store app using Kotlin, featuring a clean layout and essential components.

In this tutorial, we will focus on building the home page of a furniture store app using Kotlin. This part of the series will guide you through setting up a user-friendly interface that showcases various furniture items, enhancing the overall user experience. Let’s delve into the essential components and functionalities needed for an effective home page.

Setting Up Your Project

Before we start coding, ensure that you have Android Studio installed and your Kotlin environment set up. Begin by creating a new project or opening your existing furniture store app project. For this tutorial, we will be primarily working within the MainActivity and its associated XML layout files.

Designing the Home Page Layout

The first step in creating the home page is to design the layout. Open the activity_main.xml file, where we will define the user interface. A typical layout for a furniture store app might include a toolbar, a search bar, and a grid or list view to display the furniture items.

Here’s a simple example of how you can structure the XML layout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@color/colorPrimary"/>

    <EditText
        android:id="@+id/search_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/toolbar"
        android:hint="Search for furniture"
        android:padding="16dp" />

    <RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/search_bar"
        android:padding="4dp"/>
</RelativeLayout>

In this layout:

  • A Toolbar is included at the top to serve as the app's header.
  • An EditText component acts as a search bar for users to find specific furniture items.
  • A RecyclerView is used to display a list of furniture products dynamically.

Implementing the RecyclerView

To populate the RecyclerView with furniture data, we need to create a model class for the furniture items. This class will represent the data structure of each item in our store.

data class FurnitureItem(
    val id: Int,
    val name: String,
    val price: Double,
    val imageResId: Int
)

Next, create an adapter for the RecyclerView. This adapter will bind the data from the list of furniture items to the view holder.

class FurnitureAdapter(private val items: List<FurnitureItem>) : RecyclerView.Adapter<FurnitureAdapter.FurnitureViewHolder>() {

    class FurnitureViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val name: TextView = itemView.findViewById(R.id.item_name)
        val price: TextView = itemView.findViewById(R.id.item_price)
        val image: ImageView = itemView.findViewById(R.id.item_image)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): FurnitureViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.furniture_item, parent, false)
        return FurnitureViewHolder(view)
    }

    override fun onBindViewHolder(holder: FurnitureViewHolder, position: Int) {
        val currentItem = items[position]
        holder.name.text = currentItem.name
        holder.price.text = "${currentItem.price}"
        holder.image.setImageResource(currentItem.imageResId)
    }

    override fun getItemCount() = items.size
}

This adapter consists of:

  • A ViewHolder that holds references to the item views.
  • Methods to create and bind view holders with the corresponding data.

Populating Data into RecyclerView

With the adapter in place, we can now populate the RecyclerView with sample furniture data. In your MainActivity, initialize the RecyclerView and set the adapter.

class MainActivity : AppCompatActivity() {
    private lateinit var recyclerView: RecyclerView
    private lateinit var adapter: FurnitureAdapter

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        recyclerView = findViewById(R.id.recycler_view)
        recyclerView.layoutManager = GridLayoutManager(this, 2)

        val furnitureItems = listOf(
            FurnitureItem(1, "Sofa", 299.99, R.drawable.sofa_image),
            FurnitureItem(2, "Chair", 89.99, R.drawable.chair_image),
            FurnitureItem(3, "Table", 199.99, R.drawable.table_image)
        )

        adapter = FurnitureAdapter(furnitureItems)
        recyclerView.adapter = adapter
    }
}

In this code snippet, we set a GridLayoutManager to display the furniture items in a grid format. A sample list of furniture items is created, and the adapter is assigned to the RecyclerView.

Final Touches

To enhance the user experience, consider adding click listeners to the items in the RecyclerView, allowing users to view more details about each furniture item. Additionally, you can implement a real search functionality that filters the items based on user input in the search bar.

Conclusion

In this part of the tutorial series, we successfully designed and implemented the home page for a furniture store app using Kotlin. We created a user-friendly layout, set up a RecyclerView, and populated it with sample data. This foundational work paves the way for further development, including navigation and additional features.

For a detailed visual walkthrough, Watch the full tutorial on YouTube.

Watch the video tutorial: https://www.youtube.com/watch?v=9HJQUasC2SM