How We Can Search Spinner Item Using Kotlin In Android Studio

Introduction

 
Android is an open-source operating system based on Linux with a Java programming interface for mobile devices such as Smartphone (Touch Screen Devices who supports Android OS) as well for tablets. With over 85% market share worldwide, Android Operating System dominates the mobile platform market. Today, I will show you how to search spinner Item using Kotlin in Android Studio.
 
Requirements
Steps to be followed,
 
Follow these steps to use a search spinner Item using Kotlin In Android Studio. I have included the source code in the attachment.
 
Step 1
 
Open Android Studio and start a new Android Studio Project.
 
How We Can Search Spinner Item Using Kotlin In Android Studio 
 
Step 2
 
Now, add the activity and click the "Next" button.
 
How
 
Step 3
 
You can choose your application name and choose where your project is to be stored and choose Kotlin language for coding the project, Now, select the version of Android and select the target Android devices, and click the "Finish" button.
 
How We Can Search Spinner Item Using Kotlin In Android Studio
 
Step 4
 
Go to build.grade file. Add the third party dependencies for the search spinner.
 
How We Can Search Spinner Item Using Kotlin In Android Studio 
 
The third party dependency is given below,
  1. implementation 'com.toptoche.searchablespinner:searchablespinnerlibrary:1.3.1'  
Step 5
 
Go to activity_main.xml. This XML file contains the designing code for your Android app.
 
The XML code is given below,
  1. <?xml version="1.0" encoding="utf-8"?>      
  2. <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"      
  3.    xmlns:app="http://schemas.android.com/apk/res-auto"     
  4.    android:layout_width="match_parent"      
  5.    android:layout_height="match_parent">      
  6.    <com.toptoche.searchablespinnerlibrary.SearchableSpinner      
  7.    style="@android:style/Widget.Spinner.DropDown"      
  8.    android:id="@+id/spinner_view"      
  9.    android:layout_width="200dp"      
  10.    android:layout_height="wrap_content"      
  11.    app:layout_constraintBottom_toBottomOf="parent"      
  12.    app:layout_constraintLeft_toLeftOf="parent"      
  13.    app:layout_constraintRight_toRightOf="parent"      
  14.    app:layout_constraintTop_toTopOf="parent" />      
  15. </androidx.constraintlayout.widget.ConstraintLayout>    
Step 6
 
Go to Main Activity.kt. This Kotlin program is the back-end language for your app.
 
The Kotlin code is given below,
  1. package com.example.searchspinner  
  2. import androidx.appcompat.app.AppCompatActivity  
  3. import android.os.Bundle  
  4. import android.widget.ArrayAdapter  
  5. import kotlinx.android.synthetic.main.activity_main.*  
  6.    
  7. class MainActivity : AppCompatActivity() {  
  8.    
  9.         private var number = arrayOf(  
  10.         "Search Number",  
  11.         "110",  
  12.         "111",  
  13.         "112",  
  14.         "113",  
  15.         "114",  
  16.         "115",  
  17.         "116",  
  18.         "117",  
  19.         "118",  
  20.         "119",  
  21.         "120",  
  22.         "121",  
  23.         "122",  
  24.         "123",  
  25.         "124",  
  26.         "125",  
  27.         "126",  
  28.         "127",  
  29.         "128",  
  30.         "129",  
  31.         "130",  
  32.         "131",  
  33.         "132",  
  34.         "133",  
  35.         "134",  
  36.         "135",  
  37.         "136",  
  38.         "137",  
  39.         "138",  
  40.         "139",  
  41.         "140",  
  42.         "141",  
  43.         "142",  
  44.         "143",  
  45.         "144",  
  46.         "145",  
  47.         "146",  
  48.         "147",  
  49.         "148",  
  50.         "149",  
  51.         "150",  
  52.         "151",  
  53.         "152",  
  54.         "153",  
  55.         "154",  
  56.         "155",  
  57.         "156",  
  58.         "157",  
  59.         "158",  
  60.         "159",  
  61.         "160",  
  62.         "161",  
  63.         "162",  
  64.         "163",  
  65.         "164",  
  66.         "165",  
  67.         "166",  
  68.         "167",  
  69.         "168",  
  70.         "169",  
  71.         "170"  
  72.     )  
  73.     override fun onCreate(savedInstanceState: Bundle?) {  
  74.         super.onCreate(savedInstanceState)  
  75.         setContentView(R.layout.activity_main)  
  76.         SearchSpinner()  
  77.     }  
  78.    
  79.     private fun SearchSpinner()  {  
  80.             val searchmethod = ArrayAdapter(this, android.R.layout.simple_spinner_item,number)  
  81.         searchmethod.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)  
  82.             spinner_view!!.adapter = searchmethod  
  83.         }  
  84.    
  85. }  
Step 7
 
Then, click the "Run" button or press shift+f10 to finally run the project. And, choose the "virtual machine" option and click OK.
 

Conclusion

 
How We Can Search Spinner Item Using Kotlin In Android Studio
How We Can Search Spinner Item Using Kotlin In Android Studio 
How We Can Search Spinner Item Using Kotlin In Android Studio