I would like to change the menu items displayed in my ActionBar depending on the current destination of my NavController. By "change" I mean inflate a specified menu-xml for each destination.
The behavior could be compared to the integrated way the new navigation system changes the ActionBar title, depending on the given android:label for the destination fragment in the navigation-xml.
So far I setup a basic activity with ActionBar and DrawerLayout using the new Android navigation. Also I created all necessary XML files and Fragments to navigate between.
...
#Override
protected void onCreate(Bundle savedInstanceState)
{
...
this._drawerLayout = this.findViewById(R.id.drawer_layout);
Toolbar toolbar = this.findViewById(R.id.action_bar);
this.setSupportActionBar(toolbar);
ActionBar actionbar = Objects.requireNonNull( this.getSupportActionBar() );
actionbar.setDisplayHomeAsUpEnabled(true);
actionbar.setHomeAsUpIndicator(R.drawable.ic_menu);
NavigationView navigationView = this.findViewById(R.id.navigation_view);
navigationView.setNavigationItemSelectedListener(menuItem -> {
menuItem.setChecked(true);
this._drawerLayout.closeDrawers();
return true;
});
NavController navController = Navigation.findNavController(this, R.id.navigation_host);
NavigationUI.setupWithNavController(toolbar, navController, this._drawerLayout);
}
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Here I inflate the ActionBar menu that is kept between all destinations.
// Instead of keeping the same menu between all destinations I want to display different menus depending on the destination fragment.
this.getMenuInflater().inflate(R.menu.actionbar_items, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch(item.getItemId())
{
case android.R.id.home:
this._drawerLayout.openDrawer(GravityCompat.START);
return true;
case R.id.appbar_search:
return true;
}
return super.onOptionsItemSelected(item);
}
I thought about using an individual toolbar in each destination fragment
but I discarded the idea, because I would lose the transition animation between hamburger icon and back-arrow icon.
Is there any way to achieve this with the new navigation system or in any other way?
I just tested this. With the current version (2.2.2) of the navigation library, menu items inflated by each fragment will be added and removed as you navigate around.
But onCreateOptionsMenu is not called by default! To make things work, you have to sprinkle the magic dust and call setHasOptionsMenu(true) during initialisation.
Beware, any menu items inflated by the containing activity will always appear alongside the ones from the current fragment, so if you don't want any common menu items, it's probably easiest to not inflate a menu in the Activity.
Side note: in the Fragment class, onCreateOptionsMenu(menu, inflater) is an empty method, so the call to super.onCreateOptionsMenu(menu, inflater) in your code is a no-op.
I found a way to achieve the desired behavior by calling menu.clear(); before inflating the updated layout. I am still wondering if there is a built-in way to achieve this with the new navigation system.
In the destination Fragment I am using this now:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater)
{
super.onCreateOptionsMenu(menu, inflater);
menu.clear();
inflater.inflate(R.menu.toolbar_items_idle, menu);
}
Related
My Android app has a navigation drawer with a menu. In the free version of the app, I want to have a menu item to allow the user to Upgrade to the paid version. Obviously I don't want this in the paid version.
How do I hide the menu item in the paid version?
The menu looks like this:
<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/action_home"
android:icon="#drawable/btn_home"
android:title="#string/nav_home"
/>
<item
android:id="#+id/action_acc_upgrade"
android:icon="#drawable/ic_star_black"
android:title="#string/str_acc_upgrade" />
<item
android:id="#+id/action_help"
android:icon="#drawable/ic_help_black"
android:title="#string/nav_help"
/>
</menu>
In the activity, I have:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
MenuInflater inflater = this.getMenuInflater();
inflater.inflate(R.menu.activity_main_drawer, menu);
if(isPaid()) {
MenuItem upgrade = menu.findItem(R.id.action_acc_upgrade);
upgrade.setVisible(false);
}
return true;
}
If I run this through the debugger, it reaches the line if(isPaid()) and then evaluates it as true, so goes into the setVisible part. The item still shows up in the menu, though.
I also tried removing the item from the menu instead of hiding it; the debugger shows that the item is removed, but then it still shows up when the menu is displayed.
How can I hide/remove this item?
Edit
I'm using a navigation drawer to hold the menu. This is set up in onCreate as follows:
// Set up toolbar
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// Set up navigation drawer
DrawerLayout drawer = findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.nav_drawer_open, R.string.nav_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
I suspect that this somehow bypasses onPrepareOptionsMenu etc? Is there a way of adding a listener which is called when the "open navigation drawer" button is clicked? I can only find callbacks for the drawer being opened or closed, and they're called after the drawer has moved - I need to call them before.
You can do it in onCreate() of your activity:
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
Menu navMenu = navigationView.getMenu();
navMenu.removeItem(R.id.action_acc_upgrade);
replace nav_view with your NavigationView's id.
There are several options:
The most convenient option: create 2 different productFlavours in your build.gradle file
productFlavors {
free {
...
}
paid {
...
}
}
And then override your menu file without action_acc_upgrade item for paid flavour.
If you want change visibility programmatically, try to use onPrepareOptionsMenu instead of onCreateOptionsMenu:
#Override
public void onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
if(isPaid()) {
MenuItem upgrade = menu.findItem(R.id.action_acc_upgrade);
upgrade.setEnabled(false);
upgrade.setVisible(false);
}
}
If you need to change your menu after creation, invoke invalidateOptionsMenu().
You should override onPrepareOptionsMenu like this:
#Override
public void onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
if(isPaid()) {
menu.findItem(R.id.action_settings).setVisible(false);
}
}
Notice:
Ensure get right value of isPaid() before onPrepareOptionsMenu callback, such as in onCreate() or onResume().
If it's on the fly, you need to override onPrepareOptionsMenu and call invalidateOptionsMenu if applicable:
#Override public void onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
menu.findItem(R.id.action_acc_upgrade).setVisible(!isPaid());
}
See Activity.onPrepareOptionsMenu:
Prepare the Screen's standard options menu to be displayed. This is
called right before the menu is shown, every time it is shown. You can
use this method to efficiently enable/disable items or otherwise
dynamically modify the contents.
How can I add action (icon) on Toolbar (AppConpat v21+)? Like 3-dot icon on right side.
I can create this action icons via onCreateOptionsMenu( Menu menu, MenuInflater inflater):
#Override
public void onCreateOptionsMenu( Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu( menu, inflater);
inflater.inflate( R.menu.menu, menu);
menu.clear();
MenuItem menuItemReload = menu.add( getResources().getString( R.string.sReload));
menuItemReload.setNumericShortcut( '0')
.setIcon( ResourcesCompat.getDrawable( getResources(), android.R.drawable.stat_notify_sync, getActivity().getTheme()));
if( Build.VERSION.SDK_INT >= 11) menuItemReload.setShowAsAction( MenuItem.SHOW_AS_ACTION_ALWAYS);
}
I not found any information inside getSupportActionBar() about add actions.
Is there exist different method add icons to Toolbar?
Did you set the AppCompatToolBar correctly in your AppCompatActivities onCreate()?
Toolbar toolBar = (Toolbar) findViewById(R.id.toolBar);
setSupportActionBar(toolBar);
After initializing it this way onCreateOptionsMenue() should work as intended.
I currently have an app which has 4 pages, these pages are being added using fragments by a pageViewer, so that I can swipe between them.
What I want, is to be able to access the 'options' menu (the one which has a 'settings' item in it by default) from each page, so that I can perform commands specifically for one page, such as a 'refresh' item, which refreshes the data in the current page.
Could anyone point me in the right direction?
Thanks!
Each Fragment can declare its own menu, which will be automatically merged with the current activity's menu.
class YourPage extends Fragment
{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// Signal that this fragment has proper actions
setHasOptionsMenu(true);
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater)
{
super.onCreateOptionsMenu(menu, inflater);
// The menu will be added to the action bar
inflater.inflate(R.menu.fragment_page_menu, menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.action_refresh:
{
// Handle the action...
return true;
}
default:
return super.onOptionsItemSelected(item);
}
}
}
Here's a complete example that I made, and also a relevant tutorial.
Why do I have to tell my activity what its layout should be twice?
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); // <--
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu); // <--
return true;
}
What's the difference between these two methods?. when should I use one, and when the other?
They are two separate things. The names tell you. R.layout.activity_main is your layout, R.menu.activity_main is for your menu.
setContentView() sets the layout for the Activity. It includes Buttons, TextViews, etc.
onCreateOptionsMenu() makes the menu that you see when you press the menu key or it populates the ActionBar on Android 3.0+.
They do two completetly separate things. setContentView() is often needed (unless you have an empty Activity), onCreateOptionsMenu() is optional, depending on if you need to show more options.
java file inside the gen folder there will be defined layout, ID and menu static class. you will get the idea from there.
I am trying an option menu in a class that extends ListAcitivity. How do I add an option menu to this activity, so when I click on the option button an options menu appears at the bottom of the screen.
I searched online but most tutorials refer to this function onCreateOptionsMenu() which is part of Activity class and it does not seem to work in the subclass of ListActivity.
It is Java. ListActivity (and anything that extends it) inherits all of the features and functions of Activity.
Therefore, just overriding onCreateOptionsMenu will work the same.
Boilerplate code:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_xml_name, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_item_id:
// action
break;
}
return true;
}
:)