Android – add action buttons to the action bar of a clip
My application has three tags, and I'm using a pager to switch between these tags. Each tab has its own segment. I use the optionsmenu method to add the settings and help action buttons to the action bar in the main activity. Now, I want to add a new action button to the action bar, But only for the first tab and the first clip, and when they appear in their tabs, I don't want it to appear on other clips
The main menu I created in the main activity has this layout-
<item android:id="@+id/action_settings"
android:title="@string/action_settings"
android:orderInCategory="100" app:showAsAction="never" />
<item android:id="@+id/help"
android:title="@string/help"
android:orderInCategory="100" app:showAsAction="never" />
</menu>
This menu is displayed correctly and works properly
I have another menu, menu_ prelaunch_ Fragment is used for the first fragment-
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="@+id/clear_form"
android:title="Clear"
android:icon="@drawable/ic_action_action_delete"
app:showAsAction="always" />
</menu>
Then add it to the action bar in the first fragment using the following code-
@Override
public void onCreate(Bundle savedInstance) {
Log.d(TAG, "onCreate");
super.onCreate(savedInstance);
setHasOptionsMenu(true);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflator) {
Log.d(TAG, "onCreateOptionsMenu");
inflator.inflate(R.menu.menu_prelaunch_fragment, menu);
super.onCreateOptionsMenu(menu, inflator);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
Log.d(TAG, "onOptionsItemSelected");
if (item.getItemId() == R.id.clear_form)
clearFragmentData();
return super.onOptionsItemSelected(item);
}
The problem is that this added button does not disappear when I go to another clip on another page / tab. There are no option menu codes in the other clips
Then I add this code to other snippets to remove a button
@Override
public void onCreate(Bundle savedInstanceState) {
Log.d(TAG, "onCreate");
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
@Override
public void onPrepareOptionsMenu (Menu menu) {
menu.findItem(R.id.clear_form).setVisible(false);
super.onPrepareOptionsMenu(menu);
}
However, no matter which tab is selected, the clear button now does not appear in the action bar
How do I add an action button in the action bar for one of my three tags (i.e. three clips)? This tab (clip) should only be displayed when it is selected (displayed)
thank you!
resolvent:
A simpler way is to include the menu item in the active menu item and show and hide it programmatically according to the tab selection. Get the reference of the menu item in the oncreateoptionsmenu method and keep it available externally
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
checkOut = (MenuItem) menu.findItem(R.id.action_abc);
return true;
}
Use addonpagechangelistener on viewpager to change the visibility of menu items
mViewPager.addOnPagechangelistener(new ViewPager.OnPagechangelistener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
if (position == 0) {
menuItem.setVisible(true);
} else {
menuItem.setVisible(false);
}
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
I haven't tried it myself