Hello Friends, Today we are going to see another example of SearchView which is Custom Toolbar with SearchView in Android. SearchView in Toolbar was the first example of SearchView. The same type of toolbar I had developed in one of my project and It took more than enough time to develop it. That’s why I am sharing it so that it will help other as well as me in future projects.
So, here is the code
MainActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 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 |
package com.zatackcoder.customtoolbarwithsearchview; import android.app.SearchManager; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.view.Gravity; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast; import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.widget.SearchView; import androidx.core.view.GravityCompat; import androidx.drawerlayout.widget.DrawerLayout; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; import com.google.android.material.navigation.NavigationView; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener { private TextView appNameTV; private SearchView searchSV; List<Item> items = new ArrayList<>(); private RecyclerView mRecyclerView; private RecyclerView.Adapter mAdapter; private RecyclerView.LayoutManager mLayoutManager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final DrawerLayout drawer = findViewById(R.id.drawer_layout); NavigationView navigationView = findViewById(R.id.nav_view); navigationView.setNavigationItemSelectedListener(this); ImageView menuIcon = findViewById(R.id.menu_icon); menuIcon.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { drawer.openDrawer(Gravity.LEFT); } }); mRecyclerView = findViewById(R.id.recycler_view); mRecyclerView.setHasFixedSize(true); mLayoutManager = new LinearLayoutManager(this); mRecyclerView.setLayoutManager(mLayoutManager); items.add(new Item("Item 1", "Red", 10, 100.00)); items.add(new Item("Item 2", "Red", 12, 100.00)); items.add(new Item("Item 3", "Red", 14, 100.00)); items.add(new Item("Item 4", "Red", 16, 150.00)); items.add(new Item("Item 5", "Red", 18, 170.00)); items.add(new Item("Item 6", "Green", 20, 190.00)); items.add(new Item("Item 7", "Green", 10, 100.00)); items.add(new Item("Item 8", "Green", 12, 200.00)); items.add(new Item("Item 9", "Green", 14, 210.00)); items.add(new Item("Item 10", "Green", 16, 240.00)); items.add(new Item("Item 11", "Blue", 18, 250.00)); items.add(new Item("Item 12", "Blue", 20, 280.00)); items.add(new Item("Item 13", "Blue", 10, 300.00)); items.add(new Item("Item 14", "Blue", 12, 150.00)); items.add(new Item("Item 15", "White", 10, 170.00)); mAdapter = new ItemAdapter(getApplicationContext(), items); mRecyclerView.setAdapter(mAdapter); appNameTV = findViewById(R.id.appNameTV); searchSV = findViewById(R.id.searchSV); SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE); searchSV.setSearchableInfo(searchManager.getSearchableInfo(getComponentName())); searchSV.setOnQueryTextFocusChangeListener(new View.OnFocusChangeListener() { @Override public void onFocusChange(View view, boolean b) { if (view.hasFocus() || searchSV.getQuery().length() > 0) { appNameTV.setVisibility(View.GONE); } else { appNameTV.setVisibility(View.VISIBLE); } } }); searchSV.setOnQueryTextListener(new SearchView.OnQueryTextListener() { @Override public boolean onQueryTextSubmit(String s) { return true; } @Override public boolean onQueryTextChange(String s) { findViewById(R.id.appNameTV).setVisibility(View.GONE); List<Item> newList = new ArrayList<>(); for (Item item : items) { if (item.getName().toLowerCase().contains(s.toLowerCase()) || item.getColor().toLowerCase().contains(s.toLowerCase()) || String.valueOf(item.getSize()).contains(s) || String.valueOf(item.getPrice()).contains(s)) { newList.add(item); } } mAdapter = new ItemAdapter(getApplicationContext(), newList); mRecyclerView.setAdapter(mAdapter); return true; } }); } /*To Handle Mic Search*/ @Override protected void onNewIntent(Intent intent) { super.onNewIntent(intent); if (Intent.ACTION_SEARCH.equals(intent.getAction())) { String query = intent.getStringExtra(SearchManager.QUERY); searchSV.setQuery(String.valueOf(query), false); searchSV.requestFocus(); } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @SuppressWarnings("StatementWithEmptyBody") @Override public boolean onNavigationItemSelected(MenuItem item) { // Handle navigation view item clicks here. int id = item.getItemId(); if (id == R.id.nav_home) { Toast.makeText(getApplicationContext(), "Home Clicked", Toast.LENGTH_LONG).show(); } else if (id == R.id.nav_gallery) { Toast.makeText(getApplicationContext(), "Gallery Clicked", Toast.LENGTH_LONG).show(); } else if (id == R.id.nav_slideshow) { Toast.makeText(getApplicationContext(), "Slideshow Clicked", Toast.LENGTH_LONG).show(); } DrawerLayout drawer = findViewById(R.id.drawer_layout); drawer.closeDrawer(GravityCompat.START); return true; } } |
activity_main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
<?xml version="1.0" encoding="utf-8"?> <androidx.drawerlayout.widget.DrawerLayout 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:id="@+id/drawer_layout" android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true" tools:openDrawer="start"> <include layout="@layout/app_bar_main" android:layout_width="match_parent" android:layout_height="match_parent" /> <com.google.android.material.navigation.NavigationView android:id="@+id/nav_view" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_gravity="start" android:fitsSystemWindows="true" app:headerLayout="@layout/nav_header_main" app:menu="@menu/activity_main_drawer" /> </androidx.drawerlayout.widget.DrawerLayout> |
app_bar_main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
<?xml version="1.0" encoding="utf-8"?> <androidx.coordinatorlayout.widget.CoordinatorLayout 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"> <com.google.android.material.appbar.AppBarLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:theme="@style/AppTheme.AppBarOverlay"> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_marginTop="5dp" android:layout_marginLeft="5dp" android:layout_marginRight="5dp" android:layout_marginBottom="5dp" android:background="@android:drawable/editbox_background" app:layout_scrollFlags="scroll|enterAlways"> <ImageView android:id="@+id/menu_icon" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_centerVertical="true" android:src="@drawable/ic_action_menu" android:layout_marginLeft="10dp"/> <TextView android:id="@+id/appNameTV" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_toRightOf="@+id/menu_icon" android:background="@android:color/transparent" android:paddingTop="10dp" android:paddingBottom="10dp" android:paddingLeft="25dp" android:paddingRight="25dp" android:singleLine="true" android:text="App Name" android:textColor="#FF000000" android:textStyle="bold" android:textSize="20sp" /> <androidx.appcompat.widget.SearchView android:id="@+id/searchSV" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_toRightOf="@+id/menu_icon" android:background="@android:color/transparent" android:imeOptions="actionSearch" android:textColor="#FA615E5E" android:textColorHint="#FAA59D9D" android:textStyle="bold" android:focusable="false" app:iconifiedByDefault="false" app:searchIcon="@null" app:queryHint="" /> </RelativeLayout> </com.google.android.material.appbar.AppBarLayout> <include layout="@layout/content_main" /> </androidx.coordinatorlayout.widget.CoordinatorLayout> |
content_main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<?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" app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:showIn="@layout/app_bar_main"> <androidx.recyclerview.widget.RecyclerView android:id="@+id/recycler_view" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginBottom="5dp" tools:listitem="@layout/list_item" /> </androidx.constraintlayout.widget.ConstraintLayout> |
nav_header_main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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="@dimen/nav_header_height" android:background="@drawable/side_nav_bar" android:gravity="bottom" android:orientation="vertical" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingBottom="@dimen/activity_vertical_margin" android:theme="@style/ThemeOverlay.AppCompat.Dark"> <ImageView android:id="@+id/imageView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:contentDescription="@string/nav_header_desc" android:paddingTop="@dimen/nav_header_vertical_spacing" app:srcCompat="@mipmap/ic_launcher_round" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingTop="@dimen/nav_header_vertical_spacing" android:text="@string/nav_header_title" android:textAppearance="@style/TextAppearance.AppCompat.Body1" /> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/nav_header_subtitle" /> </LinearLayout> |
menu/activity_main_drawer.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" tools:showIn="navigation_view"> <group android:checkableBehavior="single"> <item android:id="@+id/nav_home" android:icon="@drawable/ic_menu_camera" android:title="@string/menu_home" /> <item android:id="@+id/nav_gallery" android:icon="@drawable/ic_menu_gallery" android:title="@string/menu_gallery" /> <item android:id="@+id/nav_slideshow" android:icon="@drawable/ic_menu_slideshow" android:title="@string/menu_slideshow" /> </group> </menu> |
ItemAdapter.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
package com.zatackcoder.customtoolbarwithsearchview; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import android.widget.Toast; import androidx.annotation.NonNull; import androidx.recyclerview.widget.RecyclerView; import java.util.List; public class ItemAdapter extends RecyclerView.Adapter<ItemAdapter.MyViewHolder> { private Context context; private List<Item> items; public ItemAdapter(Context context, List<Item> items) { this.context = context; this.items = items; } @NonNull @Override public MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int position) { View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_item, viewGroup, false); MyViewHolder mvh = new MyViewHolder(view); return mvh; } @Override public void onBindViewHolder(@NonNull final MyViewHolder myViewHolder, final int position) { myViewHolder.container.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String message = "You clicked on " + items.get(position).getName() + " - color:" + items.get(position).getColor() + " | size:" + items.get(position).getSize() + " | price:" + items.get(position).getPrice(); Toast.makeText(context, message, Toast.LENGTH_LONG).show(); } }); myViewHolder.name.setText(items.get(position).getName()); myViewHolder.color.setText("Color: " + items.get(position).getColor()); myViewHolder.size.setText("Size: " + items.get(position).getSize()); myViewHolder.price.setText("Price: " + items.get(position).getPrice()); } @Override public int getItemCount() { return items.size(); } public static class MyViewHolder extends RecyclerView.ViewHolder { View container; TextView name; TextView color; TextView size; TextView price; public MyViewHolder(View view) { super(view); container = view; name = view.findViewById(R.id.name); color = view.findViewById(R.id.color); size = view.findViewById(R.id.size); price = view.findViewById(R.id.price); } } } |
list_item.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="5dp" android:background="#ffffff" android:clickable="true" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="?android:attr/selectableItemBackground" android:orientation="vertical" android:padding="5dp"> <TextView android:id="@+id/name" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Name" android:textFontWeight="bold" android:textSize="15dp" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:id="@+id/color" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Color: Red" android:textSize="12dp" /> <View android:layout_width="1dp" android:layout_height="match_parent" android:layout_margin="5dp" android:background="#ADABAB" /> <TextView android:id="@+id/size" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Size: 10" android:textSize="12dp" /> <View android:layout_width="1dp" android:layout_height="match_parent" android:layout_margin="5dp" android:background="#ADABAB" /> <TextView android:id="@+id/price" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Price: 100" android:textSize="12dp" /> </LinearLayout> </LinearLayout> </LinearLayout> |
Item.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
package com.zatackcoder.customtoolbarwithsearchview; public class Item { private String name; private String color; private Integer size; private Double price; public Item(String name, String color, Integer size, Double price) { this.name = name; this.color = color; this.size = size; this.price = price; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } public Integer getSize() { return size; } public void setSize(Integer size) { this.size = size; } public Double getPrice() { return price; } public void setPrice(Double price) { this.price = price; } } |
xml/searchable.xml
1 2 3 4 5 6 |
<?xml version="1.0" encoding="utf-8"?> <searchable xmlns:android="http://schemas.android.com/apk/res/android" android:label="@string/app_name" android:hint="Search" android:voiceSearchMode="showVoiceSearchButton|launchRecognizer"> </searchable> |
This (searchable.xml) xml is gonna use in AndroidManifest.xml to add mic icon on SearchView
styles.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
<resources> <!-- Base application theme. --> <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> <!-- Customize your theme here. --> <item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorAccent">@color/colorAccent</item> </style> <style name="AppTheme.NoActionBar"> <item name="windowActionBar">false</item> <item name="windowNoTitle">true</item> </style> <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" > <!--To Change SearchView Text & Mic--> <item name="android:editTextColor">@android:color/black</item> <item name="android:textColorHint">@android:color/darker_gray</item> <item name="android:textColorPrimary">@android:color/black</item> </style> <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" /> </resources> |
This (styles.xml) is customised to change text and mic color of SearchView as it was white
AndroidManifest.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.zatackcoder.customtoolbarwithsearchview"> <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" android:label="@string/app_name" android:launchMode="singleTop" android:theme="@style/AppTheme.NoActionBar"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <action android:name="android.intent.action.SEARCH" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> <!--To Show Mic in SearchView--> <meta-data android:name="android.app.searchable" android:resource="@xml/searchable" /> </activity> </application> </manifest> |
Video
AndroidStudio Project Download
CustomToolbarWithSearchView AndroidStudio Project
1 file(s) 15.36 MB
Thanks for Stoping by
If you find this helpful then please do share
Comments