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.
Related
I'm trying to show custom layout as an item in options menu.
And this is what I have done so far
<item
android:id="#+id/action_profile"
android:orderInCategory="100"
android:title="Profile"
android:actionLayout="#layout/layout_menu_profile"
app:showAsAction="never" />
I tried
app:actionLayout="#layout/layout_menu_profile"
as well as per this SO link but still it shows only title - Profile
I have created new project through Android Studio 1.5 with Blank Activity.
with minSDK = 15 and targetSDK = 23
Following is the code in Blank Activity.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_home, menu);
MenuItem item = menu.findItem(R.id.action_profile);
return true;
}
where am I going wrong ?
use app:actionLayout instead of android:actionLayout.
<item
...
app:actionLayout="#layout/layout_menu_profile"
app:showAsAction="always" />
Try this code
<item
android:id="#+id/action_profile"
android:orderInCategory="100"
android:title="Profile"
android:actionLayout="#layout/layout_menu_profile"
app:showAsAction="always" />
Just set the showAsAction elements to always
Use "app:" instead of "android:"
app:actionLayout="#layout/layout_menu_profile"
I am currently closely following the steps on the website
https://developer.android.com/training/basics/actionbar/adding-buttons.html and at this stage of the tutorial it wants me to copy and paste the code to add an action search and action settings. However, when I run my application, the action search doesn't want to appear.
I have also made sure to include a .png for the icon of the action search, but still won't show.
I have also tried changing the minimumsdk version in my build.gradle from 8 to 11 as suggested by the website, but didn't work either. However, if I am not mistaken, the action bar is present in the app though since the overflow is there.
From my wild guess, it might be that the code is outdated since I have noticed a lot of things have changed since this tutorial was written. But I am still clueless about this weird problem.
you have to create an entry inside res/menu,override onCreateOptionsMenu and inflate it
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.yourentry, menu);
return true;
}
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_cart"
android:icon="#drawable/cart"
android:orderInCategory="100"
android:showAsAction="always"/>
</menu>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/action_settings"
android:orderInCategory="1"
app:showAsAction="always"
android:icon="#drawable/ic_action_settings"
android:title="#string/action_settings"/>
<item
android:id="#+id/volume"
android:orderInCategory="2"
android:title="Volume"
android:icon="#drawable/ic_action_volume_on"
app:showAsAction="always"/>
You need to xmlns referencing res-auto and then use it as I have used in my code. Hope this helps.
This question already has answers here:
Actionbar not shown with AppCompat
(3 answers)
Closed 9 years ago.
I am following the tutorial on developer.android.com and trying to add items on action bar.
Although i added all the code the search action shows as an overflow element instead of an action button element. I tried on 4" and 7" virtual devices with soft keyboard option.
Here is the
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>
Here is the MainActivity.java 's onCreateOptionsMenu method.
#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_activity_actions, menu);
return super.onCreateOptionsMenu(menu);
}
I want to learn what causes this problem.
This is because if you use the support AppCompat ActionBar library and ActionBarActivity you should create your menus in a different than the standard way of creating xml menus in ActioBarSherlock or the default ActionBar.
So try this code :
<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:icon="#drawable/ic_action_search"
android:title="#string/action_search"
app:showAsAction="always" />
<item android:id="#+id/action_compose"
android:icon="#drawable/ic_action_compose"
android:title="#string/action_compose"
app:showAsAction="always"/>
</menu>
Though the issue is resolved, let me post an answer with more information, may be found helpful by anyone later.
Now, the issue is you have used android:showAsAction="ifRoom" and android:showAsAction="never", instead if you would want to make action button always visible then use android:showAsAction="always"
FYI, android:showAsAction can take either of any values:
android:showAsAction=["ifRoom" | "never" | "withText" | "always" |
"collapseActionView"]
You can read more about Menu Resource
I cannot get icons to show in the action bar.
I am using the most recent SDK with eclipse.
I am trying to do this tutorial
http://developer.android.com/training/basics/actionbar/adding-buttons.html
My MainActivity.java is
package com.example.myfirstapp;
import android.os.Bundle;
import android.app.Activity;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuInflater;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
}
My main.xml under menu is
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"
android:showAsAction="always" />
<item android:id="#+id/action_settings"
android:title="#string/action_settings"
android:showAsAction="always" >
</item>
</menu>
But I dont see the menu items. It looks like this
Any help on how to get these menu items shows would be useful.
Thank you.
Quoting from the docs:
The showAsAction attribute above uses a custom namespace
defined in the tag. This is necessary when using any XML
attributes defined by the support library, because these attributes do
not exist in the Android framework on older devices. So you must use
your own namespace as a prefix for all attributes defined by the
support library.
So do the following modifications:
<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="always" />
<item android:id="#+id/action_settings"
android:title="#string/action_settings"
yourapp:showAsAction="always" >
</item>
</menu>
I am pretty sure you must have already done this, but did you download the icons and put them inside your res > drawable folders? If you are using a dark action bar, be sure to install the light colored icons and vice versa.
I followed the same tutorial and it works for me. Your code looks correct.
Just put
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
and change
app:showAsAction="always"
to
android:showAsAction="always"
i think your problem is here:
return super.onCreateOptionsMenu(menu);
change it to this:
return Boolean.TRUE;
Source: Menus in android and an old project of mine.
I'm following the tutorial too, I think the mistake is here:
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
It should be
getMenuInflater().inflate(R.menu.main_activity_actions, menu);
It also took me a while for this issue after I followed the tutorial step by step. Funny how, my problem is that I used light color search button image while chose the dark Holo theme. Therefore, my search button simply becomes invisible (it is there) in the dark background. You may try to adjust your theme if you encouter same issue.
I have an action bar in my app with 3 items.
Only 2 can be displayed due to space issues, so I'd expect the first to be displayed and the rest to be displayed in the overflow.
However in practice only the first 2 items are shown and there is no overflow detectable.
Here is the relevant code:
list_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/menu_insert"
android:icon="#android:drawable/ic_menu_add"
android:title="#string/menu_insert"
android:showAsAction="ifRoom|withText"/>
<item android:id="#+id/menu_call"
android:icon="#android:drawable/ic_menu_call"
android:title="#string/menu_call"
android:showAsAction="ifRoom|withText"/>
<item android:id="#+id/menu_agenda"
android:icon="#android:drawable/ic_menu_agenda"
android:title="#string/menu_agenda"
android:showAsAction="ifRoom|withText"/>
</menu>
Activity.java
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater mi = getMenuInflater();
mi.inflate(R.menu.list_menu, menu);
return true;
}
If you want to show the three dots, irrespective of device menu button! then you can call this method in your application class' 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) {
Log.d(TAG, e.getLocalizedMessage());
}
}
Output
res/menu/menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- Search / will display always -->
<item
android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:showAsAction="always"
android:title="#string/action_search"/>
<!-- Location Found -->
<item
android:id="#+id/action_location_found"
android:icon="#drawable/ic_action_location_found"
android:showAsAction="always"
android:title="#string/action_location_found"/>
<!-- More -->
<item
android:id="#+id/a_More"
android:icon="#drawable/ic_action_overflow"
android:showAsAction="always"
android:title="More">
<menu>
<!-- Refresh -->
<item
android:id="#+id/action_refresh"
android:icon="#drawable/ic_action_refresh"
android:showAsAction="never"
android:title="#string/action_refresh"/>
<!-- Help -->
<item
android:id="#+id/action_help"
android:icon="#drawable/ic_action_help"
android:showAsAction="never"
android:title="#string/action_help"/>
<!-- Check updates -->
<item
android:id="#+id/action_check_updates"
android:icon="#drawable/ic_action_refresh"
android:showAsAction="never"
android:title="#string/action_check_updates"/>
</menu>
</item>
</menu>
MainActivity.java
public class MainActivity extends Activity {
#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.menu, menu);
return super.onCreateOptionsMenu(menu);
}
}
Download the Action Bar Icon Set
I realize this is not an overflow menu, but it is similar.
Okay, this is simple but was hard to figure out.
You first need a menu item you want to use as the overflow inflater. Example
<item
android:id="#+id/a_More"
android:icon="#drawable/more"
android:showAsAction="always"
android:title="More">
</item>
Once you have your item, add a sub-menu containing your items you want in the overflow menu. Example:
<item
android:id="#+id/a_More"
android:icon="#drawable/more"
android:showAsAction="always"
android:title="More">
<menu>
<item
android:id="#+id/aM_Home"
android:icon="#drawable/home"
android:title="Home"/>
</menu>
</item>
On click this will inflate other items within. My application is using ActionBarSherlock 4.0 so before this will work for you, you will need to access the "SplitActionBar". (Will still work on default android Actionbar)
Here's how:
In your AndroidManifest.xml file, you need to add this code under the activity you need the overflow menu in.
android:uiOptions="splitActionBarWhenNarrow"
NOTE: Your item that inflates your overflow menu MUST showAsAction="always"
Vwola! you have an overflow menu! Hope I helped you out. :)
On devices with hardware menu buttons (Galaxy S3, stubborn as Samsung is...) the overflow menu behaves as the 'traditional' menu, by using the hardware menu button.
When you say "overflow" menu, do you mean the three dots that show up in the end to indicate that there are more items.... or do you mean the split actionbar that shows up in the bottom for overflow items?
If you mean the split action bar, you should add this to your activity's manifest file
android:uiOptions="splitActionBarWhenNarrow"
By default, the three dots overflow menu should happen automatically, so it's hard to tell what the problem is from the information you provided above.
To alway show action overflow (three dot) on actionbarcompat:
in menu file, example:
main.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" >
<item
android:id="#+id/action_settings"
android:title="#string/action_settings"
app:showAsAction="never"/>
<item
android:id="#+id/action_about"
android:title="#string/action_about"
app:showAsAction="never"/>
</menu>
and in activity file:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
It's worked fine for me.
Tested on Google Nexus S, Samsung S3.
It appears that on devices with a menu button the overflow menu does not show in the ActionBar. Also we have no way to know if a device has a hardware menu button before API level 14 (4.0+).
This solution adds a overflow button to a android.support.v7.app.ActionBar that opens the standard options menu on pre-Honeycomb (<3.0) only, otherwise leaving it to the system. In any case you can choose between always or ifRoom for all actions.
Get the overflow icon from the Action Bar Icon Pack.
Add a menu (I'omitting some non-relevant parts as titles, ordering etc.)
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/action_overflow"
android:orderInCategory="100"
android:icon="#drawable/ic_action_overflow"
app:showAsAction="always" />
<item
android:id="#+id/action_first"
app:showAsAction="always" />
<item
android:id="#+id/action_second"
app:showAsAction="ifRoom" />
</menu>
Remove our overflow action on 3.0+
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
menu.removeItem(R.id.action_overflow);
}
return super.onPrepareOptionsMenu(menu);
}
Finally call Activity.openOptionsMenu() from the overflow action.
Er... no. You cannot not call openOptionsMenu() from a menu button... but if you are using AndroidAnnotations you're done easily:
#OptionsItem
void action_overflow() {
openOptionsMenuDeferred();
}
#UiThread
void openOptionsMenuDeferred() {
openOptionsMenu();
}
If not you should do something like this, I suppose
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == id.action_overflow) {
openOptionsMenuDeferred();
return true;
}
}
private Handler handler = new Handler(Looper.getMainLooper());
public void openOptionsMenuDeferred() {
handler.post(new Runnable() {
#Override
public void run() {
openOptionsMenu();
}
}
);
}
Try changing the theme of the app from
Theme.AppCompat.Light.DarkActionBar to Theme.AppCompat.Light
Put xmlns:your_app_name="http://schemas.android.com/apk/res-auto" inside menu tag
and instead android:showAsAction="always" use your_app_name:showAsAction="always"
for eg
<item
android:id="#+id/action_search"
android:icon="#drawable/icon_search"
android:title="#string/search"
your_app_name:showAsAction="ifRoom"/>