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
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:
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 )
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>
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) } }
In your activity or fragment:
val spinner: Spinner = findViewById(R.id.spinner) val adapter = CustomSpinnerAdapter(this, items) spinner.adapter = adapter
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.
Creating a custom layout for spinner items in Android:
<!-- 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>
Implementing a custom adapter for a custom spinner in Android:
BaseAdapter
or ArrayAdapter
to create a custom adapter for your custom spinner.class CustomSpinnerAdapter(private val context: Context, private val items: List<CustomItem>) : BaseAdapter() { // Adapter implementation }
Handling item selection events in a custom spinner:
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 } }