SearchView

Search View in Android Example | Android development

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.

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 AttributeDescription
android:iconifiedByDefault It specifies the SearchView’s default state.
android: maxWidth It specifies the maximum width of the SearchView, which is optional.
android: inputTypeIt specifies the type of input to be used in the query text box.
android : imeOptionsIt specifies which IME options should be applied to the query text field.
android : queryHintIn 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.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
//SearchView//
<SearchView
android:id="@+id/searchView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
//SearchView// <SearchView android:id="@+id/searchView" android:layout_width="match_parent" android:layout_height="wrap_content" />
//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.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<?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>
<?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>
<?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

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
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
}
})
}
}
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 } }) } }
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
            }
        })
    }
}

http://theudaipurstore.com

This Post Has 3 Comments

Leave a Reply