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
-
Android Studio version 3.6.1
-
Little bit XML and KOTLIN knowledge
-
Android Emulator (or) Android mobile
-
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.
Step 2
Now, add the activity and click the "Next" button.
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.
Step 4
Go to build.grade file. Add the third party dependencies for the search spinner.
The third party dependency is given below,
- 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,
- <?xml version="1.0" encoding="utf-8"?>
- <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- android:layout_width="match_parent"
- android:layout_height="match_parent">
- <com.toptoche.searchablespinnerlibrary.SearchableSpinner
- style="@android:style/Widget.Spinner.DropDown"
- android:id="@+id/spinner_view"
- android:layout_width="200dp"
- android:layout_height="wrap_content"
- app:layout_constraintBottom_toBottomOf="parent"
- app:layout_constraintLeft_toLeftOf="parent"
- app:layout_constraintRight_toRightOf="parent"
- app:layout_constraintTop_toTopOf="parent" />
- </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,
- package com.example.searchspinner
- import androidx.appcompat.app.AppCompatActivity
- import android.os.Bundle
- import android.widget.ArrayAdapter
- import kotlinx.android.synthetic.main.activity_main.*
-
- class MainActivity : AppCompatActivity() {
-
- private var number = arrayOf(
- "Search Number",
- "110",
- "111",
- "112",
- "113",
- "114",
- "115",
- "116",
- "117",
- "118",
- "119",
- "120",
- "121",
- "122",
- "123",
- "124",
- "125",
- "126",
- "127",
- "128",
- "129",
- "130",
- "131",
- "132",
- "133",
- "134",
- "135",
- "136",
- "137",
- "138",
- "139",
- "140",
- "141",
- "142",
- "143",
- "144",
- "145",
- "146",
- "147",
- "148",
- "149",
- "150",
- "151",
- "152",
- "153",
- "154",
- "155",
- "156",
- "157",
- "158",
- "159",
- "160",
- "161",
- "162",
- "163",
- "164",
- "165",
- "166",
- "167",
- "168",
- "169",
- "170"
- )
- override fun onCreate(savedInstanceState: Bundle?) {
- super.onCreate(savedInstanceState)
- setContentView(R.layout.activity_main)
- SearchSpinner()
- }
-
- private fun SearchSpinner() {
- val searchmethod = ArrayAdapter(this, android.R.layout.simple_spinner_item,number)
- searchmethod.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
- spinner_view!!.adapter = searchmethod
- }
-
- }
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