Home > Software engineering >  How to make search box in Android Studio
How to make search box in Android Studio

Time:09-27

i want to make search box inside my android app. i am fetching posts from my Wordpress Website using Json and Retrofit , i want users can sort posts by keyword , for example if they search for "love" all posts which contain keyword love will be shown to them , this is how i am using the search method:

https://wahstatus.com/wp-json/wp/v2/posts/?search=love&per_page=29

now i want to make search box and when users insert keyword in box and press enter it , the url should make changes to itself..

like this ..

https://wahstatus.com/wp-json/wp/v2/posts/?search= keyword here    &per_page=29

i don't know how to insert the keword from search box to the url,, please help

i don't want search box in toolbar , i want it to be in activity..

CodePudding user response:

You can use searchview

in xml

 <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"  

/>

in Activity

 searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {  
        @Override  
        public boolean onQueryTextSubmit(String query) {  

             String searchText = query;       
             // Call the  api
         https://wahstatus.com/wp-json/wp/v2/posts/?search= searchText   &per_page=29

            }  
            return false;  
        }  

        @Override  
        public boolean onQueryTextChange(String newText) {  
         
            return false;  
        }  
    });  
  • Related