Rapid Subscribe Android App

Rapid Subscribe Android App
Rapid Subscriber

Recent Posts

how to create a search view in android




In Android Programming, Search View is a different Widget view which is used in our activity with a search bar and list view on the bottom to search something from that.

To Create a Search View, Firstly We create a Srchvw activity where we use that search View.
 <?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=".Srchvw">  
   <SearchView  
     android:id="@+id/searchView"  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:queryHint="Search Here"  
     android:iconifiedByDefault="false"  
     android:layout_alignParentTop="true" />  
    
   <ListView  
     android:layout_width="match_parent"  
     android:layout_height="match_parent"  
     android:id="@+id/SearchListView"  
     android:divider="#ad5"  
     android:dividerHeight="2dp"  
     android:layout_below="@+id/searchView"/>  
   
 </RelativeLayout>  

Then we use that search view in our activity file.
Also, We create an Array List And ArrayAdapter in this activity.
 public class Srchvw extends AppCompatActivity {  
   SearchView searchview;  
   ListView searchlist;  
   
   ArrayList<String> list = new ArrayList<String>();  
   ArrayAdapter adapter;     
   
   @Override  
   protected void onCreate(Bundle savedInstanceState) {  
     super.onCreate(savedInstanceState);  
     setContentView(R.layout.activity_srchvw);  

     searchview = findViewById(R.id.searchView);  
     searchlist = (ListView) findViewById(R.id.SearchListView);  
   

Add something in your list and set your adapter.
 list.add("Harpreet");  
     list.add("Studio");  
     list.add("hello");  
     list.add("today");  
     list.add("we create");  
     list.add("Searchview");  
     list.add("search");  
     list.add("something");  
     list.add("in your list");  
   
     adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,list);  
     searchList.setAdapter(adapter);  


Then your Query Text Listener to Search Something in your list
 srch.setOnQueryTextListener(new android.widget.SearchView.OnQueryTextListener() {  
   
       @Override  
       public boolean onQueryTextSubmit(String query) {  
         if (list.contains(query)) {  
           adapter.getFilter().filter(query);  
   
         } else {  
           Toast.makeText(Srchvw.this, "no text found", Toast.LENGTH_SHORT).show();  
   
         }  
         return true;  
          }  
   
       @Override  
       public boolean onQueryTextChange(String newText) {  
         if(list.isEmpty())  
         {  
   
         }  
         else {  
           adapter.getFilter().filter(newText);  
   
         }  
         return false;  
       }  
     });  


Then we create a button to go to our Search Activity, a button inside EditText on the right side.

set drawable right in XML. an icon you want to display.

Here tooltext is your edittext.
 tooltext.setOnTouchListener(new View.OnTouchListener() {  
       @Override  
       public boolean onTouch(View v, MotionEvent event) {  
         v.performClick();  
   
         final int DRAWABLE_RIGHT = 2;  
   
         if(event.getAction() == MotionEvent.ACTION_UP) {  
           if(event.getRawX() >= (tooltext.getRight() - tooltext.getCompoundDrawables()[DRAWABLE_RIGHT].getBounds().width())) {  
             startActivity(new Intent(MainActivity.this,Srchvw.class));  
   
             return true;  
           }  
         }  
         return false;  
       }  
     });  





Follow us for more posts like this, 
Subscribe Harpreet studio on Youtube  
Like Harpreet Studio on Facebook  
Follow me on Instagram 

No comments