I have an imageView. When i click image, i am hidding image and enabling searchview in expanded. But problem is close icon is not visible.
How to make close icon visible when searchview 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 screenshot where close icon at top right not visible:
Use the attribute app:closeIcon="@drawable/ic_my_close_icon"
in your SearchView
or android.support.v7.widget.SearchView
Use xmlns:app="http://schemas.android.com/apk/res-auto"
Try this
searchView.setOnCloseListener(new OnCloseListener() {
@Override
public boolean onClose() {
searchView.clearFocus();
return false;
}
});
the back option will display when you use[Complete concept about SearchView]
<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 Query String use this:
Intent intent = getIntent();
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String searchQuery = intent.getStringExtra(SearchManager.QUERY);
}
Dont forget to add this code in 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>
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);
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 click listener on closeButton