I have a tabbed fragment I need two different actionbars / same action bar that could like the one shown in the image!
Page 1!
Page 2
After watching both images , you don't need two different actionbar instead have one action bar and two different menu.xml file , when fragment 1 is clicked go to the code of firstFragment class :
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_one, menu);//here menu_one.xml is called
return true;
}
and on second fragment call this in your secondFragmentclass
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_two, menu);//here menu_two.xml is called
return true;
}
both menu_one.xml and menu_two.xml will contain different item as per your wish
menu_one.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/someButton"
android:icon="#drawable/some_button"
android:title="#string/some_button"
android:showAsAction="always"/>
</menu>
menu_two.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/someButton2"
android:icon="#drawable/some_button2"
android:title="#string/some_button2"
android:showAsAction="always"/>
</menu>
Related
I have an activity that extends the ActionBarActivity that's included in the support package revision 18. I have a menu item that contains a submenu and it works fine when I load up the app. However, if I call supportInvalidateOptionsMenu() for some reason the submenu doesn't pop up any more. The related code would be the xml for the menu
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:myapp="http://schemas.android.com/apk/res-auto" >
<item
android:id="#+id/menu_search"
android:icon="#drawable/ic_search"
android:title="#string/menu_search"
myapp:actionViewClass="android.support.v7.widget.SearchView"
myapp:showAsAction="always|collapseActionView"/>
<item
android:id="#+id/menu_now_playing"
android:icon="#drawable/ic_nowplaying"
android:title="#string/menu_nowplaying"
myapp:showAsAction="always"/>
<item
android:id="#+id/menu_station_overflow"
android:icon="#drawable/ic_overflow"
android:title="#string/more"
myapp:showAsAction="always">
<menu>
<item
android:id="#+id/menu_favorite"
android:icon="#drawable/ic_favorite"
android:title="#string/favorite"/>
</menu>
</item>
</menu>
And then the code to create the menu
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_activity, menu);
return super.onCreateOptionsMenu(menu);
}
I should note that this problem occurs on Gingerbread devices but there are no problems on android 4.x. Does anyone have any idea what might be going on here?
Here is a work-around (as we had the same issue). Any menu items that need to be modified later, we put into instance variables, example:
private MenuItem stationMenuItem;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_activity, menu);
stationMenuItem = menu.findItem(R.id.menu_station_overflow);
return super.onCreateOptionsMenu(menu);
}
public void doStuff(boolean menuVisible) {
if (stationMenuItem != null) {
stationMenuItem.setVisible(menuVisible);
}
}
This is NOT an ideal solution, but something that will work till this is fixed. Changes to menu items SHOULD happen in onPrepareOptionsMenu(Menu menu) after calling supportInvalidateOptionsMenu()
Do not return super.onCreateOptionsMenu(menu); because that will always return false. Just return true.
I want to create a simple option menĂ¹ on the Action Bar (classic three dots). I wrote this part of code but nothing appear:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
return true;
}
Even this one but always nothing appear:
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(0, 0, 0, "Settings");
return super.onCreateOptionsMenu(menu);
}
There are no three dots on the Action Bar. Is there anything else to write maybe in the onCreate()?
Edit. The menu.xml
<xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:title="Info" android:id="#+id/settings"></item>
</menu>
Add the below line into the menu xml file
<item
android:title="Info"
android:showAsAction="never"
android:id="#+id/settings" >
</item>
check AndroidManifest.xml, set android:targetSdkVersion="10" or lowwer.
I've got a SherlockListActivity, and am trying to have a button in the action bar. Here's my menu.xml code:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android=">http://schemas.android.com/apk/res/android">
<item
android:id="#+id/add"
android:title="Add"
android:showAsAction="ifRoom" />
</menu>
Here's my java code to show the menu:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = this.getSupportMenuInflater();
inflater.inflate(R.layout.menu, menu);
return false;
}
But in my activity, the action bar shows, but with no button, and the menu button on my device does nothing. My device is running 2.3.3.
Thanks.
Try to return true from onCreateOptionsMenu().
I have been trying to add a button to the SherlockActionBar but I can't get it working.
This is the code that I have:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
android.view.MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, (android.view.Menu) menu);
return super.onCreateOptionsMenu(menu);
}
This is my menu.xml code:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/save_button"
android:title="i"
android:showAsAction="always" />
</menu>
This doesn't work, as even if I press the menu button, nothing shows up.
Is there any other way? Am I making any mistake?
You are using Android's Menu and MenuInflater, but should be using the classes that come with ActionBarSherlock:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
com.actionbarsherlock.view.MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.menu, (com.actionbarsherlock.view.Menu) menu);
return super.onCreateOptionsMenu(menu);
}
It seems like you are intermingling the two right now. Make sure that you import only com.actionbarsherlock.view.Menu and com.actionbarsherlock.view.MenuInflater, and not its Android counterparts. I recommend you to do something like the following:
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
...
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.menu, menu);
return super.onCreateOptionsMenu(menu);
}
I think in the menu.xml. Your item does not declare android:showAsAction attribute completely.
You must declare it like this:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/save_button"
android:title="i"
android:showAsAction="always|withText" />
</menu>
Since you did not specify any icon for the item action bar cannot display any item. By default icon are display than text.
The text doesn't show up in my android menu, only the icon. What can I do to fix this?
Thanks
XML menu file
<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/context_menu_favourites"
android:icon="#drawable/dash_fav_btn"
android:title="Favourites"
android:titleCondensed="Fav"
/>
</menu>
The inflater code in my activity
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.mainmenu, menu);
return true;
}
This sometimes happens when the icon is too big... as you just confirmed, that's the case. And no, if you want to use a bigger icon, you will have to implement a menu yourself.