How to prevent ActionBar items get hidden when Navigation Drawer opens? - android

I have implemented Navigation Drawer with ActionBar which has only one item which opens the navigation drawer. So both the Home and menu item buttons open the drawer. And all works fine with no crashes.
But when I click on the ActionBar Home button or menu item, the Navigation drawer opens and menu item becomes invisible.
How can I prevent this? I want it be always visible, regardless if drawer is open or closed.
I tried multiple solutions already. For example, in the drawer class, method setHasOptionsMenu(true); is called in onCreate. Then I set menu item to be present always.
Then in the main Activity, I tried to capture the menu item during the creation
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
try {
MenuItem item = menu.getItem(0);
if (item != null) {
item.setVisible(true);
}
} catch (Exception e) {
Log.e(TAG, "Error: " + e.getLocalizedMessage());
}
return super.onPrepareOptionsMenu(menu);
}
but when drawer appears, the menu object return with 0 items.
I am out of ideas.
EDIT
#Override
public boolean onCreateOptionsMenu(Menu menu) {
if (!mNavigationDrawerFragment.isDrawerOpen())
getMenuInflater().inflate(R.menu.main, menu);
restoreActionBar();
return true;
}
return super.onCreateOptionsMenu(menu);
}
public void restoreActionBar() {
ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayShowHomeEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(false);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (item != null && id == android.R.id.home) {
if (mNavigationDrawerFragment.isDrawerOpen(Gravity.RIGHT)) {
mNavigationDrawerFragment.closeDrawer(Gravity.RIGHT);
} else {
mNavigationDrawerFragment.openDrawer(Gravity.RIGHT);
}
return true;
}
return super.onOptionsItemSelected(item);
}
This is RIGHT-SIDE Navigation drawer.

this is your problem
#Override
public boolean onCreateOptionsMenu(Menu menu) {
if (!mNavigationDrawerFragment.isDrawerOpen())
getMenuInflater().inflate(R.menu.main, menu);
restoreActionBar();
return true;
}
return super.onCreateOptionsMenu(menu);
}
you dont have a menu when the drawer opens, remove that if statement

Related

Hide optionmenu

I have a option Menu in my toolbar in app.but I want to Is unseen Some places.
What solution do you recommend to it my friends?
Toolbar toolbar=(Toolbar)findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
//codes...
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.optionmenu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.item1) {
return true;
}
return super.onOptionsItemSelected(item);
}
TO:
Just remove onCreateOptionsMenu and onOptionsItemSelected method from activity.
try : Open menu xml using ctrl + click on R.menu.optionmenu in your code. Delete all "item" in xml, if you want to add menu items afterwards.
or
try : remove onCreateOptionsMenu(Menu menu) and onPrepareOptionsMenu(Menu menu) from Activity if you don't want to use action bar menus at all.
You can change this code and try again :
#Override
public boolean onCreateOptionsMenu(Menu menu) {
if("Your condition")
{
getMenuInflater().inflate(R.menu.optionmenu, menu);
}
return true;
}

Android Tabs and Different ActionBar Menu Items

I have a TabGroup in my app. How can I have different actions items for each Tab ? I just found an example from here, but it is not enough for me.
Can someone provide some hints or at least a link ?
I'm using SlidingTabLayout, and this is the way i changed the Toolbar icons for each Tab.
I've 3 tabs, each tab got its own fragment, and in each fragment I've created the following:
Tab 1 fragment:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.tab_1_menu, menu);
}
Tab 2 fragment:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.tab_2_menu, menu);
}
And so on, hope this help .
Full Example with getItemId / Click
#Override
public boolean onCreateOptionsMenu(Menu menu) {
//Set The Menu View
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
//Menu Items, every fragment menu item
//Must have different ID
if (id == R.id.settings) {
//Do Something here
);
return true;
}
if (id == R.id.About) {
//Do Something here
);
return true;
}
if ( id == R.id.exit)
{
finish();
return true;
}
return super.onOptionsItemSelected(item);
}

Action bar items not working with Drawer Android

My Action bar items were working fine until I added a menu drawer, now when the activity is first displayed, the switch button on the menu doesn't work and as soon as the drawer opens it starts working absolutely fine. Although I have not called it in my onDrawerOpened method.
// ----------For Options Menu-------------------
#Override
public boolean onCreateOptionsMenu(Menu menu) {
try {
super.onCreateOptionsMenu(menu);
menu.clear();
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main_screen_menu, menu);
menuitem1 = menu.findItem(R.id.menu_item1);
menuitem2 = menu.findItem(R.id.menu_item2);
final Switch getView = (Switch) menuitem2.getActionView();
getView.setChecked(false);
getView.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton arg0,
boolean isSelected) {
if (isSelected) {
method1();
} else {
method2();
}
}
});
} catch (Exception e) {
e.printStackTrace();
Log.e("OnCreateOptionsMenu", "exception", e);
}
mOptionsMenu = menu;
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(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.menu_item1: {
return true;
}
case R.id.menu_item2: {
return true;
}
default:
return super.onOptionsItemSelected(item);
}
}
Once I open and close the Drawer than the switch is working perfectly!
Can anyone please help?
Thanks in advance.
You're supposed to call super.onCreateOptionsMenu(menu) after having inflated the menu (as shown in the docs : http://developer.android.com/guide/topics/ui/actionbar.html)
I would change your code to :
// ----------For Options Menu-------------------
#Override
public boolean onCreateOptionsMenu(Menu menu) {
try {
menu.clear();
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main_screen_menu, menu);
(...)
return super.onCreateOptionsMenu(menu);
}
(Not totally sure that it could fix your issue, but it's worth the try)

How to hide buttons on actionbar of certain fragments,where fragments are part of navigation drawer

I have a MainActivity on that I have a navigation drawer,Lists of navigation drawers are fragments.
I want to have refresh button on one of my fragment,to add refresh button on action bar i added following code in my MainActivity
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.activity_main_actions, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// toggle nav drawer on selecting action bar app icon/title
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
if (item.getItemId() == R.id.action_settings) {
return true;
} else if(item.getItemId() == R.id.action_refresh) {
if(haveNetworkConnection(getApplicationContext()))
{
LoadAllProducts task = new LoadAllProducts(ListDepartmentSemesterActivity.this);
task.execute();
}else{
Toast.makeText(getApplicationContext(), "No Internet Connection", Toast.LENGTH_LONG).show();
}
return true;
}else {
return super.onOptionsItemSelected(item);
}
}
But the thing is on every fragment refresh button is visible and from any fragments i can refresh products,how to hide it on other fragments???
Navigation drawer MainActivity and Fragments share same action bar???
Define onOptionsItemSelected(MenuItem item) in fragment and add your refresh button to it. And insert this in your fragment:
#Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
setRetainInstance(true);
}
if you want handle menu button in each fragment try this code
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.simple_calendar_view,
container, false);
// you code here
setHasOptionsMenu(true);
return rootView;
}
inside same fragment you want handle its menu item clicks
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_refresh:
// you Custom code here
// Example i make method called Refresh_event
Refresh_event();
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
if you need hide some menu item when you open slide menu navigation
in you activity that host fragments
public void onDrawerClosed(View view) {
// calling onPrepareOptionsMenu() to show action bar icons
supportInvalidateOptionsMenu();
}
public void onDrawerOpened(View drawerView) {
// calling onPrepareOptionsMenu() to hide action bar icons
supportInvalidateOptionsMenu();
}
/* *
* Called when invalidateOptionsMenu() is triggered
*/
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
// if nav drawer is opened, hide the action item(About App && Refresh)
boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
menu.findItem(R.id.about_app).setVisible(!drawerOpen);
menu.findItem(R.id.refresh).setVisible(!drawerOpen);
return super.onPrepareOptionsMenu(menu);
}

Menu is null when activity gets recreated

I have inflated the action bar menu in the base activity and modifying the action bar items in my base activity. But When i change the orientation, menu is always null and thats why it doesn't update my action bar items. Why it null?
Here is my tried code:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.base, menu);
mMenu = menu;
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// switch (item.getItemId()) {
if (item.getItemId() == R.id.action_refresh) {
refreshNewsData();
return true;
}
if (item.getItemId() == R.id.action_share) {
sharenewsData();
return true;
}
if (item.getItemId() == R.id.action_favorite) {
addFavorites();
return true;
}
return super.onOptionsItemSelected(item);
}
I have written this code in base activity.
protected void showIcons(int id) {
if (mMenu != null) {
MenuItem item = mMenu.findItem(id);
item.setVisible(true);
}
}
protected void hideIcons(int id) {
if (mMenu != null) {
MenuItem item = mMenu.findItem(id);
item.setVisible(false);
}
I'm using these methods to update the action bar menu items
You cannot update ActionBar Menu items directly. Whenever you want to update menu items, make a call to invalidateOptionsMenu() this'll redraw the action bar menus and will make a call to onPrepareOptionsMenu(). You'll have to take care of the menu items you want to show / hide inside onPrepareOptionsMenu().

Categories

Resources