Following the instructions at http://developer.android.com/guide/topics/ui/menus.html I try to add an option menu to my existing Activity by first creating an xml as
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/new_game"
android:icon="#drawable/ic_new_game"
android:title="#string/new_game"
android:showAsAction="ifRoom"/>
<item android:id="#+id/help"
android:icon="#drawable/ic_help"
android:title="#string/help" />
</menu>
and then inflating the menu in the activity as
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.game_menu, menu);
return true;
}
Apparently that is not sufficient. So what am I missing in the code? I am testing with Samsung S5.
My activity extends FragmentActivity, in case that matters.
The problem might be with the theme your activity is using. Try to use a theme which has actionbar in it. I suppose, Theme.Holo.ActionBar.
I'm sure the menu is there but you are not able to just see it. The above code, as far as I know, has no problem. The problem might be with the phones with a menu key and the ones without it. There have been infinite discussions over that here, here and here, and also here. One of the hacks that people use to force their menu options in the action bar overflow is
try {
ViewConfiguration config = ViewConfiguration.get(this);
Field menuKeyField = ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey");
if(menuKeyField != null) {
menuKeyField.setAccessible(true);
menuKeyField.setBoolean(config, false);
}
} catch (Exception ex) {
// Ignore
}
Put the above code in the onCreate() of your application class. You are using S5, so try long pressing the multitasking key. That enables the menu options in S5.
Related
First off, I'm running the latest Lollipop - Minimum and targeted SDK are both 22
minSdkVersion 22
targetSdkVersion 22
in the gradle build. I've changed/re changed these to 22, as well as rebuilt/invalidated caches/cleaned. I'm extending Activity in my main:
public class login_activity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login_layout);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.login_menu, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
return true;
}
}
And here are my menu items:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/login_help"
android:icon="#android:drawable/ic_menu_help"
android:title="#string/login_help_text"
app:showAsAction="ifRoom"/>
</menu>
When I run my code, it keeps showing the items in the ActionBar overflow menu. How do I fix this? Thanks!
Try replacing "ifroom" with "always" for showAsAction like below
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/login_help"
android:icon="#android:drawable/ic_menu_help"
android:title="#string/login_help_text"
app:showAsAction="always"/>
</menu>
I actually spent nearly five hours trying to figure this out, but I actually figured it out just now going through my gradle files. Here's how you fix it:
Go to your Gradle Scripts folder
Open the build.gradle file with the SDK options in it
Remove the line under dependencies (Mine's this one, not sure how they vary):
compile 'com.android.support:appcompat-v7:22.2.0' (REMOVE THIS)
Go back to your menu file and REMOVE the namespace definition for the namespace you put before showAsAction
xmlns:app="http://schemas.android.com/apk/res-auto" (REMOVE THIS)
Go change the showAsAction line from something:showAsAction to android:showAsAction
Rebuild & compile. It should work! I wish changing the SDK cleaned up after itself, but hey, what can you do.
I am trying to implement the SearchView ActionBar item as android developers says but I am having some trouble.
(http://developer.android.com/guide/topics/ui/actionbar.html).
There are two mistakes that although I have looked for a lot, I have not been able to find the solution.
1) I have a problem with the class MenuItemCompat. It says:
The method getActionView(MenuItem) is undefined for the type MenuItemCompat
I can only use for this class the following methods:
setShowAsAction(item, actionEnum)
setActionView(item, view)
Here it is the code
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.restloader, menu);
MenuItem searchItem = menu.findItem(R.id.search_menu);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
// Configure the search info and add any event listeners
return super.onCreateOptionsMenu(menu);
}
2) There is a problem with this:
xmlns:myapp="http://schemas.android.com/apk/res-auto"
I don't understand why it is used but if google says it, it must be appropriate.
Error message:
Multiple annotations found at this line:
- error: No resource identifier found for attribute 'actionViewClass' in package
'com.example.pruebahttp3'
- error: No resource identifier found for attribute 'showAsAction' in package
'com.example.pruebahttp3'
<?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/search_menu"
android:orderInCategory="100"
android:title="#string/search"
android:icon="#drawable/ic_search_category_default"
myapp:showAsAction="ifRoom|collapseActionView"
myapp:actionViewClass="android.support.v7.widget.SearchView">
</item>
Thank you very much!
i have got the same problem, i solved it by using the follow code. Be care of your namespace.`
<!-- Search, should appear as action button -->
<item
android:id="#+id/action_search"
android:icon="#drawable/abc_ic_search"
share:showAsAction="ifRoom"
share:actionViewClass="android.support.v7.widget.SearchView"
android:title="#string/abc_searchview_description_search" />
`
For the 1st:Fixing the second one will fix this :)
For the 2nd:
<?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" >
Change myapp to you application namespace com.xxx.xxx
Try to copy the lib files directly from yourFolder\sdk\extras\android\support\v7\appcompat\libs
I have a similar problem,but It occurs to me when i directly copy the JAR library file rather than following the android support library procedure. Try the opposite it might work for you.
Kinda weird if you ask me.
I added the SherlockActionBar to my project, added a few menu items and it works fine in the emulator (4.0.3), but it does not work on my phone (Galaxy S3); it just doesn't show up.
I'm targeting 15 with a minimum sdk version of 9 in my manifest:
<uses-sdk
android:minSdkVersion="9"
android:targetSdkVersion="15" />
<application
android:icon="#drawable/dimmiicon"
android:label="#string/app_name"
android:theme="#style/Theme.Sherlock.Light.DarkActionBar" >
Code is rather straightforward, but perhaps i'm missing something. Relevant bits...
import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.MenuItem;
public class Launcher extends SherlockFragmentActivity implements OnUserAuthenticatedListener, OnDialogChatterListener, OnAnimationCompleteListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_launcher);
final ActionBar ab = getSupportActionBar();
ab.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
ab.setCustomView(R.layout.title_bar);
ab.setStackedBackgroundDrawable(getResources().getDrawable(R.drawable.titlebarbackground));
TextView txt = (TextView) findViewById(R.id.myTitle1);
Typeface font = Typeface.createFromAsset(getAssets(), "caviar_dreams_bold.ttf");
txt.setTypeface(font);
TextView txt2 = (TextView) findViewById(R.id.myTitle2);
Typeface font2 = Typeface.createFromAsset(getAssets(), "caviardreams.ttf");
txt2.setTypeface(font2);
}
#Override
public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) {
getSupportMenuInflater().inflate(R.menu.commonmenus, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
return false;
case R.id.search_for_product:
performProductReview(findViewById(android.R.id.content));
return true;
case R.id.search_for_business:
performCompanyReview(findViewById(android.R.id.content));
return true;
case R.id.search_for_service:
performServiceReview(findViewById(android.R.id.content));
return true;
case R.id.search_for_person:
performPersonReview(findViewById(android.R.id.content));
return true;
default:
return super.onOptionsItemSelected(item);
}
}
And the commonmenus.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/menu_settings"
android:orderInCategory="100"
android:showAsAction="never"
android:title="#string/menu_settings"/>
<item android:id="#+id/search_for_product" android:showAsAction="never" android:orderInCategory="10" android:title="#string/menu_search_for_product"></item>
<item android:id="#+id/search_for_business" android:showAsAction="never" android:orderInCategory="11" android:title="#string/menu_search_for_business"></item>
<item android:id="#+id/search_for_service" android:showAsAction="never" android:orderInCategory="12" android:title="#string/menu_review_service"></item>
<item android:id="#+id/search_for_person" android:showAsAction="never" android:orderInCategory="14" android:title="#string/menu_review_person"></item>
<item android:id="#+id/view_my_reviews" android:showAsAction="never" android:orderInCategory="15" android:title="#string/menu_view_my_reviews"></item>
</menu>
I'm using the sherlock actionbar to customize the title using some custom font's and that is working on the emulator AND the phone, but the menu only shows up on the emulator. I did try it with the custom font code commented out and it made no difference.
Just so I'm clear: the 3 menu dots don't even show up.
After spending a couple of hours searching and looking for a clue, I'm at a loss. Any insight would be appreciated.
For devices with Android Honeycomb+ ActionBarSherlock will use stock ActionBar features to work with, which means you're left with official ActionBar, which only puts the legacy menu button in very specific scenarios, see here:
http://android-developers.blogspot.co.il/2012/01/say-goodbye-to-menu-button.html
Under "Action overflow button for legacy apps":
If your app runs on a device without a dedicated Menu button, the
system decides whether to add the action overflow to the navigation
bar based on which API levels you declare to support in the
manifest element. The logic boils down to:
If you set either minSdkVersion or targetSdkVersion to 11 or higher,
the system will not add the legacy overflow button.
Otherwise, the system will add the legacy overflow button when running
on Android 3.0 or higher.
The only exception is that if you set minSdkVersion to 10 or lower,
set targetSdkVersion to 11, 12, or 13, and you do not use ActionBar,
the system will add the legacy overflow button when running your app
on a handset with Android 4.0 or higher.
I'm having a rather odd issue where the menu for an activity works totally fine on real device but not on the emulator.
I tried launching one of my older projects in the emulator that I remember the menus working in, and it also failed. I suppose some sort of update has caused this?
The code is simple...
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuItem prefs = menu.add("Preferences");
prefs.setIcon(R.drawable.gear_01);
return true;
}
LogCat displays series of exceptions ultimately caused by:
E/AndroidRuntime(6714): Caused by: java.io.FileNotFoundException: res/drawable-hdpi/ic_menu_more.png
This actually happens not only in my applications but on the home screen to.
Any ideas for solutions?
Its hard to find error in your code.
So try this.Create one Folder in res named menu.Put this xml inside this folder
Make Sure you have icon in your drawable names ic_new_game,ic_help
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/new_game"
android:icon="#drawable/ic_new_game"
android:title="#string/new_game"
android:showAsAction="ifRoom"/>
<item android:id="#+id/help"
android:icon="#drawable/ic_help"
android:title="#string/help" />
</menu>
Override this method as you did
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.game_menu, menu);
return true;
}
I am creating an option menu. it has three option HOME, EMAIL, VISIT. All three options coming at the same row. i have to show HOME option above and rest two below to Home Option(according to attached Screen Shot).
Any Ideas??
Thanks.
Here is my XML file.
<menu android:id="#+id/new_game"
android:icon="#drawable/red_no_bg"
android:title="Home" />
<item android:id="#+id/email"
android:icon="#drawable/red_no_bg"
android:title="Email" />
<item android:id="#+id/help"
android:icon="#drawable/red_no_bg"
android:title="Visit Microsite" />
</menu>
Here is my code.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.layout.overview_menu, menu);
return true;
}
Using the above code i am getting the all option in Same line.
You can Create in two ways
1.Statically.
2.Dynamically.
Am explaining the easiest method of adding menus.
Step 1: Create a new Android Application.
Step 2: After onCreate() method write onCreateOptionMenu();
Step 3: In onCreateOptionMenu() Write these lines
menu.add(11, 21, 1, "Gowtham").setIcon(R.drawable.ic_launcher);
Here "Gowtham" is title of your menu item You can Home&Email use instead of "Gowtham".
Ans use Any images Instead of ic.launcher