Toolbar.inflateMenu seems to do nothing - android

Im currently messing arround with the new AppCompat library bringing material design to older devices.
Setting a toolbar as actionbar works fine for me, but the toolbar seems to not do anything on calling inflateMenu(int resId). From the docs, i thought this is to replace getMenuInflater().inflate(int resId) called from onCreateOptionsMenu.
If I do the latter, the menu items are correctly inflated and added to the toolbar, but inflateMenu seems to to nothing.
What am I missing?
Activity Code:
Toolbar toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.inflateMenu(R.menu.main); // this does nothing at all
setSupportActionBar(toolbar);
}
// this works
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
Thanks in advance!

If you are calling setSupportActionBar() you don't need to use toolbar.inflateMenu() because the Toolbar is acting as your ActionBar. All menu related callbacks are via the default ones. The only time you need to call toolbar.inflateMenu() is when you are using the Toolbar as a standalone widget. In this case you will also have to handle menu item click events via
toolbar.setOnMenuItemClickListener(
new Toolbar.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
// Handle menu item click event
return true;
}
});

Related

How use setOnClickListner on "custom" toolbar's menu items?

I want to set OnClick behaviour on menu items of custom toolbar in an activity. I found many answers of same thing but on the actionbar which comes from theme, no answer was found for clicking items of menu of custom material toolbar.
Note: I don't want to add Image button on toolbar.
Any help will be appreciated.
It is the same as with standard ActionBar.
1) Replace ActionBar with your own material toolbar like so:
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
...
2) Override OnCreateOptions menu as usual:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_layout,menu);
super.onCreateOptionsMenu(menu, inflater);
}
3) Handle clicks:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.register: {
...
return true;
}
default:
return super.onOptionsItemSelected(item);
}
}

What is the equivalent of onPrepareOptionsMenu for support Toolbar?

I know that if I have a support v7 Toolbar set on my Activity I can make changes to the overflow menu right before it is displayed by overriding onPrepareOptionsMenu()
However I have a standalone support v7 Toolbar. I still want to display an overflow menu and be able to update it right before it is opened. I know how to display it...
Toolbar toolbar = ...
toolbar.inflateMenu(R.menu.my_menu);
toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
// TODO
return false;
}
});
... but how can I make changes right before the overflow menu is open?

Trying to hide a menuitem in Android, not behaving as expected

I have the below menu item created and is showing always as an action, instead of being hidden in an overflow menu.
<item android:id="#+id/menu_refresh_network"
android:orderInCategory="100"
android:icon="#drawable/ic_menu_action_refresh_network"
android:title="#string/title_refresh_network"
android:enabled="true"
app:showAsAction="always" />
I need it to be hidden when ThreshVoteIntentService.mOpportunistic is TRUE. I have a receiver which is alerted by intent when that value has changed. It's a very simple procedure that invalidates the menu.
IntentFilter filterModeUpdated = new IntentFilter(resources.getString(R.string.action_mode_updated));
mModeUpdatedReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
invalidateOptionsMenu();
}
};
this.registerReceiver(mModeUpdatedReceiver, filterModeUpdated);
I have my options menu then (supposedly) redraw in onPrepareOptionsMenu.
#Override
public boolean onPrepareOptionsMenu (Menu menu) {
menu.findItem(R.id.menu_refresh_network).setVisible(ThreshVoteIntentService.mOpportunistic);
return super.onPrepareOptionsMenu(menu);
}
What actually happens is that the visibility of menu_refresh_network only changes when I open the overflow menu. Have I basically misunderstood how this should work?
onPrepareOptionsMenu() is only called, as you say, just before the overflow menu is show, so that's too late.
You need to call setVisible(false) on the correct MenuItem before invalidating the options menu. So in your onCreateOptionsMenu(), find a reference to the MenuItem with findItem() for the item id that you want to change and store that in a member variable. Then, when the broadcast arrives, change the visibility of that item and then invalidate the options menu.
Here is a sample activity based off a new project I created in Android Studio to demonstrate this. If you run this, you can see that the menu item disappears after 5 seconds, and no invalidate is even required to force the menu to refresh.
public class MainActivity extends AppCompatActivity {
private MenuItem menuItem;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// Hide the menu item in 5 seconds
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
menuItem.setVisible(false);
}
}, 5000);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
menuItem = menu.findItem(R.id.action_settings);
MenuItemCompat.setShowAsAction(menuItem, MenuItemCompat.SHOW_AS_ACTION_ALWAYS);
return true;
}
}
If you are using AppCompatActivity (as app:showAsAction implies), you should use supportInvalidateOptionsMenu() in place of invalidateOptionsMenu() as invalidateOptionsMenu() does not know about always visible action items (hence, why it only calls onPrepareOptionsMenu() when the menu is opened)

How to open Side bar on icon click in Android?

I have implemented Hamburger bar with App toolbar and both of them are working fine. Following is the snapshot of toolbar and hamburgerbar:
Hamburger bar
I can open this bar by sliding it but I also want to make it open by clicking on drawable icon (right top corner icon). How can i do that?
MainActivity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowHomeEnabled(true);
drawerFragment = (FragmentDrawer)
getSupportFragmentManager().findFragmentById(R.id.fragment_navigation_drawer);
drawerFragment.setUp(R.id.fragment_navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout), toolbar);
drawerFragment.setDrawerListener(this);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
return super.onOptionsItemSelected(item);
}
I don't think so that I need to do some changes in layout files. What do I have to add in MainActivity file to make it possible?
I am newbie in Android code. Any help will be appreciable.
Using the Toolbar component should be fairly easy to achieve this by using a similar code to this:
Toolbar toolbar = (Toolbar) findViewById(R.id.home_toolbar);
toolbar.inflateMenu(R.menu.menu_home);
toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
if (item.getItemId() == R.id.action_settings) {
mDrawerLayout.openDrawer(Gravity.RIGHT); // where mDrawerLayout is your android.support.v4.widget.DrawerLayout in your activity's xml layout.
}
return false;
}
});
EDIT:
The key component here is the menu_home.xml file which goes to your res/menu folder. You can add your desired menu item there, customize it's icon and even more, add as many items as you'd like to have on the right side of the toolbar(Obviously handle the openDrawer() method on whichever menu item you need - the recommended one is the rightmost though).
Use the openDrawer() method.
private DrawerLayout mDrawerLayout;
...
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
...
mDrawerLayout.openDrawer(Gravity.END); // or whatever gravity the of the drawer you want to open
Use Activity's onOptionsItemSelected(MenuItem menuItem) method:
First of all, keep the reference to your DrawerLayout in a class field:
DrawerLayout drawerLayout;
Somewhere in onCreate put this:
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout)
And implement the method:
#Override
public boolean onOptionsItemSelected(MenuItem menuItem) {
// if you want the default back/home hamburger menu item just put android.R.id.home instead
if (menuItem.getItemId() == R.drawable.icon_navigation) {
drawerLayout.openDrawer(GravityCompat.END);
}
return super.onOptionsItemSelected(menuItem);
}

Gmail tablet like Actionbar items

I'm trying to build an app with a split actionbar/toolbar like in the Gmail app.
Is there any view element for this behaviour or do I have to write such a toolbar myself?
The search icon is moving with the master fragment when opening the slidingDrawer.
To accomplish this you can add one of the new Toolbar widgets to each of your fragments layouts. The new Toolbar class was designed to be much more flexible than a traditional Actionbar and will work well in this split design. This post is a good overview for implementing a standalone Toolbar. For posterity's sake I've included the sample code for it below.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.blah);
Toolbar toolbar = (Toolbar) findViewById(R.id.my_awesome_toolbar);
// Set an OnMenuItemClickListener to handle menu item clicks
toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
// Handle the menu item
return true;
}
});
// Inflate a menu to be displayed in the toolbar
toolbar.inflateMenu(R.menu.your_toolbar_menu);
}

Categories

Resources