Does anyone know how to hide the back button in AppCompat v21 searchview? (outlined by green line)
I've searched a lot but couldn't find anything useful.
menu_main.xml:
<item android:id="#+id/search"
android:title="#string/search_title"
app:showAsAction="always|collapseActionView"
android:icon="#drawable/abc_ic_search_api_mtrl_alpha"
android:orderInCategory="300"
app:actionViewClass="android.support.v7.widget.SearchView" />
<item android:id="#+id/action_home"
android:title="Home"
android:icon="#drawable/v_home"
app:showAsAction="always"
android:orderInCategory="180"/>
<item android:id="#+id/action_favorites"
android:title="Favorites"
android:icon="#drawable/v_favorites"
app:showAsAction="always" />
MainActivity:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_main, menu);
firstMenu = menu;
searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
MenuItem searchItem = menu.findItem(R.id.search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setSubmitButtonEnabled(true);
searchView.setActivated(true);
searchView.setOnSearchClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
menuItemsVisibility(false);
}
});
searchView.setOnCloseListener(new SearchView.OnCloseListener() {
#Override
public boolean onClose() {
menuItemsVisibility(true);
return false;
}
});
return true;
}
#Override
public void onBackPressed() {
menuItemsVisibility(true);
super.onBackPressed();
}
// setting visibility of menu items on search
private void menuItemsVisibility(boolean visibility) {
MenuItem homeItem = firstMenu.findItem(R.id.action_home);
MenuItem favoriteItem = firstMenu.findItem(R.id.action_favorites);
MenuItem otItem = firstMenu.findItem(R.id.action_ot);
MenuItem ntItem = firstMenu.findItem(R.id.action_nt);
homeItem.setVisible(visibility);
favoriteItem.setVisible(visibility);
otItem.setVisible(visibility);
ntItem.setVisible(visibility);
}
Note: the behavior showAsAction:Always and using methods menuItemsVisibility() to adjust the visibility of toolbar items is intentional.
Another Note: MainActivity extends ActionBarActivity and it also implements implements ObservableScrollViewCallbacks from ObservableScrollView Library.
changed app:showAsAction="always|collapseActionView" to app:showAsAction="always"
Use the method:
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
in order to remove the home button from the action bar.
It's not perfectly safe method because back button (navigation up) doesn't have id. But if you are using AppCompat with Toolbar, you can use this code to find it. It should be the first in the layout.
int count = this.getToolbar().getChildCount();
for(int i = 0; i < count; ++i) {
View v = this.getToolbar().getChildAt(i);
if(v instanceof ImageButton) {
return (ImageButton)v;
}
}
Call this method inside onPrepareOptionsMenu if you want to change drawable of that button.
Related
How can i set my cursor position after search icon in android toolbar hint.. its a androidx search view.
<item
android:id="#+id/ic_action_search"
android:icon="#drawable/ic_search_black_icon"
android:title="Search"
app:actionViewClass="androidx.appcompat.widget.SearchView"
app:showAsAction="collapseActionView|ifRoom" />
Try to use this code.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
...
MenuItem miSearch = menu.findItem(R.id.ic_action_search);
SearchView mSearchView = (SearchView) miSearch.getActionView();
mSearchView.setOnSearchClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mSearchView.setFocusable(true);
mSearchView.requestFocusFromTouch();
mSearchView.setIconified(false);
}
});
I am wondering how to show searchview in actionbar only in parent fragment. For now, when I submit a search from searchview in actionbar and go to next fragment, I still can see the searchview in my child fragment. Any suggestions to fix that? Thanks.
For now, in my parent class
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
// Inflate the options menu from XML
inflater.inflate(R.menu.homesearchbar, menu);
super.onCreateOptionsMenu(menu,inflater);
// Get the SearchView and set the searchable configuration
SearchManager searchManager = (SearchManager) getActivity().getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
searchView.setMaxWidth(Integer.MAX_VALUE);
searchView.setIconifiedByDefault(false); // Do not iconify the widget; expand it by default
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
Bundle bundle = new Bundle();
bundle.putString("HomeToSearch_keyword", query);
SearchVC searchVC = new SearchVC();
searchVC.setArguments(bundle);
getFragmentManager().beginTransaction().add(R.id.HomeVC_frameChildFragment, searchVC)
.addToBackStack(null)
.commit();
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
return false;
}
});
}
homesearchbar.xml
<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/action_search"
android:title=""
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="always"/>
</menu>
To hide searchView inside child fragment override inside it onPrepareOptionsMenu method. Then inside that method you can do something like this: menu.findItem(R.id.search).setVisible(false); or eventually when needed you can change it to true. And also don't forget to override onCreate method where you will put setHasOptionsMenu(true);
I implemented a search bar in my toolbar and it currently looks like this:
But I want to change the background to white with a shadow so that it looks more like this (taken from Material Design guidelines):
How can I do this?
Here is my implementation of the SearchView:
menu_main.xml:
<menu
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/action_search"
android:title="Search"
android:icon="#drawable/ic_search_white_24dp"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="collapseActionView|ifRoom" />
</menu>
MainActivity.java:
public abstract class MainActivity extends AppCompatActivity {
protected Toolbar toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
if (toolbar != null) {
setSupportActionBar(toolbar);
}
}
#Override
public boolean onCreateOptionsMenu(final Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
final MenuItem searchItem = menu.findItem(R.id.action_search);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) searchItem.getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(new ComponentName(this, SearchActivity.class)));
searchView.setQueryHint("Search");
MenuItemCompat.setOnActionExpandListener(searchItem, new MenuItemCompat.OnActionExpandListener() {
#Override
public boolean onMenuItemActionExpand(MenuItem item) {
setMenuItemVisibility(menu, searchItem, false);
return true;
}
#Override
public boolean onMenuItemActionCollapse(MenuItem item) {
supportInvalidateOptionsMenu();
return true;
}
});
return true;
}
private void setMenuItemVisibility(Menu menu, MenuItem searchItem, boolean isVisible) {
for (int i = 0; i < menu.size(); i++) {
MenuItem item = menu.getItem(i);
Log.d(TAG, item.toString());
if (item != searchItem) {
item.setVisible(isVisible);
}
}
}
}
How can I style the SearchView to change the background color?
i m using searchview for search functionality my problem is that when i click on search icon keyboard gets opened but searchview does not get focus below is my code
menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/send_invitation"
android:icon="#drawable/send_icon_invite_participant"
android:orderInCategory="93"
android:title="#string/invitation"
app:showAsAction="always">
</item>
<item
android:id="#+id/search"
android:orderInCategory="100"
app:actionViewClass="android.support.v7.widget.SearchView"
android:icon="#drawable/search_icn"
android:title="#string/search"
app:showAsAction="always"/>
</menu>
below is code from method
#Override
public void onCreateOptionsMenu(final Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.invite_participant, menu);
final MenuItem searchItem = menu.findItem(R.id.search);
SearchManager searchManager = (SearchManager) activity.getSystemService(Context.SEARCH_SERVICE);
final SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView();
EditText searchEditText = (EditText) searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);
searchEditText.setTextColor(getResources().getColor(R.color.colorAccent));
searchEditText.setHintTextColor(getResources().getColor(R.color.colorAccent));
searchItem.expandActionView();
ImageView searchIcon = (ImageView) searchView.findViewById(android.support.v7.appcompat.R.id.search_button);
searchIcon.setImageResource(R.drawable.search_icn);
ImageView searchCloseIcon = (ImageView) searchView.findViewById(android.support.v7.appcompat.R.id.search_close_btn);
searchCloseIcon.setImageResource(R.drawable.reject_icon_notification);
if (searchView != null) {
searchView.setSearchableInfo(searchManager.getSearchableInfo(activity.getComponentName()));
searchView.setOnSearchClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
setItemsVisibility(menu, searchItem, false);
}
});
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
searchView.clearFocus();
return true;
}
#Override
public boolean onQueryTextChange(String newText) {
if (adapter != null) {
adapter.fliter(newText);
}
return false;
}
});
searchView.setOnCloseListener(new SearchView.OnCloseListener() {
#Override
public boolean onClose() {
setItemsVisibility(menu, searchItem, true);
return false;
}
});
}
}
private void setItemsVisibility(Menu menu, MenuItem exception, boolean visible) {
for (int i=0; i<menu.size(); ++i) {
MenuItem item = menu.getItem(i);
if (item != exception) item.setVisible(visible);
}
}
i m not able to find why it does not get focused please help me in finding where i have done mistake any help is appreciated.
You can add these lines for focus with searchview.
searchView.setFocusable(true);
searchView.requestFocus();
searchView.requestFocusFromTouch();
If it'll not work then First expand search view then request for focus.
You can achieve it either by adding below code in your Activity's onCreate method.
getWindow().setSoftInputMode( WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
or by adding this line in your AndroidManifest file
android:windowSoftInputMode="stateHidden"
Edit:
Try something like this
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
I've recently updated my apps to appcompat to v7:22.2.0. I have a searchview in the actionbar which was working fine before updating, but now the search field appears under the title when i press the search icon.
This is my code:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/search_participants"
android:icon="#drawable/search_white"
android:title="#string/search"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="collapseActionView|always"/>
</menu>
It still works, its fully functional, only the actionbar title is still displayed when using the SearchView. How can i prevent this from happening? Should i manually set the title to an empty string or is there a decent fix for this?
#Override
public void onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
mOptionsMenu = menu;
if (mOptionsMenu != null) {
menu.clear();
mMenuInflater.inflate(R.menu.live_tracking_overview_menu, menu);
mSearchItem = menu.findItem(R.id.search_participants);
mSearchView = (SearchView) MenuItemCompat.getActionView(mSearchItem);
mSearchView.setOnQueryTextListener(this);
mSearchView.setQueryHint(getString(R.string.search));
MenuItemCompat.setOnActionExpandListener(mSearchItem, new MenuItemCompat.OnActionExpandListener() {
#Override
public boolean onMenuItemActionExpand(MenuItem item) {
showSearch();
//i could hide the title here, but that's an ugly fix
return true;
}
#Override
public boolean onMenuItemActionCollapse(MenuItem item) {
hideSearch();
return true;
}
});