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.
Related
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
I'm trying to make a dynamic action bar in android, right now I'm trying to make a search in the action bar like this.
http://developer.android.com/images/ui/actionbar-searchview#2x.png
However when I click the the search button in the action bar nothing happens.
Here is the menu XML file:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res-auto" >
<item android:id="#+id/action_search"
android:title=""
android:icon="#drawable/abc_ic_search"
android:showAsAction="ifRoom|collapseActionView"
android:actionViewClass="android.support.v7.widget.SearchView"/>
<item android:id="#+id/action_menu"
android:title=""
android:icon="#drawable/abc_ic_menu"
android:showAsAction="always"
android:actionViewClass="android.support.v7.widget.ShareActionProvider"/>
Here is the OnCreateOptionsMenu in the Activity
#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);
MenuItem searchItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
return true;
}
I know the icon I have in the action bar is working because I have this code working without problems
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
case R.id.action_search:
Toast.makeText(getBaseContext(), "Search icon is working", Toast.LENGTH_LONG).show();
return true;
case R.id.action_menu:
Toast.makeText(getBaseContext(), "Menu icon is working", Toast.LENGTH_LONG).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
I think it has something to do with this:
android:actionViewClass="android.support.v7.widget.SearchView"/>
Because even when I delete the android.support.v7 jar on the project (I just deleted it to test, I have it back again) I can execute the project without any errors. So maybe the XML file doesn't recognise the android.support.v7 path?
P.S: the action_menu icon doesn't work either.
I know the icon I have in the action bar is working because I have this code working without problems
That is consuming the event, preventing the SearchView from working. Please delete both case sections of this method, as they are incorrect for both of your menu items.
(and, please use this, not getBaseContext(), unless you know what getBaseContext() does)
Here is a sample app showing the use of SearchView.
Additionally android.support.v7.widget.SearchView throws NullPointerException at onCreateOptionsMenu() with default configuration.
Solved by changing it to android.widget.SearchView
The answer by #CommonsWare is valid also. It is more likely to be the correct answer.
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.
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!
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