5th February, 2023
Hello android developer, Android spinner is a view similar to the dropdown list which is used to select one option from the list of options. This tutorial shows you how to clear spinner value in kotlin android. Android programming swap elements of spinner.
Step 1: Create New Project
Create a new project in Android Studio from File ⇒ New Project and select Empty Activity from the templates.
Step 2: Add spinner to the activity layout
Open the activity layout file and add the following code.
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_vertical" android:orientation="horizontal" android:padding="10dp"> <RelativeLayout android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginStart="3dp" android:layout_marginEnd="3dp" android:layout_weight="30" android:background="@drawable/spinner_bg" android:padding="2dp"> <Spinner android:id="@+id/yearSpinner" android:layout_width="match_parent" android:layout_height="wrap_content" android:entries="@array/years" android:textAlignment="center" /> </RelativeLayout> <RelativeLayout android:id="@+id/layoutDateSpinner1" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginStart="3dp" android:layout_marginEnd="3dp" android:layout_weight="45" android:background="@drawable/spinner_bg" android:padding="2dp"> <androidx.appcompat.widget.AppCompatSpinner android:id="@+id/weekSpinner" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginStart="5dp" android:layout_marginEnd="5dp" android:textAlignment="center" /> </RelativeLayout> <androidx.appcompat.widget.AppCompatButton android:id="@+id/buttonSearch" android:layout_width="0dp" android:layout_height="match_parent" android:layout_marginStart="5dp" android:layout_marginEnd="5dp" android:layout_weight="25" android:background="@drawable/button_bg" android:minHeight="25dp" android:text="@string/search" android:textColor="@android:color/white" android:textSize="12sp" /> </LinearLayout> <TextView android:id="@+id/hintTextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="selected week: "/> </RelativeLayout>
<resources> <string name="app_name">SpinnerTest1</string> <string name="search">Search</string> <string-array name="years"> <item>2019</item> <item>2020</item> <item>2021</item> <item>2022</item> <item>2023</item> <item>2024</item> <item>2025</item> <item>2026</item> <item>2027</item> <item>2028</item> <item>2029</item> <item>2030</item> </string-array> </resources>
Step 3: Styling android spinner
We are adding some style to the spinners to enhance their beauty!
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <corners android:radius="10dp" /> <solid android:color="@android:color/transparent" /> <stroke android:width="0.5dp" android:color="#656565" /> </shape>
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <corners android:radius="15dp"/> <solid android:color="#2196F3" /> </shape>
Step 4: Update Main Activity
Next, Open the Activity and bind data to the year Spinner dropdown. In this example we are binding all the weekend data to the week spinner.
package com.codestringz.spinnertest1 import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import android.view.View import android.widget.* import java.text.DateFormat import java.text.SimpleDateFormat import java.util.* import kotlin.collections.ArrayList class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) initUI() } private fun initUI() { //find views val yearSpinner = findViewById<Spinner>(R.id.yearSpinner) val weekSpinner = findViewById<Spinner>(R.id.weekSpinner) val searchButton = findViewById<Button>(R.id.buttonSearch) val hintTextView = findViewById<TextView>(R.id.hintTextView) //create a blank week spinner adapter val weekSpinnerAdapter = ArrayAdapter( this, android.R.layout.simple_spinner_item, arrayListOf("") ) weekSpinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item) //set the adapter to the week spinner weekSpinner.adapter = weekSpinnerAdapter //add year spinner item selection yearSpinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener { override fun onItemSelected( parent: AdapterView<*>?, view: View?, position: Int, id: Long ) { if (!weekSpinnerAdapter.isEmpty) weekSpinnerAdapter.clear() //get all weeks of the selected year val weeks = getAllSundayDate(resources.getStringArray(R.array.years)[position].toInt()) //add all weeks to the spinner adapter weekSpinnerAdapter.addAll(weeks) //notify the adapter for the data changes weekSpinnerAdapter.notifyDataSetChanged() } override fun onNothingSelected(parent: AdapterView<*>?) { } } //set search button click listener searchButton.setOnClickListener { if (weekSpinner.selectedItem != null) { hintTextView.text = weekSpinner.selectedItem.toString() } } } fun getAllSundayDate(year: Int): ArrayList<String> { //create a blank arrayList val weekList: ArrayList<String> = ArrayList() //it helps us to format the date val dateFormat: DateFormat = SimpleDateFormat("MM/dd/yyyy", Locale.US) val calendar = Calendar.getInstance() //starting from first day and first month of the year calendar.time = GregorianCalendar(year, Calendar.JANUARY, 1).time //loop through the all month for (month in 0..11) { val daysInMonth = calendar.getActualMaximum(Calendar.DAY_OF_MONTH) //loop through the all days in the month for (day in 1..daysInMonth) { calendar.set(year, calendar[Calendar.MONTH], day) val dayOfWeek = calendar[Calendar.DAY_OF_WEEK] //check the current day if (dayOfWeek == Calendar.SUNDAY) { weekList.add(dateFormat.format(calendar.time)) } } calendar.add(Calendar.MONTH, 1) } return weekList } }
That’s it. Happy coding!