SOLUTION: It was a problem with the image search icon. Android did not find the image for the search action , I had not added them in all the
res-folders , it started to show after that...
I am trying to add the action bar to a app and I am following the basic app tutorial shown in the Google Developer Website
I have written the following code.
MainActivity
public class MainActivity extends ActionBarActivity {
public void openSearch(){
System.out.println("TEST SEARCH");
}
public void openSettings(){
System.out.println("TEST SETTINGS");
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#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.main_activity_actions, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
case R.id.action_search:
openSearch();
return true;
case R.id.action_settings:
openSettings();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
res/menu/main_activity_actions.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- Search, should appear as action button -->
<item android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"
android:showAsAction="ifRoom" />
<!-- Settings, should always be in the overflow -->
<item android:id="#+id/action_settings"
android:title="#string/action_settings"
android:showAsAction="never" />
</menu>
AndroidManifest.xml
<uses-sdk android:minSdkVersion="7" android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.sample.actionbarsample.MainActivity"
android:label="#string/app_name"
android:theme="#style/Theme.AppCompat"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
But instead of a Action bar like this
I get A options menu. Which shows up when I press the menu button while running the app.
What is that I am missing out from the tutorial, Or is there something wrong in the code?
EDIT
After doing what beworker asked me I got this result, still missing the search box.
Nothing wrong either with you code or with the tutorial. It's all about configuration of your emulator. You need to configure your emulator in the way, that it has software buttons. Switch to "Device Definition" tab and edit device that is uses "Software" buttons as in the picture below.
Alternatively you can simply uncheck "Keyboard / Hardware keyboard present" property in ADV configuration (see picture below) and restart your emulator.
Android 4.0 emulates old-style menu, if a hardware menu button is present. That is why all menu actions will go in there. If you disable hardware menu in emulator, then actions will go to action bar, as expected.
Please note, that on phones with hardware menu buttons like Samsung G2, G3 etc. menu popup will still appear and you cannot change this in your code. Android will decide what is better for user experience on each and every device.
Update
Another note. If you use compatibility library, make sure you use xmlns:yourapp="http://schemas.android.com/apk/res-auto" and yourapp:showAsAction="always" as described below.
However, if your app is using the Support Library for compatibility on
versions as low as Android 2.1, the showAsAction attribute is not
available from the android: namespace. Instead this attribute is
provided by the Support Library and you must define your own XML
namespace and use that namespace as the attribute prefix.
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto" >
<!-- Search, should appear as action button -->
<item android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"
yourapp:showAsAction="always" />
...
</menu>
What is that I am missing out from the tutorial, Or is there something wrong in the code?
Yes, there is a difference.
Try to remove the sample word from your project.
Actually your project name is occupying all the space and in search menu item you have asked the system to provide the space if there is any room/space available which is not due to the long project name(may be activity or whatever) in your case.
So either change your project name to something small or use "always" like this:
<item android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"
android:showAsAction="always" />
Related
The app runs fine. On devices with hard options menu button the menu shows up, so i know it works. On some devices it shows the overflow button at the top-right.
I my test case the device is Asus Zenphone 5 there is no hard button, i dont get a overflow button either. But when i run the showOptionsMenu() command from a button click it displays the options menu and all related events work no issues.
Menu - Xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/select" android:title="Select" android:showAsAction="never"></item>
<item android:id="#+id/add_kid" android:title="Add" android:showAsAction="never"></item>
</menu>
onCreate & onPrepare
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.menu1, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onPrepareOptionsMenu(Menu menu)
{
if(!getCheckInStatus())
{
menu.setGroupVisible(R.id.group1,false);
}
else
{
menu.setGroupVisible(R.id.group1,true);
}
return super.onPrepareOptionsMenu(menu);
}
Manifest values for the Activity
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
.....
<activity
android:name="com.my.package.MyActivity"
android:configChanges="orientation|keyboard"
android:label="#string/app_name"
android:launchMode="singleInstance"
>
</activity>
I would really appreciate any help on this matter.
I used to have same problem in few of my projects. I followed the steps given below which worked for me
1. extends your activity to ActionBarActivity
call the following code in onCreate method
private void makeActionOverflowMenuShown()
{
//devices with hardware menu button (e.g. Samsung Note) don't show action overflow menu
try {
ViewConfiguration config = ViewConfiguration.get(this);
Field menuKeyField = ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey");
if (menuKeyField != null) {
menuKeyField.setAccessible(true);
menuKeyField.setBoolean(config, false);
}
} catch (Exception e) {
}
}
set android:showAsAction="ifRoom" in menu.xml, the button would show.
Try like this. This works for me..
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:icon="#drawable/ic_action_overflow"
android:id="#+id/moreItmes"
android:showAsAction="never"
android:title="#string/more">
<menu>
<item
android:id="#+id/select"
android:showAsAction="never"
android:title="#string/Select"/>
<item
android:id="#+id/add_kid"
android:showAsAction="never"
android:title="#string/Add"/>
</menu>
</item>
</menu>
As i commented in vinay's answer , the reason was that i was debugging an old program written by some one else. It seems all were Activities , but i had changed the target and compile-with to 22. Once i changed the Activity to ActionBarActivity the overflow appeared on the devices with soft menu button. While those with hard options menu button did not display it.
Menu - Xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/select" android:title="Select" android:showAsAction="never"></item>
<item android:id="#+id/add_kid" android:title="Add" android:showAsAction="never"></item>
</menu>
in your menu.xml you just use showAsAction as never which represent Never place this item in the Action Bar. please refer the link for more info
I'm trying to understand what is wrong with what I'm doing.
I'm currently following the android action bar tutorial and for some reason it's not showing me the search icon in the action bar on my device, it is going straight into my overflow.
I'm running 4.4.4 version on my device, Nexus 5. I've tried to follow several tutorials and the result was no different. I've set up new project by default using API 11 as told, I've tried to set the showAsAction to "Always".
What else could I do? Thank you for any help!
My XML menu file:
<item android:id="#+id/action_search"
android:icon="#drawable/ic_action_new"
android:title="new"
android:showAsAction="ifRoom" />
<item android:id="#+id/action_settings"
android:title="Settings"
android:showAsAction="never" />
Main activity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
Try using a namespace
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:{anynamespace}="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/action_search"
android:icon="#drawable/ic_action_new"
android:title="new"
{anynamespace}:showAsAction="always" />
</menu>
Replace the {anynamespace} parts with any word
EDIT: The tutorial suggests that you use namespaces in case you are using the Support library.
I can't understand why wrong and incompatible (AndroidStudio tells me "Should use app:showAsAction with the appcompat library) code
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/action_search"
android:title="#string/action_search"
android:icon="#drawable/search"
android:showAsAction="always" />
</menu>
works perfect, but proper and compatible version like
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" >
<item android:id="#+id/action_search"
android:title="#string/action_search"
android:icon="#drawable/search"
app:showAsAction="always" />
</menu>
not showing my icon at all.
I'm testing on Samsung GT P5210 (android v. 4.4.2) and Nexus 7 (4.4.4)
Things you should always check when you want to use action bar are
1) Extend ActionBarActivity instead of Activity
public class MainMenu extends ActionBarActivity{
2) Have the right style selected as defined at manifest
Manifest
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
Style
<style name="AppTheme"
parent="Theme.AppCompat.Light.DarkActionBar">
</style>
3) Select the right title for showAsAction
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:**yourapp**="http://schemas.android.com/apk/res-auto" >
<item android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"
**yourapp**:showAsAction="ifRoom" />
...
</menu>
This is what most people get wrong
4) Define your Menu in Activity
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_activity_actions, menu);
return super.onCreateOptionsMenu(menu);
}
If you do all the following your action bar should work.
Then you should add the onClickListener for every position...
I just reread your question and saw your problem is the complete opposite (but some pieces of my old answer still apply to your problem), so here is the updated answer:
Update:
You have imported the appcompat library in your gradle file, but you seem to support only devices newer than API Level 11 or 14? If this is the case, the lint check sees that you have imported the appcompat library via gradle and it thinks that you should use the ActionBarActivity because of your library import. That's why you are getting the error. But as your android:showAsAction attribute is working, you are using the native Activity and the native attribute call is correct, even if the lint check says it is wrong. So if you want to remove the lint error, you have to delete the appcompat lib from your gradle file and maybe change your activity theme to the native Holo Light theme, as your current theme might rely on the appcompat theme.
The answer why it isn't working with the app namespace is in the XML attribute loading for native respectively library code, which is handled in the old answer.
Old Answer
If you are using the ActionBarActivity from the support library to reach devices lower than API level 11, the main issue here is, that the ActionBarActivity recreates some of the native Android XML attributes such as android:showAsActionin its own scope, which you define with:
xmlns:app="http://schemas.android.com/apk/res-auto"
and then access them with the same attribute (here showAsAction) in the app: namespace.
So the ActionBarActivity can't see or reach the native android:showAsAction attribute as it is only looking for it in the app namespace and not the android namespace.
If you want to use the native attribute, you have to use the native Activity with a Holo Theme, which is only supported from API Level 11 and higher.
addthis:
yourapp:showAsAction="ifRoom"
or
android:showAsAction
for example:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto" >
<item android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"
yourapp:showAsAction="ifRoom" />
</menu>
and in Activity:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_activity_actions, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
case R.id.action_search:
openSearch();
return true;
case R.id.action_compose:
composeMessage();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
And read more here
Hello and thank you for the time you take in reading this question.
I am trying to develop an android app which will use the ActionBar compat library. I have followed (as far as I see it) all the recommendations when using the compat library. My Manifest looks like this(only relevant code shown):
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/Theme.AppCompat.Light" >
</application>
</manifest>
As you can see I am targeting sdk 8+. I have used the Theme.AppCompat theme as recommended.
My menu file looks like this:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:cds="http://schemas.android.com/apk/res-auto" >
<item
android:id="#+id/action_map"
android:icon="#drawable/ic_action_map"
android:title="#string/action_map"
cds:showAsAction="ifRoom"/>
<item
android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"
cds:showAsAction="ifRoom"/>
<item
android:id="#+id/action_mail"
android:icon="#drawable/ic_action_mail"
android:title="#string/action_mail"
cds:showAsAction="ifRoom"/>
</menu>
I am using my own namespace for the showAsAction attribute.
My activity extends the ActionBarActivity class.
The problem is this: On sdk 10 (android 2.3.3), both on device and emulator, the overflow menu (the three dots on the right side of the action bar) are not shown. Only the first 2 of the menu items are shown on the action bar. If i press the "Menu" button on the device then the third item is shown from the bottom left corner of the screen (not from the upper right corner as on the devices with more recent android versions). The same code works well on android sdk 17 on emulator (the overflow menu is shown with the proper actions).
I have searched the web for a solution but I could not find one with this specific problem. I would have abandoned the issue if I wouldn't have installed apps on the android 2.3.3 device that have the same action bar and which show the overflow menu icon and work properly like on any recent android device. One example of this app is the todoist app (https://en.todoist.com/android) or the handcent app(https://play.google.com/store/apps/details?id=com.handcent.nextsms&hl=en) which both behave well on this device.
Is there anything I am missing or is there an alternative solution to the recommended way of using the actionbar compat?
Thank you for your time.
#Andrei Google have disabled the menu overflow button in appcompat on pre honycomb.
If You really want to add it go to the android's github repository and download
platform_frameworks_support. It contains sorce for appcompat in platform_framework_support_master/v7/appcompat.
Create a libs folder inside appcompat and put latest android-support-v4.jar.
Now open file v7/appcompat/src/android/support/v7/internal/view/ActionBarPolicy.java.
You will see that showOverflowMenuButton is returned false for pre honycomb.Just return it true and add this edited appcompat as library to your project
and you will not need any custom overflow button
This worked with me.
Sorry for my English
EDIT: actual code from android/support/v7/internal/view/ActionBarPolicy.java
public boolean showsOverflowMenuButton() {
// Only show overflow on HC+ devices
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB;
}
Try to show as I did.
I add overflow menu (three dots) manually:
<item
android:id="#+id/menu_more"
android:icon="#drawable/abc_ic_menu_moreoverflow_normal_holo_light"
android:title="#string/action_settings"
myapp:showAsAction="always">
<menu>
<item
android:id="#+id/submenu_about"
android:showAsAction="always"
android:title="#string/menu_about"/>
</menu>
</item>
and override menu button click in activity to show this menu (solution from Opening submenu in action bar on Hardware menu button click):
private Menu mainMenu;
...
#Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_UP) {
switch (keyCode) {
case KeyEvent.KEYCODE_MENU:
mainMenu.performIdentifierAction(R.id.menu_more, 0);
return true;
}
}
return super.onKeyUp(keyCode, event);
}
Result on 2.2 looks like:
Hope it helps you.
I found the answer finally for this situation.
All you need to do is call the following in the OnCreate in your activity
ActionBarPolicy.get(this).showsOverflowMenuButton();
My original application was written for Android 2.1. Afterwards, I've added compatibilty library and ActionBar Sherlock.
Now, I would like to present options menu as overflow in the action bar and it works as expected. However, on devices without menu button, I still get default menu bar at the bottom of the screen. Clicking on it opens the options menu from the action bar. See image below:
What is worse, this bar shows even on activities that have no options menu defined.
Regarding my relevant code, there is nothing special about it.
Inflating options menu:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_menu, menu);
return true;
}
main_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:icon="#drawable/ic_menu_flag" android:title="#string/I_STR_LANGUAGE" android:id="#+id/menu_lang" android:showAsAction="never"></item>
<item android:icon="#drawable/ic_menu_pin_change" android:title="#string/change_pin" android:id="#+id/menu_pin_change" android:showAsAction="never"></item>
<item android:icon="#drawable/ic_menu_about" android:id="#+id/menu_about" android:title="#string/about_application" android:showAsAction="never"></item>
<item android:icon="#drawable/ic_menu_exit" android:id="#+id/menu_logout" android:title="#string/I_CLOSE" android:showAsAction="never"></item>
</menu>
Application theme inherits from DarkActionBar Sherlock Theme
<style name="AppTheme" parent="Theme.Sherlock.Light.DarkActionBar">
Is there a way to hide system menu bar? Can options menu be presented only from action bar? Can it at least be hidden for activites without options menu?
[UPDATE:] <uses-sdk android:minSdkVersion="7" />
You need to modify your uses-sdk node to target the latest API, like so :
<uses-sdk android:minSdkVersion="7" android:targetSdkVersion="18"/>