Being the primary menus of android, Android Option Menus are used for settings, search, delete an item, etc. In this tutorial, we will first learn about the simple options menu use for searchview on the toolbar. The inflate() method of the MenuInflater class is used to inflate the menu. The onOptionsItemSelected() method of Activity class needs to be overridden to perform event handling on menu items.
Android Options Menu Example:
In the below example, we are demonstrating a simple options menu example with various menu items.
activity_main.xml:
In the activity_main.xml file, we will add the ListView.
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.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"> <ListView android:id="@+id/listView" android:layout_width="match_parent" android:layout_height="fill_parent" tools:ignore="MissingConstraints" /> </android.support.constraint.ConstraintLayout> |
menu.xml:
We will place the code to place the SearchView widget over ToolBar, in the menu.xml file in the menu folder. To create the menu folder, right-click on the res folder, then click New, select Directory and name it as menu. The menu.xml file then needs to be created inside the menu folder.
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/app_bar_search" android:title="Search" app:showAsAction="ifRoom|withText" app:actionViewClass="android.widget.SearchView"/> </menu> |
Activity class:(File: MainActivity.java)
package com.example.radioapp; import android.support.v4.view.MenuItemCompat; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.SearchView; import android.widget.Toast; import java.util.ArrayList; public class MainActivity extends AppCompatActivity { ListView listView; ArrayList<String> list; ArrayAdapter<String > adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); listView = (ListView) findViewById(R.id.listView); list = new ArrayList<>(); list.add("A1"); list.add("B2"); list.add("C3"); list.add("D4"); list.add("E5"); list.add("F6"); list.add("G7"); list.add("H8"); list.add("I9"); list.add("J10"); adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,list); listView.setAdapter(adapter); } @Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.menu, menu); MenuItem searchViewItem = menu.findItem(R.id.app_bar_search); final SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchViewItem); searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() { @Override public boolean onQueryTextSubmit(String query) { searchView.clearFocus(); /* if(list.contains(query)){ adapter.getFilter().filter(query); }else{ Toast.makeText(MainActivity.this, "No Match found",Toast.LENGTH_LONG).show(); }*/ return false; } @Override public boolean onQueryTextChange(String newText) { adapter.getFilter().filter(newText); return false; } }); return super.onCreateOptionsMenu(menu); } } |
Output 1:
Output 2: