I am currently having an issue showing the option menu items on the actionbar. For some reason, the items only show on the overflow menu and this is true for post ICS devices as well. I am using the v7-appcompat library to support sdks from version 8 onwards.
In the main activity I have:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.wild_news, menu);
return true;
}
My menu items are defined as follow:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
**xmlns:app="http://schemas.android.com/apk/res-auto"** >
<item
android:id="#+id/menu_refresh"
android:icon="#drawable/ic_action_refresh"
**app:showAsAction="always"**
android:title="#string/menuTitle_refresh"/>
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
**app:showAsAction="ifRoom"**
android:title="#string/action_settings"/>
I also have a spinner in the actionbar with the following xml layout:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="50dp"
android:minWidth="140dp"
android:layout_gravity="center_vertical"
style="#style/ActionBarNavListTitle" />
I can't seem to get my head around it.
Great I found the issue to be related to a mismatch in the ActionBar style definition which I have resolved now:
<style name="ActionBarStyle" parent="#style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
Related
I have a action bar set up as this:
When I click on it a settings popup appears like this which when pressed takes me to my apps settings page.
My question is how would I remove the settings popup so that it takes me directly to my settings activity when I press the three dotted button? I tried playing around with code below but it yielded no result.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
Goto the menu folder of your project. res>>menu and find the xml file representing the
You have to add the android:showAsAction="always" like below:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/menu"
android:title="#string/menu_settings_item"
android:showAsAction="always"
android:icon="#android:drawable/ic_menu_a" />
</menu>
Use the showAsAction parameter.
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/menu"
android:title="#string/menu_settings_item"
android:showAsAction="always"
android:icon="#android:drawable/ic_menu_a" />
</menu>
You can specify that your settings option always appears as a button on the action bar. You don't need the three dots in this case.
For instance, you can specify your action bar menu item like this (notice the "always" value):
<item android:id="#+id/action_settings"
android:title="#string/action_settings"
android:icon="#drawable/ic_action_settings"
android:showAsAction="always" />
My ActionBar has 2 action buttons, when I'm trying to change logo on my ActionBar, it doesn't show. I found the solution: in my Mainactivity class I changed ActionBarActivity(it shows by default) to Activity. Then in manifest.xml I change the Theme from
name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
to
name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar">
the logo appeared, but 2 action buttons moved to overflow menu. The question is: How to move this action buttons back?
My menu_main.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity">
<item android:id="#+id/action_share"
android:title="#string/action_share"
android:icon="#drawable/ic_action_share"
android:orderInCategory="110"
app:showAsAction="always" />
<item android:id="#+id/deleteNote"
android:title="#string/delete"
android:orderInCategory="111"
android:icon="#drawable/ic_action_delete"
app:showAsAction="always" />
</menu>
MainActivity
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
if (isAddingNote)
{
menu.removeItem(R.id.deleteNote);
menu.removeItem(R.id.action_share);
}
return true;
On this forum I found two close posts but it didn't help me.
Action buttons doesn't show up on Action Bar?
and Actionbar not shown with AppCompat
As per my understanding to your question you were originally trying to change the icon of the actionBar in which you didn't succeed and end up with other error.
Let me tell you that your menu_main.xml code works perfectly there is no error in that.
There are two possible solutions for you and that depends on your requirements.
If you are developing your app with material design support i.e using Appcompat then use following answer.
Change android:Theme.Holo.Light.DarkActionBar to Theme.AppCompat.Light.DarkActionBar again.
Change extends Activity to extends ActionBarActivity again.
Write this code in your java file to change the logo of the actionbar.
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_SHOW_TITLE);
actionBar.setIcon(R.drawable.YourLogo);`
If you want to implement your app with Holo theme then use following answer.
First of all remove Appcompat lib from your project depending on the IDE you are using.
Let your activity be extends Activity.
Let your theme be android:Theme.Holo.Light.DarkActionBar.
Change TargetSDK to below 21. Change compile version to below 21.
Replace you main_menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/action_share"
android:title="share"
android:orderInCategory="110"
android:showAsAction="always"
android:icon="#drawable/ic_action_share" />
<item android:id="#+id/deleteNote"
android:title="delete"
android:orderInCategory="111"
android:showAsAction="always"
android:icon="#drawable/ic_action_delete" />
</menu>
P.S. I do not understand why you removing menu items from your code.
I.e.
if (isAddingNote)
{
menu.removeItem(R.id.deleteNote);
menu.removeItem(R.id.action_share);
}
I'm following the android developers guide. I have this menu I'm inflating in the action bar:
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="always" />
<!-- Settings, should always be in overflow -->
<item android:id="#+id/action_settings"
android:title="#string/action_settings"
android:showAsAction="never"/>
</menu>
I have a corresponding icon in res/drawable, and am inflating like so:
#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 true;
}
The problem I'm facing is that the search action gets pushed into the overflow, and I can't figure out how to make it an action button that always shows up in the action bar.
I did not find a duplicate question on this, though there are many related questions.
Please..please help!
I'm new with android development. After i create an app with simple menu. i click on the parent menu, i don't see the items in the submenu appeared. I don't know why is that. Enyone know this, please teach me.
i use nexus Emulator to show result. (nesux S (4.0", 480 X 800:ddpi)
create menu in the Menu folder -> menu-demo like:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/addnew" android:title="add new">
<menu>
<item android:id="#+id/name" android:title="add name"></item>
<item android:id="#+id/age" android:title="Add age"></item>
</menu>
</item>
<item android:id="#+id/update" android:title="update"></item>
<item android:id="#+id/delete" android:title="delete"></item>
</menu>
i update Mainactivity.java in the src.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_demo, menu);
return true;
}
You're missing android:showAsAction="[property".
You can use a few parameters for showAsAction, such as always, never, and ifRoom.
Add these properties to your code, so for example, a menu item that I always want to appear on the actionbar would be like
<item
android:id="#+id/test"
android:showAsAction="always"
android:title="test" >
</item
For submenus, you basically want the topmost selection shown as an action always and the submenu shown never.
I'm using actionbarsherlock library, activity has flag android:uiOptions="splitActionBarWhenNarrow", so ActionBar's items are in the bottom of screen
the items are described in the xml file
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/menu_0"
android:title="#string/action_0"
android:showAsAction="always"/>
<item android:id="#+id/menu_1"
android:title="#string/action_1"
android:showAsAction="always"/>
<item android:id="#+id/menu_2"
android:title="#string/action_2"
android:showAsAction="always"/>
</menu>
I'm inflating menu
public boolean onCreateOptionsMenu(Menu menu) {
menu.clear();
MenuInflater supportMenuInflater = getSupportMenuInflater();
supportMenuInflater.inflate(R.menu.inbox_conversation, menu);
return super.onCreateOptionsMenu(menu);
}
after, on devices with android 2.3, menu items do not have the same weight
first two options take about 80% percents of action bar & the latest takes only 20%, so they are not aligned properly & don't take equal space.
Don't know what to do. Any suggestions?
Many thanks!
might want to try adding layout-weight in item tag in xml
<item
android:id="#+id/menu_0"
android:title="#string/action_0"
android:showAsAction="always"
android:layout_width="wrap_content"
android:layout_weight="1" >
</item>