How to display search icon in dropdown in android?

I have an imageView. When I click on an image, I leave the image and expand the search in Searchview. But the problem in the close icon is not visible. How to make a closed icon visible when searching in expanded form?

Here is my piece of code:

public class HomeActivity extends Activity{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); search = (SearchView) findViewById(R.id.search); searchImage=(ImageView) findViewById(R.id.search_image); searchImage.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { search.setVisibility(View.VISIBLE); search.setIconifiedByDefault(false); // here i am disabling default icon searchImage.setVisibility(View.GONE); } }); //This function not works as close icon not visible. search.setOnCloseListener(new OnCloseListener() { @Override public boolean onClose() { searchImage.setVisibility(View.VISIBLE); return false; } }); 

Here is a screenshot where the close icon is not visible in the upper right: enter image description here

+1
android searchview android-imageview android-search
Dec 30 '15 at 13:15
source share
6 answers

Use the app:closeIcon="@drawable/ic_my_close_icon" attribute app:closeIcon="@drawable/ic_my_close_icon" in SearchView or android.support.v7.widget.SearchView

Use xmlns:app="http://schemas.android.com/apk/res-auto"

0
Dec 30 '15 at 13:30
source share
β€” -

Try

  searchView.setOnCloseListener(new OnCloseListener() { @Override public boolean onClose() { searchView.clearFocus(); return false; } }); 
0
Dec 30 '15 at 14:00
source share

the opposite option will appear when you use [Complete SearchView Concept]

 <menu 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" tools:context="blackmonk.mobile.activity.SearchActivity" > <item android:id="@+id/action_searchh" android:actionViewClass="android.support.v7.widget.SearchView" android:icon="@drawable/ic_search" android:showAsAction="ifRoom|collapseActionView" android:title="@string/search" app:actionViewClass="android.support.v7.widget.SearchView" app:showAsAction="always"/> </menu> 

and use

 @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.search_menu, menu); MenuItem searchItem = menu.findItem(R.id.action_searchh); mSearchView = (SearchView) searchItem.getActionView(); // Associate searchable configuration with the SearchView SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE); SearchView searchView = (SearchView) menu.findItem(R.id.action_searchh) .getActionView(); searchView.setSearchableInfo(searchManager .getSearchableInfo(getComponentName())); searchView.setQueryHint("Search here"); //to expand search bar searchItem.expandActionView(); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.action_search: break; case android.R.id.home: onBackPressed(); return true; default: break; } return super.onOptionsItemSelected(item); } 

If you want to get a Query String, use this:

 Intent intent = getIntent(); if (Intent.ACTION_SEARCH.equals(intent.getAction())) { String searchQuery = intent.getStringExtra(SearchManager.QUERY); } 

Remember to add this code to your manifest activity.

  <activity android:name="blackmonk.mobile.activity.SearchActivity" android:label="@string/app_name" android:screenOrientation="portrait" > <intent-filter> <action android:name="android.intent.action.SEARCH" > <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </action> </intent-filter> <meta-data android:name="android.app.searchable" android:resource="@xml/search" /> </activity> 
0
Feb 02 '15 at 11:05
source share
 int search_close_btnId = android.support.v7.appcompat.R.id.search_close_btn; ImageView search_close_btn = (ImageView) searchView.findViewById(search_close_btnId); search_close_btn.setImageResource(R.drawable.ic_close_search); 
0
Jan 27 '16 at 13:40
source share
 int searchCloseIconButtonId = SearchView.getContext().getResources() .getIdentifier("android:id/search_close_btn", null, null); ImageView closeButton = (ImageView) this.mSearchView.findViewById(searchCloseIconButtonId); closeButton.setImageResource(R.mipmap.close); //any icon image 

If you want, you can add a closeButton to closeButton

0
Jan 10 '18 at 7:28
source share

Since SearchView always set the Close button to Gone after onFocusChange, I did this in an elegant way.

 public void onFocusChange(View view, boolean b) { Util.Android.scheduleRunOnUi(Util.Android.getActivity(view), ()->mCloseView.setVisibility(VISIBLE), 500); } 
0
Jul 22 '19 at 8:19
source share



All Articles