Android Tutorial

Software Setup and Configuration

Android Studio

File Structure

Components

Core Topics

Layout

View

Button

Intent and Intent Filters

Toast

RecyclerView

Fragments

Adapters

Other UI Component

Image Loading Libraries

Date and Time

Material Design

Bars

Working with Google Maps

Chart

Animation

Database

Advance Android

Jetpack

Architecture

App Publish

App Monetization

How to add Custom Spinner in android?

A Spinner in Android provides a quick way to select one value from a set. If you want to use custom layouts for the spinner items or the dropdown views, you can create custom adapters. Here's a step-by-step guide on creating a custom spinner:

1. Prepare Your Data:

Let's assume you have a list of custom objects:

data class CustomItem(val icon: Int, val text: String)

You can have a sample list:

val items = listOf(
    CustomItem(R.drawable.icon1, "Item 1"),
    CustomItem(R.drawable.icon2, "Item 2"),
    // ... add more items
)

2. Create the Custom Spinner Item Layout:

Let's create spinner_item.xml in the res/layout directory:

<?xml version="1.0" encoding="utf-8"?>
<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"
    android:gravity="center_vertical">

    <ImageView
        android:id="@+id/icon"
        android:layout_width="24dp"
        android:layout_height="24dp"
        android:layout_marginEnd="8dp"/>

    <TextView
        android:id="@+id/text"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"/>
</LinearLayout>

3. Create the Custom Adapter:

class CustomSpinnerAdapter(context: Context, private val items: List<CustomItem>) :
    BaseAdapter(), SpinnerAdapter {

    private val inflater: LayoutInflater = LayoutInflater.from(context)

    override fun getCount(): Int = items.size

    override fun getItem(position: Int): CustomItem = items[position]

    override fun getItemId(position: Int): Long = position.toLong()

    override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
        val view: View
        val viewHolder: ViewHolder

        if (convertView == null) {
            view = inflater.inflate(R.layout.spinner_item, parent, false)
            viewHolder = ViewHolder(view)
            view.tag = viewHolder
        } else {
            view = convertView
            viewHolder = view.tag as ViewHolder
        }

        val item = getItem(position)
        viewHolder.icon.setImageResource(item.icon)
        viewHolder.text.text = item.text

        return view
    }

    private class ViewHolder(view: View) {
        val icon: ImageView = view.findViewById(R.id.icon)
        val text: TextView = view.findViewById(R.id.text)
    }
}

4. Attach the Adapter to the Spinner:

In your activity or fragment:

val spinner: Spinner = findViewById(R.id.spinner)
val adapter = CustomSpinnerAdapter(this, items)
spinner.adapter = adapter

5. Handle Spinner Item Selection:

spinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
    override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
        val selectedItem = items[position]
        // Handle the selected item
    }

    override fun onNothingSelected(parent: AdapterView<*>?) {
        // Handle the case where nothing is selected
    }
}

With these steps, you'll have a custom spinner where each item has an icon and a text. You can further customize the layout, appearance, and behavior to fit your specific needs.

  1. Creating a custom layout for spinner items in Android:

    • Description: Design a custom layout for spinner items to have a unique appearance.
    • Code:
      <!-- res/layout/custom_spinner_item.xml -->
      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:orientation="horizontal">
      
          <ImageView
              android:id="@+id/customImageView"
              android:layout_width="24dp"
              android:layout_height="24dp"
              android:src="@drawable/ic_custom_icon" />
      
          <TextView
              android:id="@+id/customTextView"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="Custom Item" />
      
      </LinearLayout>
      
  2. Implementing a custom adapter for a custom spinner in Android:

    • Description: Extend BaseAdapter or ArrayAdapter to create a custom adapter for your custom spinner.
    • Code:
      class CustomSpinnerAdapter(private val context: Context, private val items: List<CustomItem>) : BaseAdapter() {
          // Adapter implementation
      }
      
  3. Handling item selection events in a custom spinner:

    • Description: Set up a listener to respond to item selection events in your custom spinner.
    • Code:
      val customSpinner = findViewById<Spinner>(R.id.customSpinner)
      customSpinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
          override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
              // Handle item selection
          }
      
          override fun onNothingSelected(parent: AdapterView<*>?) {
              // Handle no item selected
          }
      }