In this tutorial, we will learn how to implement a Search view in android combined with listview using the kotlin language.
android.widget. SearchView. A widget that allows a user to enter a search query and submit it to a search provider through a user interface. If available, displays a list of query recommendations or results and allows the user to select one to launch into.
Contents
How to Search on android?
The Android system controls the search dialogue, which is a UI component. The search dialogue appears at the top of the activity when the user activates it, as seen in the figure below.
The search widget is a SearchView instance that may be positioned anywhere in your layout.
SearchView XML Attribute
| XML Attribute | Description |
|---|---|
| android:iconifiedByDefault | It specifies the SearchView’s default state. |
| android: maxWidth | It specifies the maximum width of the SearchView, which is optional. |
| android: inputType | It specifies the type of input to be used in the query text box. |
| android : imeOptions | It specifies which IME options should be applied to the query text field. |
| android : queryHint | In the empty query box, an optional query hint string should be displayed. |
Preview: Search view in android
<–SearchView Example
–>As shown below, Search view in android is defined in the XML layout.
//SearchView//
<SearchView
android:id="@+id/searchView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
File: activity_main.xml
–>Below is the activity main.xml file. It consists of ListView or SearchView.
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<SearchView
android:id="@+id/searchView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:iconifiedByDefault="false"
android:queryHint="Search Here"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ListView
android:id="@+id/listview"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_below="@+id/searchView"
android:divider="#33691E"
android:dividerHeight="2.5dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/searchView" />
</androidx.constraintlayout.widget.ConstraintLayout>
File: MainActivity.kt
–> Below is the MainActivity.kt file
package com.sagar.searchviewexample
import android.os.Bundle
import android.view.View
import android.widget.ArrayAdapter
import android.widget.ListView
import android.widget.SearchView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
var searchView: SearchView? = null
var listView: ListView? = null
var list: ArrayList<String>? = null
var adapter: ArrayAdapter<String>? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
searchView = findViewById<View>(R.id.searchView) as SearchView
listView = findViewById<View>(R.id.listview) as ListView
list = ArrayList()
list!!.add("c")
list!!.add("C++")
list!!.add("Ruby")
list!!.add("Kotlin")
list!!.add("Android")
list!!.add("Web")
list!!.add("Game")
list!!.add("Website")
list!!.add("Coding")
list!!.add("DevelopersDome")
list!!.add("c++")
list!!.add("ruby")
list!!.add("kotlin")
list!!.add("android")
list!!.add("web")
list!!.add("game")
list!!.add("website")
list!!.add("coding")
list!!.add("developersdome")
adapter = ArrayAdapter(this, android.R.layout.simple_list_item_1, list!!)
listView!!.adapter = adapter
searchView!!.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
override fun onQueryTextSubmit(query: String): Boolean {
if (list!!.contains(query)) {
adapter!!.filter.filter(query)
} else {
Toast.makeText(applicationContext, "There was no match.", Toast.LENGTH_LONG).show()
}
return false
}
override fun onQueryTextChange(newText: String): Boolean {
return false
}
})
}
}
Pingback: Bottom Navigation Bar in Android with Example | Kotlin - Developers Dome
Pingback: RecyclerView GridView Android Example in Kotlin - Developers Dome
Pingback: Image Gallery App with Example Android - Developers Dome