I have an app with no home/title bar, and an actionbar in tab navigation mode. I've added a menu to my app, which I want to show in Android 4+ as an overflow menu in the tab actionbar.
It sounds well and simple, but when I add the menu
// Initiating menu XML file
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.layout.menu, menu);
return true;
}
// Event handling for individual menu item selected
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_search:
// Load search fragment
return true;
case R.id.menu_add:
// Load add fragment
return true;
default:
return super.onOptionsItemSelected(item);
}
}
I get this in landscape view, which is good:
But I get this in portrait mode, which is bad:
How do I prevent the tab actionbar from splitting in portrait mode?
EDIT: So it appears that when a menu is created in android 4, it immediately becomes an action overflow menu in the activity's titlebar. The problem is, I don't have a titlebar, just a tab navigation actionbar. So the activity creates a new title/action bar with an action overflow option.
I need to find a way to make the activity add the action overflow tab to the tab navigation actionbar. Is there a way to do it?
Thanks
Related
I have an activity in an android app with fragments displayed using a pager.
One of the three fragments shows a menu item (always displayed as action) and an overflow menu, the two others fragments show in the app bar only the first menu item (but not the overflow menu).
My problem is that when I switch from one tab to another, the menu is not updating smoothly.
Is it possible to add animations to menu inflation ?
Has anyone ever encountered such an issue and how did you handle it ?
Here is the app bar :
And here is how I inflate the menu inside the fragment, of course, the other fragment is inflating another XML file.
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.home_menu, menu);
}
Please feel free to ask for more details ;-)
Thanks for helping !
In you fragment please write this line in your oncreateview mehod:
setHasOptionsMenu(true);
and implement this mehod:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_referesh, menu);
super.onCreateOptionsMenu(menu,inflater);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.refresh:
pageNumber=1;
getWebServiceData1();
return false;
}
return super.onOptionsItemSelected(item);
}
So.. I'm facing a problem that has been driving me crazy for the past hours.
I have an App using the AppCompact v21 and toolbar. I also handle back navigation using:
getSupportActionBar().setDefaultDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
In combination with the parent activity on the manifest. Which works perfect....
My problem is:
I have an activity with 3 tabs with a viewpager and I need one of the fragments to have it's own menu.
I can inflate the menu just fine but once the menu is inflated the back arrow in that fragment don't work anymore. In the other 2 fragments of the view pager the back navigation through the toolbar still works.
Inside my fragment:
// Inside onCreate...
this.setHasOptionsMenu(true);
// Later on somewhere else...
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.menu_submit, menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
// my menu logic goes here.
return true;
}
Any suggestions?
When you always return true in onOptionsItemSelected(), that means you've handled every menu item possible (including the Up button). You should instead return super.onOptionsItemSelected(item) in cases where you do not handle one of your items:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// Your menu logic such as
case R.id.your_menu_item:
// Do something
return true;
default:
return super.onOptionsItemSelected(item);
}
}
I'm trying to add an 'Add Item' button at the top in the action bar. (To the right of the App Icon and Title).
Right under the action bar, I have two tabs that I can swipe between. I also have a menu XML file defined for the settings menu.
I thought actionbar uses a menu XML as well. So I added a actionbar menu XML, but when I use
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_SHOW_TITLE | ActionBar.DISPLAY_SHOW_CUSTOM);
actionbar.setCustomView(R.menu.actionbar);
my program crashes. I believe I'm doing this totally incorrectly.
My actionbar XML:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/item1" android:icon="#android:drawable/ic_menu_add"></item>
</menu>
I read on some tutorials that I'm supposed to add items to the actionbar and populate it via the OnCreateOptionsMenu function in mainActivity. But that's where my options menu is populated, not my actionbar.
An activity populates the ActionBar in its onCreateOptionsMenu() method.
Instead of using setcustomview(), just override onCreateOptionsMenu like this
#Override
public boolean onCreateOptionsMenu(Menu menu){
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.mainmenu, menu);
return true;
}
If an actions in the ActionBar is selected, the onOptionsItemSelected() method is called. It receives the selected action as parameter. Based on this information you code can decide what to do for example:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menuitem1:
Toast.makeText(this,"Menu Item 1 selected",Toast.LENGTH_SHORT).show();
break;
case R.id.menuitem2:
Toast.makeText(this,"Menu item 2 selected",Toast.LENGTH_SHORT).show();
break;
default:
break;
}
return true;
}
I am developing one application in which I'm using Sherlock Actionbar library for 4.0 effect in lower device, in this app there are 5 items in Menu and it is appear at option menu. When I press menu in actionbar its getting me the item list correctly but when I click on Overflow menu button(hardware menu)it doesn't give me any option.I need all items in both menu as it is.
I have tried with android:showAsAction="never" and it appears in overflow menu but not in option menu that appear in actionbar. and if I do android:showAsAction="ifRoom|withText" then it only appears in option menu in action bar not in Overflow menu so can you please find the solution for this.
Have you overridden your OnCreateOptionsMenu()? You need this for menu button support.
This is mine:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater mi = getMenuInflater();
mi.inflate(R.menu.activity_main, ((MenuWrapper) menu).unwrap());
return true;
}
Edit: and onOptionsItemSelected(), too?
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_logout:
logout();
return true;
case R.id.menu_settings:
openSettings();
return true;
case android.R.id.home:
getSlidingMenu().toggle();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
I use a sherlockfragmentactivity which has a ViewPager, two Fragment and an ActionBar displaying two tabs. I populate the actions items from the fragment but i have to call the method
setHasOptionsMenu(true);
in order than the fragment display the Actionbar MenuItem.
But when the screen oriention change, the MenuItems are not displayed.
Here is my code to create a option menu in my first fragment :
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
menu.add(R.string.action_refresh).setIcon(R.drawable.ic_menu_refresh)
.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
refresh();
return false;
}
}).setShowAsActionFlags(MenuItem.SHOW_AS_ACTION_IF_ROOM);
menu.add(R.string.action_add_a_file).setIcon(R.drawable.ic_menu_upload)
.setShowAsActionFlags(MenuItem.SHOW_AS_ACTION_IF_ROOM);
menu.add(R.string.action_settings).setIcon(android.R.drawable.ic_menu_preferences)
.setIntent(new Intent(getSherlockActivity(), SettingsActivity.class))
.setShowAsActionFlags(MenuItem.SHOW_AS_ACTION_NEVER | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
}
Is there a way to properly add item to the action bar from a SherlockFragment and how can i keep these item in ActionBar after screen orientation change ?
add following line in your FragmentActivity in the manifeast file
android:configChanges="orientation"
orientation is automatically handled by the system itself. but i don't know weather it is a efficient way or not.