how to use an extend SearchView in fragment


I want to extend the SearchView class to override the onKeyDown() method. So i do it within my fragment class as below :

public class myfragment extends ListFragment{ ... class CustomSearchView extends SearchView{ public CustomSearchView(Context context) { super(context); } public CustomSearchView(Context context, AttributeSet attrs) { super(context, attrs); } @Override public boolean onKeyDown(int keyCode, KeyEvent event){ Toast.makeText(getActivity(), "onkeydown", Toast.LENGTH_SHORT).show(); return super.onKeyDown(keyCode, event); } } ... }

Now i want to make use of this extended SearchView in the fragment. But i am not sure how to do that. Since i create the SearchView object using searchItem.getActionView(), is there some way i can cast it to a CustomSearchView object so that the overriden onKeyDown method gets triggered in my fragment?

@Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { ... // Retrieves the system search manager service final SearchManager searchManager = (SearchManager) getActivity().getSystemService(Context.SEARCH_SERVICE); // Retrieves the SearchView from the search menu item final SearchView searchView = (SearchView) searchItem.getActionView(); // Assign searchable info to SearchView searchView.setSearchableInfo( searchManager.getSearchableInfo(getActivity().getComponentName())); searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() { @Override public boolean onQueryTextSubmit(String queryText) { // Nothing needs to happen when the user submits the search string return true; } @Override public boolean onQueryTextChange(String newText) { ... } });

This question popped up because of the solution mentioned at <a href="https://stackoverflow.com/questions/13626756/how-can-i-get-onbackpressed-action-while-searchview-is-activated" rel="nofollow">How can I get onBackPressed() while SearchView is activated?</a>


You can cast to your custom SearchView:

final CustomSearchView searchView = (CustomSearchView) searchItem.getActionView();

When you declare the SearchView you have to declare your own SearchView using full class name in the meny xml file. Also, if you're using support library, you have to declare http://schemas.android.com/apk/res-auto namespace:

No Support Library:

<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android" > <item android:id="@+id/action_search" android:title="@string/action_search" android:icon="@drawable/action_search" android:showAsAction="always|collapseActionView" android:actionViewClass="com.xxx.ListFragment.CustomSearchView" />

Support Library:

<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:yourapp="http://schemas.android.com/apk/res-auto" > <item android:id="@+id/action_search" android:title="@string/action_search" android:icon="@drawable/action_search" yourapp:showAsAction="always|collapseActionView" yourapp:actionViewClass="com.xxx.ListFragment.CustomSearchView" />


  • How to get the phone number(callerID) in asterisk
  • Asterisk : originate call doesn't set the CALLERID in the dialplan
  • DrawerItemClickListener cannot be resolved to a type
  • Change Android Action Bar Menu Item's Icon on Click
  • Search item on action bar android
  • Using Global Variables
  • PopupMenu dismiss without checking the selected checkbox item
  • onDraw() Method work only when i creating my object in the onDraw()
  • How can I access the content of something created with programmatically?
  • Editing text in action bar
  • super() in Flash Builder. WHY?
  • SearchView wrong alignment
  • Scala prevent mixing methods
  • onActivityResult() not called after startActivityForResult() with Intent.ACTION_GET_CONTENT
  • How to record a JNLP/ Java Web Start application with JMeter
  • finding a function with particular name
  • WPF Animation doesn't run first time
  • Missing permission to control media error on Android 5/L
  • OpenOptionsMenu not working with ActionBarSherlock Custom SubMenu
  • Position Fixed in Chrome
  • Get rendered html code in Backing Component from Composite Component
  • richtextbox to string
  • IE11 textarea loses focus if another textarea is disabled
  • calling a fragment from fragment
  • Warning: strpos() expects parameter 1 to be string, resource
  • Ruby 1.8.6 Array#uniq not removing duplicate hashes
  • Why cepheus don't send int without quotes to orion?
  • How do I include a SWC in an AS2 Flash project?
  • Bypass multiple inheritance in Java
  • wxPython: displaying multiple widgets in same frame
  • Deleting and Updating values from a cusrsor adapter
  • Window Size for Mac application
  • Adding custom controls to a full screen movie
  • Comma separated Values
  • Error creating VM instance in Google Compute Engine
  • Hits per day in Google Big Query
  • how does django model after text[] in postgresql [duplicate]
  • unknown Exception android
  • EntityFramework adding new object to nested object collection
  • Checking variable from a different class in C#
  • failed to connect to specific WiFi in android programmatically
  • How can I use threading to 'tick' a timer to be accessed by other threads?