How to show menu icon in Actionbar? - android

I've added a navigation drawer to my main activity. Now I would like to have a menu icon next to action bar title. So I did the following:
mDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, R.string.drawer_open, R.string.drawer_close)
{
#Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
invalidateOptionsMenu();
}
#Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
invalidateOptionsMenu();
}
};
mDrawerToggle.setDrawerIndicatorEnabled(false);
mDrawerToggle.setHomeAsUpIndicator(R.drawable.ic_drawer);
mDrawerToggle.setToolbarNavigationClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (dlDrawer.isDrawerVisible(GravityCompat.START)) {
dlDrawer.closeDrawer(GravityCompat.START);
} else {
dlDrawer.openDrawer(GravityCompat.START);
}
}
});
mDrawerToggle.syncState();
I also have:
getActionBar().setDisplayShowHomeEnabled(true);
getActionBar().setHomeButtonEnabled(true);
I cannot use getActionBar().setHomeAsUpIndicator(R.drawable.ic_drawer); because my min API level is 16.
This is the xml for the menu:
<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" tools:context="com.app.activities.MainActivity">
<item android:id="#+id/action_settings"
android:title="#string/action_settings"
android:orderInCategory="200" app:showAsAction="never" />
</menu>
However, with the above code, the menu icon is not being displayed next to the app icon. And tapping on the app icon has no reaction at all.
What code do I need to add to have the menu icon show and the activity react to the tapping of the app icon?
EDIT
To get the app icon clicking to work I added the following code in onOptionsItemSelected:
int id = item.getItemId();
if (id == android.R.id.home) {
if (dlDrawer.isDrawerVisible(GravityCompat.START)) {
dlDrawer.closeDrawer(GravityCompat.START);
} else {
dlDrawer.openDrawer(GravityCompat.START);
}
}
However, the menu icon is still not being displayed.

change your code as follow
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
R.drawable.ic_drawer, //nav menu toggle icon
R.string.app_name, // nav drawer open - description for accessibility
R.string.app_name // nav drawer close - description for accessibility
){
public void onDrawerClosed(View view) {
getActionBar().setTitle(mTitle);
// calling onPrepareOptionsMenu() to show action bar icons
invalidateOptionsMenu();
}
public void onDrawerOpened(View drawerView) {
getActionBar().setTitle(mDrawerTitle);
// calling onPrepareOptionsMenu() to hide action bar icons
invalidateOptionsMenu();
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
for option menu
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// toggle nav drawer on selecting action bar app icon/title
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
// Handle action bar actions click
switch (item.getItemId()) {
case R.id.action_settings:
return true;
default:
return super.onOptionsItemSelected(item);
}
}
/***
* Called when invalidateOptionsMenu() is triggered
*/
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
// if nav drawer is opened, hide the action items
boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
menu.findItem(R.id.action_settings).setVisible(!drawerOpen);
return super.onPrepareOptionsMenu(menu);
}

Related

How to open Navigation Drawer when click on navigation drawer icon

How to open Navigation Drawer when click on navigation drawer icon.
I want open Navigation Drawer like this
![Navigation Drawer][1]
I added a menu but still not showing
toggle = new ActionBarDrawerToggle(
DashBordActivity.this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
if (navigationView != null) {
setupDrawerContent(navigationView);
}
#Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawerLayout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.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();
//noinspection SimplifiableIfStatement
return super.onOptionsItemSelected(item);
}
#SuppressWarnings("StatementWithEmptyBody")
//#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawerLayout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
public void setupToolbar(String title) {
setSupportActionBar(toolbar);
getSupportActionBar().setTitle(title);
toolbar.setTitleTextColor(Color.BLACK);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
private void setupDrawerContent(NavigationView navigationView) {
//revision: this don't works, use setOnChildClickListener() and setOnGroupClickListener() above instead
navigationView.setNavigationItemSelectedListener(
new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
menuItem.setChecked(true);
drawer.closeDrawers();
return true;
}
});
}
navigation drawer is not open when i click on the drawer how to solve this problem
is there any code is missing in my code.
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
toolbar.setNavigationIcon(R.drawable.menu);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
drawer.openDrawer(START);
}
});
Try this,
<item
android:id="#+id/nav_camera"
android:icon="#drawable/ic_menu_camera"
android:title="#string/import" />
</group>
Try this:
This tutorial is very help full like:-
Add the navigation drawer layout
set icon and text in drawer layout
I refer this link
https://www.androidhive.info/2013/11/android-sliding-menu-using-navigation-drawer/
it is very helpful try this

Toolbar's up caret opens nav menu

I'm using the v7.widget.Toolbar in my app, but I'm getting some funky functionality. I have my main activity and fragments that are placed over it. When there are no fragments on the backStack, the hamburger button shows and the menu works correctly. When I add a fragment to the backStack, the up caret shows correctly, however when I click the up caret, the nav menu opens instead of the fragment being popped off the stack.
Now if there's a real answer, I'll take it, but at this point I will take a hackish solution. I tried adding a listener so I knew when the action bar button was hit, but that just made it so the fragment popped, the page went back, but the nav menu still opened. onOptionsItemSelected is not being called (due to the way I implemented the Drawer Toggle, but doing it the "correct" way gave me way more problems, such as no nav menu showing on the main page at all).
To sum it up for clarity: The up caret is opening the nav menu, instead of going back.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTitle = getTitle();
toolbar = (Toolbar) findViewById(R.id.tool_bar); // Attaching the layout to the toolbar object
frameLayout = (FrameLayout) findViewById(R.id.frame_layout);
setSupportActionBar(toolbar);
//Listen for changes in the back stack
getSupportFragmentManager().addOnBackStackChangedListener(new FragmentManager.OnBackStackChangedListener() {
#Override
public void onBackStackChanged() {
shouldDisplayHomeUp();
}
});
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerContent = findViewById(R.id.drawer_content);
mDrawerList = (ListView) findViewById(R.id.drawer_list);
mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
mDrawerList.setAdapter(new DrawerListItemAdapter(DRAWER_ITEMS, getApplicationContext()));
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, toolbar, R.string.openDrawer, R.string.closeDrawer) {
public void onDrawerClosed(View view) {
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
public void onDrawerOpened(View drawerView) {
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
Log.d("Main", "Open Menu");
}
};
mDrawerToggle.syncState();
mDrawerLayout.setDrawerListener(mDrawerToggle);
}
#Override
public void onBackStackChanged() {
shouldDisplayHomeUp();
}
public void shouldDisplayHomeUp(){
//Enable Up button only if there are entries in the back stack
boolean canback = getSupportFragmentManager().getBackStackEntryCount()>0;
ActionBar ab = getSupportActionBar();
if(ab != null){
ab.setDisplayHomeAsUpEnabled(canback);
}
if(!canback){
//App can crash as mDrawerToggle will be null when app launches
try{
mDrawerToggle.syncState();
}catch (Exception e){
e.printStackTrace();
}
}
Log.d("Main", "shouldDisplayHomeUp");
}
#Override
public boolean onSupportNavigateUp() {
//This method is called when the up button is pressed. Just the pop back stack.
Log.d("Main", "Up carat pressed");
getSupportFragmentManager().popBackStack();
return true;
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
mDrawerToggle.syncState();
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Pass any configuration change to the drawer toggls
mDrawerToggle.onConfigurationChanged(newConfig);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
Log.d("Main", "Menu item clicked: " + Integer.toString(item.getItemId()));
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
The solution that worked for me
A combination of Alex' answer (including his comment in the answer) below and this hacky answer.
You can use setToolbarNavigationClickListener() - it sets the listener that handles clicks when drawer indicator is disabled
drawerToggle.setToolbarNavigationClickListener((View view) -> {
getSupportFragmentManager().popBackStack();
});

How to change the icon navigation drawer closed or opened

My question is very clear that i want to change the Up icon When NavigationDrawer open and closed. So please tell me how to Change the upp icon in anction bar. I have tried so much but i m not able to do. ii have used custom layout to set the title. Thanks in advance
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
actionBar.setCustomView(R.layout.center_action_bar_text);
actionBar.setTitle("dvds");
View view = actionBar.getCustomView();
TextView textView = (TextView) view.findViewById(R.id.title);
textView.setText("zfdgfdg");
actionBar.setDisplayShowHomeEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
---------------------------------
drawerListView.setAdapter(drawerListAdapter);
DrawerLayout drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
drawerToggle = new ActionBarDrawerToggle(HomeActivity.this,
drawerLayout, R.drawable.abc_ic_clear_search_api_holo_light, R.string.drawer_open,
R.string.drawer_close){
#Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
}
#Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
}
};
drawerLayout.setDrawerListener(drawerToggle);
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
drawerToggle.syncState();
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (drawerToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
Add this method to your code:
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
drawerToggle.onConfigurationChanged(newConfig);
}
i have other one answer
just add this method:-
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
// if nav drawer is opened, hide the action items
boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
//boolean drawerOpen1 = mDrawerLayout.isDrawerOpen(this.slider);
//menu.findItem(R.id.sliding).setVisible(!drawerOpen);
if(drawerOpen)
{
menu.findItem(R.id.sliding).setIcon(android.R.drawable.ic_menu_close_clear_cancel);
}
else {
menu.findItem(R.id.sliding).setIcon(R.drawable.menuicon);
}
//menu.findItem(R.id.sliding).setIcon(R.drawable.ic_launcher);
//menu.findItem(R.id.action_settings).setVisible(!drawerOpen1);
return super.onPrepareOptionsMenu(menu);
}
for that method call you need to use this tow method
invalidateOptionsMenu();
// calling onPrepareOptionsMenu() to hide action bar icons
when menu is open
public void onDrawerOpened(View drawerView) {
getActionBar().setTitle(mDrawerTitle);
//mDrawerLayout.openDrawer(Gravity.END);
// calling onPrepareOptionsMenu() to hide action bar icons
invalidateOptionsMenu();
}
when menu is close
public void onDrawerClosed(View view) {
getActionBar().setTitle(mTitle);
//menu.findItem(R.id.sliding).;
//mDrawerLayout.closeDrawer(Gravity.END);
// calling onPrepareOptionsMenu() to show action bar icons
invalidateOptionsMenu();
}

How to show the NavigationDrawer icon in the ActionBar with AppCompat Library

I have a problem with the NavigationDrawer icon, I want it to be visible within the ActionBar. It is needed to show the user, that he can open the drawer in my app by swiping from the left edge of the screen.
I use this code before without any problems, but in this application it isn't working well, I don't know what the problem can be. Please help me.
Here is my code:
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.left_drawer);
ArrayList<Integer> smIcon_adrs = new ArrayList<Integer>();
smIcon_adrs.add(R.drawable.sm_font);
smIcon_adrs.add(R.drawable.sm_size);
smIcon_adrs.add(R.drawable.sm_count);
smIcon_adrs.add(R.drawable.sm_about);
ArrayAdapter<Integer> sm_adapter = new smIcoAdapter(getBaseContext(),
smIcon_adrs);
mDrawerList.setAdapter(sm_adapter);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
R.drawable.ic_drawer, R.string.hello_world, R.string.app_name) {
/** Called when a drawer has settled in a completely closed state. */
public void onDrawerClosed(View view) {
getSupportActionBar().setTitle("دعا عهد");
supportInvalidateOptionsMenu();
super.onDrawerClosed(view);
}
/** Called when a drawer has settled in a completely open state. */
public void onDrawerOpened(View drawerView) {
getSupportActionBar().setTitle("امکانات");
supportInvalidateOptionsMenu();
super.onDrawerOpened(drawerView);
// onPrepareOptionsMenu()
}
};
mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
mDrawerLayout.setDrawerListener(mDrawerToggle);
and here:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
mDrawerToggle.syncState();
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_play:
if (mp.isPlaying()) {
item.setIcon(R.drawable.actn_play);
mp.pause();
} else {
updateProgressBar();
mp.start();
item.setIcon(R.drawable.actn_stop);
}
break;
}
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
public boolean onPrepareOptionsMenu(Menu menu) {
mDrawerLayout.isDrawerOpen(mDrawerList);
// menu.findItem(R.id.action_settings).setVisible(!drawerOpen);
return super.onPrepareOptionsMenu(menu);
}
Here is someone with a similar problem, you might test the solution posted there: https://stackoverflow.com/a/23332975/1738838.
According to that you are missing this method:
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}

Hide ActionBar MenuItems when Navigation Drawer slides for any amount

I'm trying to implement a Navigation Drawer that hides the menu items in the ActionBar whenever the drawer is opened.
I am following google's documentation, however their code does not produce the expected behavior.
http://developer.android.com/training/implementing-navigation/nav-drawer.html
Using this code, the menu items are hidden when the drawer becomes completely opened , and shown when the drawer becomes completely closed.
However, the Gmail app behaves differently. The menu items are hidden as soon as the drawer opens by any amount. This is the behavior I want. Does anyone know how to achieve this?
Thanks!
Have you tried this:
Use invalidateOptionsMenu() whenever you toggle the nav drawer, by measuring the sliding offset.
Iterate over each menu item in onPrepareOptionsMenu(Menu menu) and hide it.
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
// If the nav drawer is open, hide action items related to the content view
boolean drawerOpen = shouldGoInvisible;
hideMenuItems(menu, !drawerOpen);
return super.onPrepareOptionsMenu(menu);
}
private void hideMenuItems(Menu menu, boolean visible)
{
for(int i = 0; i < menu.size(); i++){
menu.getItem(i).setVisible(visible);
}
}
Detecting how much the nav drawer has slided:
mDrawerLayout.setDrawerListener(new DrawerListener(){
float mPreviousOffset = 0f;
#Override
public void onDrawerClosed(View arg0) {
super.onDrawerClosed(arg0);
shouldGoInvisible = false;
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
#Override
public void onDrawerOpened(View arg0) {
super.onDrawerOpened(arg0);
shouldGoInvisible = true;
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
#Override
public void onDrawerSlide(View arg0, float slideOffset) {
super.onDrawerSlide(arg0, slideOffset);
if(slideOffset > mPreviousOffset && !shouldGoInvisible){
shouldGoInvisible = true;
invalidateOptionsMenu();
}else if(mPreviousOffset > slideOffset && slideOffset < 0.5f && shouldGoInvisible){
shouldGoInvisible = false;
invalidateOptionsMenu();
}
mPreviousOffset = slideOffset;
}
#Override
public void onDrawerStateChanged(int arg0) {
// or use states of the drawer to hide/show the items
}});
Note: shouldGoInvisible is class field.
If you want to override action bar as soon as drawer enters screen and restore action bar the moment drawer is no longer visible (exactly how Gmail behaves as of March 20, 2014) you can use the following code:
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
R.drawable.ic_drawer, R.string.drawer_open, R.string.drawer_close) {
#Override
public void onDrawerStateChanged(int newState) {
super.onDrawerStateChanged(newState);
boolean isOpened = mDrawerLayout.isDrawerOpen(mDrawerList);
boolean isVisible = mDrawerLayout.isDrawerVisible(mDrawerList);
if (!isOpened && !isVisible) {
if (newState == DrawerLayout.STATE_IDLE) {
// drawer just hid completely
restoreActionBar();
} else {
// } else if (newState == DrawerLayout.STATE_SETTLING) {
// drawer just entered screen
overrideActionBar();
}
}
}
private void restoreActionBar() {
getSupportActionBar().setTitle(mTitle);
supportInvalidateOptionsMenu();
}
private void overrideActionBar() {
getSupportActionBar().setTitle(mDrawerTitle);
supportInvalidateOptionsMenu();
}
};
// Set the drawer toggle as the DrawerListener
mDrawerLayout.setDrawerListener(mDrawerToggle);
Modify restoreActionBar() and overrideActionBar() methods acording to your needs.
There is no need to distinguish between swipes and home button and no need to measure swipe lengths.
Variation
If you don't want to reference the drawer list view use the following code instead:
boolean isOpened = mDrawerLayout.isDrawerOpen(GravityCompat.START);
boolean isVisible = mDrawerLayout.isDrawerVisible(GravityCompat.START);
You may want to use GravityCompat.END instead depending on what you specified in XML layout.
Edit - concerning actions
The above example does not hide action bar items relevant to content below the navigation drawer. To do so or show different set of icons when drawer is visible you have to keep track of whether the drawer is opened or closed manually.
In addition to the above code declare private boolean mDrawerVisible = false with proper save/restore state handling.
Then modify mDrawerToggle inner methods as follows:
private void restoreActionBar() {
getSupportActionBar().setTitle(mTitle);
mDrawerVisible = false;
supportInvalidateOptionsMenu();
}
private void overrideActionBar() {
getSupportActionBar().setTitle(mDrawerTitle);
mDrawerVisible = true;
supportInvalidateOptionsMenu();
}
Finally in onCreateOptionsMenu inflate different menu resource or in onPrepareOptionsMenu show/hide different actions based on the value of mDrawerVisible.
I'd half agree with Nikola but it's sufficient just to update the icons when the drawer state has
Create a global variable to keep track of the drawer state:
private int mDrawerState;
Set a new DrawerListener:
mDrawerLayout.setDrawerListener(new DrawerListener() {
#Override
public void onDrawerStateChanged(int state) {
mDrawerState = state;
invalidateOptionsMenu();
}
#Override
public void onDrawerSlide(View view, float slide) {
// TODO Auto-generated method stub
}
#Override
public void onDrawerOpened(View view) {
// TODO Auto-generated method stub
}
#Override
public void onDrawerClosed(View view) {
// TODO Auto-generated method stub
}
});
Update the menu visibility:
boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawer);
for(int i=0;i<menu.size();i++){
// If the drawer is moving / settling or open do not draw the icons
menu.getItem(i).setVisible(mDrawerState!=DrawerLayout.STATE_DRAGGING &&
mDrawerState!=DrawerLayout.STATE_SETTLING && !drawerOpen);
}
I have a better solution for this question:
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
if (navigationDrawerFragment.isDrawerOpen()) {
menu.clear();
}
return super.onPrepareOptionsMenu(menu);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
if (!navigationDrawerFragment.isDrawerOpen()) {
// Only show items in the action bar relevant to this screen
// if the drawer is not showing. Otherwise, let the drawer
// decide what to show in the action bar.
showLocalContextActionBar();
return false;
}
return super.onCreateOptionsMenu(menu);
}
If you want to hide all menu items, simply use:
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
return showActionBarMenu; // boolean value, set it in drawer listeners as class variable
}
Then you do not need to visible of each menu item.
I took the answer by #Laurence Dawson and simplified it a bit. This solution does not require any class members to be used.
Execute this code during onCreate():
DrawerLayout drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
drawerLayout.setDrawerListener(new DrawerLayout.DrawerListener() {
#Override
public void onDrawerSlide(View view, float v) {
invalidateOptionsMenu();
}
#Override
public void onDrawerClosed(View view) {
invalidateOptionsMenu();
}
#Override
public void onDrawerOpened(View view) {}
#Override
public void onDrawerStateChanged(int state) {}
});
And override this method:
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
DrawerLayout drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
boolean actionsVisibility = !drawerLayout.isDrawerVisible(Gravity.START);
for(int i=0;i<menu.size();i++){
menu.getItem(i).setVisible(actionsVisibility);
}
return super.onPrepareOptionsMenu(menu);
}
Few notes:
The above implementation assumes that the view associated with NavigationDrawer has its layout_gravity set to start in XML.
Unrelated to OP's question, but annoying: there seems to be some kind of a bug which causes the drawer to stuck along the way. If you do observe this behavior, here is the solution: Android Navigation Drawer bug using the sample
I have different code but the same solution:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
hideMenuItems(menu, !mShouldGoInvisible);
return super.onCreateOptionsMenu(menu);
}
private void hideMenuItems(Menu menu, boolean visible){
for(int i = 0; i < menu.size(); i++){
menu.getItem(i).setVisible(visible);
}
}
and
#Override
public void onNavigationDrawerListener(boolean opened, int position) {
if (opened){
mShouldGoInvisible = true;
invalidateOptionsMenu();
} else {
mShouldGoInvisible = false;
invalidateOptionsMenu();
}
}

Categories

Resources