How to create the Action Overflow part of the ActionBar? - android

I have been looking online for a while, and there are very little tutorials on how to do this. Even the google docs have very vague info on how to put this stuff up there! I am sure its subtle, but I can't understand the terminology because I am still fairly new to Android. I would like to be able to get the Action Overflow icon to the right side of the ActionBar. Putting the view control was pretty understandable using the docs and example code when creating a project, but it does not have one for the Action Overflow. Thanks in advance!
Edit: I should probably elaborate. I would like the menus to default being under the action overflow. I found an eye opener answer to a similar question, but it only tells you how to put the menus at the top. How can I force them to go under the list? Is it even possible to force that? Thanks!

If you use API-11, it's not problem. If lower I am inviting you to this topic :
The best way to create drop down menu in android 2.x like in ICS
In the case when API-11 and higher you must :
Create menu xml like this :
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/item_refresh"
android:icon="#drawable/ic_menu_refresh"
android:title="Refresh"
android:showAsAction="ifRoom|withText" />
<item
android:id="#+id/item_save"
android:icon="#drawable/ic_menu_save"
android:title="Save"
android:showAsAction="ifRoom|withText" />
</menu>
And create code like this :
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// gets the activity's default ActionBar
ActionBar actionBar = getActionBar();
actionBar.show();
actionBar.setDisplayHomeAsUpEnabled(true);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu); //inflate our menu
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item){
switch(item.getItemId()) {
case R.id.item_refresh:
//click on refresh item
break;
case R.id.item_save:
//click on save item
break;
}
return true;
}
Good Luck!

Related

Option menu in Android

I am new to developing Android apps.
I would like to add a settings option menu. The requirement for the app is that when you click settings in the option menu the screen will show a sentence Add beep after ___ counts.
How can I do that, please?
Its on the OnCreateOptions and OnOptionsItemSelected, Plus you need to manipulate the menu folder / menu xml also. Try to read about them and instantiating them is as easy as a switch case, as well as for the functions.
You will need a separate activity/fragment to handle Add beep after ___ counts
Here is the flow, read a little bit more on these. You can find lot of examples on each of these:
Create Main Activity with a menu.xml that has a settings action
Create another activity or a dialog fragment with a layout of
<textview> <edittext> <textview>. This is to show the Add beep
after ___ counts. In MainActivity, on menu action "settings", show this fragment.
You can save the count persistently using preferences
Hope this helps.
mainmenu.xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/action_settings"
android:title="#string/settings_label"
android:orderInCategory="100"
app:showAsAction="never" />
</menu>
Activity:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.mainmenu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id) {
case R.id.action_settings:
Toast.makeText(context, "Add beep after ___ counts.", duration).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
For more information read this Menu

How to set visibility for an actionbar menu group?

UPDATE
Originally I was using ActionBarSherlock I have since created a brand new project using a native android action bar just to test this and I am still getting the same problem.
I am successfully showing/hiding items but not groups. I am rapidly coming to the conclusion that there is a bug in the ActionBar and it is not possible to programmatically set the visibility of a group
END of UPDATE
Given the following menu When accessing the Group I get a null pointer exception
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/action_settings"
android:title="#string/settings"
android:orderInCategory="100"
android:showAsAction="never"/>
<group android:id="#+id/mnu_text_group"
android:visible="false">
<item android:id="#+id/mnu_text_type"
android:enabled="true"
android:visible="true"
android:icon="#drawable/ic_action_text_icon"
android:showAsAction="always">
</item>
<item android:id="#+id/text_color"
android:enabled="true"
android:visible="true"
android:showAsAction="always"
android:icon="#drawable/ic_action_color_line">
</item>
</group>
<item android:id="#+id/mnu_images"
...
In the onPrepareOptionsMenu of the relevant activity I have
public boolean onPrepareOptionsMenu(Menu menu) {
MenuItem mnuTextGroup = menu.findItem(R.id.mnu_text_group);
mnuImage.setEnabled(mEnableImageMenu);
mnuTextGroup.setVisible(false);
...
The call to mnuTextGroup.setVisible(false); raises a null pointer exception
However, by changing the find method to find an item within the group works fine e.g. MenuItem mnuTextGroup = menu.findItem(R.id.mnu_text_type);but obviously this works just for the specific item. I know groups are designed for exactly this purpose, to be able to set the visibility of and enable/disable all items within the group but I have been unable to find a way to do this programatically.
Finally found the solution
I needed to use the setGroupVisible() method of the menu object passed into the onPrepareOptionsMenu() method
This is what worked for me
Instead of
MenuItem mnuTextGroup = menu.findItem(R.id.mnu_text_group);
mnuImage.setEnabled(mEnableImageMenu);
mnuTextGroup.setVisible(false);
This is what I needed
menu.setGroupVisible(R.id.mnu_text_group, false);
Simple and one line
navigationView.getMenu().setGroupVisible(R.id.groupstaff, false);
Use In Activity Where You Want to Hide
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getSupportMenuInflater().inflate(R.menu.main_itemlist, menu);
boolean isdown = false;
menu.findItem(R.id.addwork).setVisible(isdown);
MenuItem mnuTextGroup = menu.findItem(R.id.mnu_text_group);
mnuTextGroup.setVisible(isdown);
return true;
}
altering the options menu needs to be done within onPrepareOptionsMenu or else it sometimes doesn't work (not sure exactly why, hopefully someone else can elaborate):
#Override public boolean onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
// set visibility of menu items here
return true;
}

Android no menu items showing in Action Bar

I am developing an Android app and I am trying to put a menu item into the ActionBar.
It has enough space, so it shouldn't be on the overflow or anything.
In my menu.xml I have added that item + android:showAsAction="ifRoom|withText"
However, no matter how large the screen is, that damn menu will not show up on the ActionBar (although it is present in the menu, if the user presses a key).
Unfortunately I cannot post any full-code since I am under a non-disclosure agreement, but I will answer all questions.
The section where I inflate the menu:
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.drinks, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
case R.id.done:
//stuff
}
return super.onOptionsItemSelected(item);
}
Since you have only a menu item, use this attribute instead: android:showAsAction="always".
EDIT Above works all the time if you're running the code on post Honeycomb. But, in order to run on pre-Honeycomb, according to developer article, you need to extend from ActionBarActivity, that means adding compatibility support v4 & v7 and set the following theme for your activity:
<activity android:theme="#style/Theme.AppCompat.Light" ... >
... or a Them.AppCompat theme. Or use one of your own that extends from these.

Menu button is not show in 3.0 OS

I have created menu button which have two functions bookmark and home button.
This is working well in all the android version without android 3.0
do here any ways ? so my menu button will be display also in android 3.0 with all the version.
my code:-
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.menu_bookmark:
db.updateContact(new Contact(itemN,imageStatus));
return true;
case R.id.home_page:
Intent i = new Intent(imageTouchs.this, Comics.class);
startActivity(i);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.layout.menu, menu);
return true;
}
and my androidmanifest.xml :-
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="15" />
menu.xml :
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/menu_bookmark"
android:title="Bookmark"
android:showAsAction="ifRoom|withText" />
<item android:id="#+id/home_page"
android:title="Home"
android:showAsAction="ifRoom|withText" />
</menu>
any menu is not display in action bar
The "overflow' button will only show up when there isn't a physical menu button on the device. This is the standard behavior for the ActionBar.
You can actually set the value of your AVD to specifically not have a menu button when you are configuring the image.
Here is a SO question addressing this same thing: Action bar overflow not displayed
If you are talking about the OverFlowMenu icon, to the best of my knowledge, it cannot be achieved (forced) using the standard Android Support Library.
If you must have an OverFlowMenu (forced), you will need to use the ActionBarSherlock library. Go through a couple of my answers where I have a few fairly detailed suggestions on how to achieve that:
https://stackoverflow.com/a/13307583/450534
https://stackoverflow.com/a/13180285/450534
NOTE: As already mentioned in both my answers linked above, this is not recommended to ensure users get a seamless UI on their devices. And also, if you must absolutely force the OverFlowMenu, you will need to use an older version of ABS, which is again, not recommended.

How to add my menu button in system bar in Android 3.1?

My pad is SAMSUNG GT-P7510.I want to add a new menu in the system bar.But the menu shows in action bar.Like this:
But now it is like this:
It's in the right of top.
public class TestActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(0,0,1,"OK");
return true;
}
}
my guess is you started off with a pre-honeycomb app and ran it on 3.*+ emulator.
it happens automatically if you use the right layout style/theme.
on eclipse just go to your layout and choose "android 3.0" on the top right corner.
hope it helps.
EDIT:
after you edited your question I understand i got your question wrong.
If I understand correctly you are just trying to show the menu item as a button outside the menu list and that's simple -
on the xml use the "showAsAction" option like so-
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/menu_share"
android:icon="#drawable/ic_menu_share"
android:title="#string/menu_share"
android:alphabeticShortcut='o'
android:showAsAction="ifRoom|withText" />
</menu>
inflating it with
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.list_options_menu, menu);
or by code:
MenuItem item = menu.add("OK");
item.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
Please remove targetSdkVersion from manifest.xml file..
like from this format:
make it like:
Thanks,
Ram

Categories

Resources