Android Options Menu on Action bar shows duplicate icons - android

action bar (Toolbar) on android has 3 options menu. Its working fine. But the problem is when ever i am uninstalling and reinstalling this duplication of the menu item is showing. Also the Title is also remains same, not changing. after sometimes, say if we close the app and restarts its working fine. I am totally confused and struck.
Here is the Code, MainActivity (OnCreateOptionsMenu):
#Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.clear();
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.menu_main, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
SearchManager searchManager = (SearchManager) MainActivity.this.getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = null;
if (searchItem != null) {
searchView = (SearchView) searchItem.getActionView();
}
if (searchView != null) {
searchView.setSearchableInfo(searchManager.getSearchableInfo(MainActivity.this.getComponentName()));
}
MenuItem item = menu.findItem(R.id.action_wishlist);
wishlistCount = (RelativeLayout) MenuItemCompat.getActionView(item);
wishlistCount.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
setWishlistCount();
MenuItem cartitem = menu.findItem(R.id.action_cart);
cartCount = (RelativeLayout) MenuItemCompat.getActionView(cartitem);
cartCount.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
restoreToolbar();
return super.onCreateOptionsMenu(menu);
}
Here is my menu_main.xml:
<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">
<item
android:id="#+id/action_search"
android:icon="#android:drawable/ic_menu_search"
app:showAsAction="always"
app:actionViewClass="android.support.v7.widget.SearchView"
android:title="Search"/>
<item
android:id="#+id/action_wishlist"
android:orderInCategory="300"
android:title="#string/wishlist"
app:actionLayout="#layout/actionbar_badge_layout"
android:icon="#drawable/ic_wishlist_dark"
app:showAsAction="always"></item>
<item
android:id="#+id/action_cart"
android:orderInCategory="300"
android:title="#string/cart"
app:actionLayout="#layout/actionbar_badge_cart_layout"
android:icon="#drawable/ic_cart_dark"
app:showAsAction="always"></item>
</menu>
Here is the Screenshot:
1: first time:
enter image description here
2: normal:
enter image description here
Note: The problem occurs when the application opens with opening navigation bar, else its working fine. On createoptionsmenu is called at all times, but still its not working. Anyone can help on this?

More Precisely this should do the trick:
menu.clear() and return super.onCreateOptionsMenu(menu); does the trick! :)
#Override
public boolean onPrepareOptionsMenu(final Menu menu)
{
menu.clear();
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.home, menu);
return super.onCreateOptionsMenu(menu);
}
CHEERS! :)

I have cleared it by myself, by changing the code from "onCreateOptionsMenu" to "OnPrepareOptionsMenu". Also it is recommended to call Super class before clearing or inflating the menu.
Thanks for all.

Related

How to hide\show SearchView from Toolbar?

I have ToolBar and I have defined Searchview as an item:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
<item
android:id="#+id/action_search"
android:icon="#android:drawable/ic_menu_search"
app:showAsAction="never"
app:actionViewClass="android.support.v7.widget.SearchView"
android:title="Search"/>
</menu>
Then I have BaseActivity class where I define Toolbar:
protected final void onCreate(Bundle savedInstanceState, int layoutId)
{
super.onCreate(savedInstanceState);
setContentView(layoutId);
Toolbar myToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(myToolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
assert myToolbar != null;
}
#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, menu);
return true;
}
I have search activity where I show toolbar and I would like to show searchview. The problem is that APP is crashing with null pointer exception that it can't find SearchView action_search
public class SearchActivity extends BaseActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
SearchView searchview = (SearchView) findViewById(R.id.action_search);
searchview.setVisibility(SearchView.VISIBLE);
}
}
What is correct way to show\hide SearchView?
In your SearchActivity, use the below code:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
MenuItem menuItem = menu.findItem(R.id.action_search);
menuItem.setVisible(false);
return true;
}
(Kotlin)
Here is a simple way to show/hide searchview:
private lateinit var menuItem: MenuItem
private var mSearchView: SearchView? = null
private fun updateSearchUI(visible: Boolean) {
if (isChecked) {
menuItem.isVisible = true
}else{
menuItem.isVisible = false
}
}
On first load of Activity if you want to check some preferences and show/hide:
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
menuInflater.inflate(R.menu.menu, menu)
menuItem = menu!!.findItem(R.id.shortcut_search)
mSearchView = menuItem.actionView as SearchView
updateSearchUI(PreferenceManager.getVisibilityStatus(applicationContext))
return true
}
if you want to toggle searchView show/hide:
switch_on_off.setOnCheckedChangeListener { buttonView, isChecked ->
updateSearchUI(isChecked)
}
}
Please take note that onCreate is the first activity life cycle method to be called. onCreateOptionsMenu is triggered during (depending on your Android version) or after onCreate. With this in mind, when you findViewById(R.id.action_search) inside your onCreate method, it will yield a NULL object, thus the error.
Please check here for other observations by other developers.
What is correct way to show\hide SearchView?
You don't have to manually show/hide it. In your menu resource, you already have the option android:showAsAction="never" included. It means:
Never place this item in the app bar. Instead, list the item in the app bar's overflow menu.
By default, your search view item is already hidden. To show it, you need to click the overflow menu (three dots) on the toolbar.
Try below:
First: Change "app:showAsAction" attribute to "always"
<menu xmlns:android="http://schemas.android.com/apk/res/android"
<item
android:id="#+id/action_search"
android:icon="#android:drawable/ic_menu_search"
app:showAsAction="always"
app:actionViewClass="android.support.v7.widget.SearchView"
android:title="Search"/>
</menu>
Second: You can not access SearchView directly in onCreate. So remove below code.
SearchView searchview = (SearchView) findViewById(R.id.action_search);
searchview.setVisibility(SearchView.VISIBLE);
Third: You can access the SearchView when it is add to toolbar, i.e. when onCreateOptionsMenu is called.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
MenuItem mSearch = menu.findItem(R.id.action_search);
mSearchView = (SearchView) mSearch.getActionView();//Store the reference in global variable of Base activity.
//Do you customization here, like
mSearchView.setOnQueryTextListener(this);
return true;
}
Fourth: Now you have the reference to SearchView. So you can show/hide search view, like below:
if(mSearchView.isShown()) {
mSearchView.setVisibility(View.GONE);
} else {
mSearchView.setVisibility(View.VISIBLE);
}

Share Button on ActionBar Appears Twice

I've created a share button on my Action Bar - but it seems to appear twice.
The menu XML file is below:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/action_share"
android:title="#string/action_share"
app:showAsAction="always"
app:actionProviderClass="android.support.v7.widget.ShareActionProvider"
/>
</menu>
And it is instantiated in the onCreateOptionsMenu in the view.
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_detail, menu);
MenuItem menuItem = menu.findItem(R.id.action_share);
mShareActionProvider =
(ShareActionProvider) MenuItemCompat.getActionProvider(menuItem);
if(mShareActionProvider != null && !mForecastString.isEmpty()){
mShareActionProvider.setShareIntent(createShareForecastIntent());
} else{
Log.d(LOG_TAG, "Share Action provider is null?");
}
super.onCreateOptionsMenu(menu,inflater);
}
How could the share button appear twice if it is defined, inflated, and instantiated only once?
You are inflating Menu twice, both in the Activity and the Fragment.
Removing one inflation should fix the problem.
Just before inflating menu options use menu.clear();
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
menu.clear();
inflater.inflate(R.menu.menu_detail, menu);
MenuItem menuItem = menu.findItem(R.id.action_share);
mShareActionProvider =
(ShareActionProvider) MenuItemCompat.getActionProvider(menuItem);
if(mShareActionProvider != null && !mForecastString.isEmpty()){
mShareActionProvider.setShareIntent(createShareForecastIntent());
} else{
Log.d(LOG_TAG, "Share Action provider is null?");
}
super.onCreateOptionsMenu(menu,inflater);
}
This is happening due to repetitive inflation of menu.
use menu.clear() before inflation.
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
menu.clear();
inflater.inflate(R.menu.my_menu_layout, menu);
}

Search button in toolbar for fragment is not displaying keyboard

I have multiple fragments in an activity where I want a search button in the toolbar. In one of my fragments this all successfully works but when I copied the exact same code into my other two fragments they aren't functioning properly.
When I click the search button on the working fragment, the keyboard pops up and I can start entering text. But on the other two fragments when I press on the search button icon, it slides to the left and then I need to press it again for it to work. Anyone know how I can fix this?
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.search, menu);
MenuItem item = menu.findItem(R.id.action_search);
SearchView sv = new SearchView(((Home) getActivity()).getSupportActionBar().getThemedContext());
MenuItemCompat.setShowAsAction(item, MenuItemCompat.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW | MenuItemCompat.SHOW_AS_ACTION_IF_ROOM);
MenuItemCompat.setActionView(item, sv);
sv.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
list.clear();
for (int i = 0; i < mainList.size(); i++) {
if (mainList.get(i).getName().toLowerCase().startsWith(newText.toLowerCase(), 0) || mainList.get(i).getAddress().toLowerCase().startsWith(newText.toLowerCase())) {
list.add(mainList.get(i));
}
}
adapter.notifyDataSetChanged();
return false;
}
});
super.onCreateOptionsMenu(menu, inflater);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId())
{
case R.id.action_search:
getActivity().onSearchRequested();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Try to define searchView in menu item.
<item android:id="#+id/action_search"
android:icon="#drawable/ic_search_white_24dp"
android:title="#string/search"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="collapseActionView|always" />
Hey i have same issue and i found solution for it. it may be because of you are giving a whole menu for search view. so when you click first time on search icon that full menu gets load and then you again click on search icon and then search view gets open.
Try below code
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater)
{
inflater.inflate(R.menu.searchview, menu);
MenuItem item = menu.findItem(R.id.menu_item_search);
SearchView sv = (SearchView) item.getActionView();
if (sv != null){
sv.setSubmitButtonEnabled(true);
sv.setOnQueryTextListener(this);
}
super.onCreateOptionsMenu(menu, inflater);
}
searchview.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
app:showAsAction="always|collapseActionView"
android:icon="#android:drawable/ic_menu_search"
android:id="#+id/menu_item_search"
android:orderInCategory="1"
app:actionViewClass="android.support.v7.widget.SearchView"
android:title="Search"
android:iconifiedByDefault="true"/>
</menu>
And make sure you import android.support.v7.widget.SearchView otherwise it will give error in casting.
Hope it will help.

android collapsable searchview with custom header

I need the suggestion for using the best way to show searchview with custom header.
My headerlayout should initially look like this
And after clicking the search button on the right of the header, it must replace header title and menu icon part with the search input field like this.
pressing back in this state, should direct to initial state.
How can I do this?
in Menu Resource directory
search_country_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/action_search"
android:icon="#drawable/abc_ic_search"
app:showAsAction="always|collapseActionView"
android:orderInCategory="0"
app:actionViewClass="android.support.v7.widget.SearchView"/>
</menu>
**In Activity's onCreateOptionmenu Method **
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.search_country_menu, menu);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
menu.findItem(R.id.action_search).expandActionView();
searchView.setQueryHint(getResources().getString(R.string.select_country));
searchView.setImeOptions(EditorInfo.IME_ACTION_DONE);
searchView.setOnQueryTextListener(new OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String arg0) {
searchView.clearFocus();
return true;
}
#Override
public boolean onQueryTextChange(String arg0) {
etSearch.setText(arg0);
return true;
}
});
return super.onCreateOptionsMenu(menu);
}
Do you want to customize the SearchViewor is a standard one fine for you?
If it is I suggest looking into the ActionBar with an ActionView
Android ActionBar documentation about adding an ActionView

SearchView (Support) in FragmentActivity always null

I've started messing around with fragments, as my app on tablets could be using space more efficiently.
Now, in my old app, I had a SearchView in the actionbar menu, that would show on ifRoom. I'm using the same code, but now the SearchView item is always null. I'm not looking for changing the menu items depending on active fragments or whatever, I just need the SearchView to function from the FragmentActivity.
FragmentActivity:
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.main, menu);
MenuItem searchItem = menu.findItem(R.id.svPet);
this.svPet = (SearchView)MenuItemCompat.getActionView(searchItem);
if(this.svPet != null)
{
this.svPet.setInputType(InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
this.svPet.setOnQueryTextListener(this);
}
return true;
}
main.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/svPet"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="ifRoom"
android:title="#string/search"
android:titleCondensed="#string/search">
</item>
...Some more items that are never shown as action and work correctly
</menu>
So, whenever the menu is being created, this.svPet stays null, the searchview is not displayed in the bar (even when there's more than enough room), and when I click on the item from the menu, my application crashes, saying there's a nullpointer on
this.svPet.setIconified(false);
in
#Override
public boolean onOptionsItemSelected(MenuItem item)
Any ideas on what might be wrong? I'm probably overlooking something, but I just don't see what's wrong at the moment. Thanks in advance :)
some ideas
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
//SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
SearchView searchView = (SearchView) searchItem.getActionView();
if (searchView != null) {
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String s) {
// do something with s, the entered string
query = s;
Toast.makeText(getApplicationContext(), "String entered is " + s,Toast.LENGTH_SHORT).show();
return true;
}
#Override
public boolean onQueryTextChange(String s) {
return false;
}
});
}
return super.onCreateOptionsMenu(menu);
}
menu.xml
<item android:id="#+id/action_search"
android:orderInCategory="5"
android:title="Search"
android:icon="#drawable/ic_action_search"
android:showAsAction="ifRoom|collapseActionView"
android:actionViewClass="android.widget.SearchView" />
</menu>
So... after some digging I found that when using MenuItemCompat in combination with FragmentActivity, getActionView(searchItem) returns null. I think this is a bug in the support library, so I'll have to submit a bugreport for that. (Done, https://code.google.com/p/android/issues/detail?id=76141&thanks=76141&ts=1410649918)
My workaround:
Instead of
this.svPet = (SearchView)MenuItemCompat.getActionView(searchItem);
I used
this.svPet = new SearchView(this);
MenuItemCompat.setShowAsAction(searchItem, MenuItemCompat.SHOW_AS_ACTION_IF_ROOM);
MenuItemCompat.setActionView(searchItem, this.svPet);
--Edit--
After some more looking around, I found that you can just use ActionBarActivity instead of FragmentActivity, as ActionBarActivity extends FragmentActivity. When using that, you can use
this.svPet = (SearchView)MenuItemCompat.getActionView(searchItem);
as usual.

Categories

Resources