I have added two menu item . Both of them works good but whenever i press menu button new menu items appear beside the old one . Like below u can see
below is my menu item xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:title="Search"
android:icon="@drawable/search_white_24dp"
android:id="@+id/searchmenu"
app:showAsAction="always">
</item>
<item
android:icon="@drawable/settings_white_24dp"
android:title="Setting"
android:id="@+id/settingmenu"
app:showAsAction="always"
/>
</menu>
and here is my code
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.searchmenu:
editText = DuwaManager.openSearchBox(this,
getSupportActionBar(), "DuwaListView");
break;
case R.id.settingmenu:
break;
case R.id.home:
NavUtils.navigateUpFromSameTask(this);
break;
}
please help where i am wrong ?
Sorry this will get to long but , I have second Question
2) My second question i have another menu. xml in which there is share icon in place of search icon , But when i am trying to show on action bar with same code menu item does not show on action bar . below is my another menu xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
>
<item
android:title="Share"
android:icon="@drawable/share_white"
android:id="@+id/share_tarika"
app:showAsAction="always"
/>
<item android:title="Setting"
android:icon="@drawable/settings_white_24dp"
android:id="@id/settingmenu"
app:showAsAction="always"/>
</menu>
and its code
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.tarika_menu, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.share_tarika:
break;
case R.id.settingmenu:
break;
}
return super.onOptionsItemSelected(item);
}
The best to do is as this :
if you have just one menu. Then set menu from Activity page and return true then no need to clear menu. and no duplicate menu will appear.
But if you use two fragment and have to change activity menu as per fragment then only need to clear menu and reset it with new one.
And if you are using onCreateOptionsMenu(Menu menu) in fragment then remove them.
Try this
use menu.clear()