How to use SearchView in Toolbar Android?

AndroidApps/ApplicationsMobile Development

This example demonstrates how do I use searchView in Toolbar android.

Step 1 − Create a new project in Android Studio, go to File⇒ New Project and fill all required details to create a new project.

Step 2 − Add the following code to res/layout/activity_main.xml.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
   xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   xmlns:app="http://schemas.android.com/apk/res-auto"
   tools:context=".MainActivity">
   <android.support.v7.widget.Toolbar
      android:id="@+id/toolbar"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:background="?attr/colorPrimary"
      android:minHeight="?attr/actionBarSize"
      app:actionViewClass="android.support.v7.widget.SearchView"
      app:theme="@style/ThemeOverlay.AppCompat.Dark" />
   <ListView
      android:id="@+id/listView"
      android:layout_width="match_parent"
      android:layout_height="wrap_content" />
   <TextView
      android:id="@+id/emptyView"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_centerInParent="true"
      android:text="No Results"
      android:textSize="20sp"
      android:visibility="gone"/>
</RelativeLayout>

Step 3 − Add the following code to src/MainActivity.java

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.SearchView;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
   Toolbar toolbar;
   ArrayAdapter adapter;
   ListView listView;
   TextView emptyView;
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      toolbar = findViewById(R.id.toolbar);
      listView = findViewById(R.id.listView);
      emptyView = findViewById(R.id.emptyView);
      adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1,
      getResources().getStringArray(R.array.months_array));
      listView.setAdapter(adapter);
      listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
         @Override
         public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            Toast.makeText(MainActivity.this, adapterView.getItemAtPosition(i).toString(), Toast.LENGTH_SHORT).show();
         }
      });
      listView.setEmptyView(emptyView);
   }
   @Override
   public boolean onCreateOptionsMenu(Menu menu) {
      getMenuInflater().inflate(R.menu.menu, menu);
      MenuItem mSearch = menu.findItem(R.id.appSearchBar);
      SearchView mSearchView = (SearchView) mSearch.getActionView();
      mSearchView.setQueryHint("Search");
      mSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
         @Override
         public boolean onQueryTextSubmit(String query) {
            return false;
         }
         @Override
         public boolean onQueryTextChange(String newText) {
            adapter.getFilter().filter(newText);
            return true;
         }
      });
      return super.onCreateOptionsMenu(menu);
   }
}

Step 4 − Right click on the project, Select − New − Android Resource Directory(menu). Right on res/menu, select New − Menu resource flle and add the following code in the res/menu/menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:app="http://schemas.android.com/apk/res-auto">
   <item
      android:id="@+id/appSearchBar"
      android:icon="@drawable/ic_search"
      android:title="Search"
      app:showAsAction="ifRoom|withText"
      app:actionViewClass="android.support.v7.widget.SearchView"/>
</menu>

Step 5 − Open res/values/strings.xml and the following code

<resources>
   <string name="app_name">Sample</string>
   <string-array name="months_array">
      <item>January</item>
      <item>February</item>
      <item>March</item>
      <item>April</item>
      <item>May</item>
      <item>June</item>
      <item>July</item>
      <item>August</item>
      <item>September</item>
      <item>October</item>
      <item>November</item>
      <item>December</item>
   </string-array>
</resources>

Step 6 − Add the following code to androidManifest.xm

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="app.com.sample">
   <application
      android:allowBackup="true"
      android:icon="@mipmap/ic_launcher"
      android:label="@string/app_name"
      android:roundIcon="@mipmap/ic_launcher_round"
      android:supportsRtl="true"
      android:theme="@style/AppTheme">
      <activity android:name=".MainActivity">
         <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
         </intent-filter>
      </activity>
   </application>
</manifest>

Let's try to run your application. I assume you have connected your actual Android Mobile device with your computer. To run the app from android studio, open one of your project's activity files and click Run Play Icon icon from the toolbar. Select your mobile device as an option and then check your mobile device which will display your default screen −

Click here to download the project code.

raja
Published on 06-Aug-2019 10:34:22
Advertisements