
122 views
SearchView in Android
The Android SearchView
is a user interface widget that allows users to enter search queries and initiate search operations. SearchView
provides a user-friendly way to implement search functionality within your Android app. It can be added to the app’s action bar (Toolbar) or any layout where you want to incorporate search functionality.
Here’s how to use a SearchView
in Android:
- Include the SearchView Widget: In your XML layout file, include the
SearchView
widget. If you want to add it to the Toolbar, ensure that your activity’s theme includes anActionBar
orToolbar
.
XML
<androidx.appcompat.widget.SearchView
android:id="@+id/search_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
If you’re using the AndroidX library, you’ll use androidx.appcompat.widget.SearchView
. For older versions, you can use android.widget.SearchView
.
- Java Code: In your activity or fragment, you need to handle the
SearchView
actions, including search queries and query text changes.
Java
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import androidx.appcompat.widget.SearchView;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_menu, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) searchItem.getActionView();
// Configure the SearchView
searchView.setQueryHint("Search...");
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
// Handle the query submission (e.g., perform search)
Toast.makeText(MainActivity.this, "Search query submitted: " + query, Toast.LENGTH_SHORT).show();
return true;
}
@Override
public boolean onQueryTextChange(String newText) {
// Handle query text change (e.g., update search suggestions)
return true;
}
});
return true;
}
}
In the code above:
- In the
onCreateOptionsMenu()
method, we inflate the menu that includes theSearchView
item. - We retrieve the
SearchView
usingmenu.findItem()
and configure it, including setting the query hint and handling query text changes and submissions.
- Handling Search Actions: In the
onQueryTextSubmit()
method of theOnQueryTextListener
, you can perform actions like searching for content based on the user’s query. This is where you would typically execute the search operation and display the results. You can also handle query text changes in theonQueryTextChange()
method if you want to provide real-time search suggestions or filter results as the user types.
You can add a SearchView
to your Android app, allowing users to search for content easily. Customize the search behavior according to your app’s requirements.