I have my min SDK version set to 11 and my target SDK version set to 11. I have an activity with some fragments in it. The style has parent="#android:style/Theme.Light" and doesn't hide the actionbar. In the activity where I expect to see the actionbar, I have an override of onCreateOptionsMenu(). My understanding is that onCreateOptionsMenu() will automatically be called when the activity starts and that will create the actionbar. This is not happening for me. I'm not sure if my understanding is wrong or if I'm doing something wrong that is causing it not to show up.
To be clear, my onCreateOptionsMenu method is not even firing. I suspect the problem is in another part of the code, but I don't know what it could be.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.signin_layout_menu, menu);
return true;
}
Here is my menu xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/menu_signin_layout"
android:title="stuff"
android:showAsAction="ifRoom|withText" />
</menu>
Here is the style that activity is using:
<style name="Theme.MyTheme" parent="#android:style/Theme.Light">
<!-- Window attributes -->
<item name="android:windowTitleSize">36dip</item>
<item name="android:windowTitleStyle">#style/WindowTitle</item>
<item name="android:windowTitleBackgroundStyle">#style/WindowTitleBackground</item>
<item name="android:textAppearanceButton">#style/TextAppearance.Widget.Button</item>
<!-- Button styles -->
<item name="android:buttonStyle">#style/Widget.Button</item>
</style>
I'm pretty sure your theme needs to be parent="#android:style/Theme.Holo.Light" for the actionbar to show.
Are you inflating the menu and returning "true", to indicate that you've handle the action, correctly in your onCreateOptionsMenu() override? http://developer.android.com/guide/topics/ui/menus.html#Inflating
If you're returning false, the event will bubble up to the parent, which by default does nothing.
Related
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 can increase the ActionBar's height to its double size (by Default is action_bar_default_height = 48dp standard ActionBarSize), so, 96dp but the Icons of my ActionBarare just as little as with standard height.
In order to acquire a better understanding, I illustrate with a Picture.
Can anyone tell me, why I get left Icons properly but the associated Icons to Actions not?
How could I solve it?
That's my code
First, I define the proper ActionBarSize Height and set the proper Size through Themes-Styles of my Activity in my styles.xml like this
<style name="Theme.ActionBarSize" parent="android:Theme.Holo.Light">
<item name="android:actionBarSize">96dp</item>
</style>
Second, I declare that Theme in the proper Activity in my Manifest File
<activity android:name="com.nutiteq.advancedmap.activity.WmsMapActivity"
android:theme="#style/Theme.ActionBarSize" >
Third, I create an ActionBar Instance and I initialize it properly in onCreate Method of my Activity
...
ActionBar actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeButtonEnabled(true);
actionBar.show();
...
Fourth and last one, I inflate the associated Action-Items of ActionBar in onCreateOptions(...) Method like this
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.wms_context_menu, menu);
return true;
}
My ActionBar Items are defined in the file wms_menu.xml which looks like as follows
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/videonotes"
android:showAsAction="ifRoom"
android:title="#string/vid_description"
android:icon="#drawable/ic_action_video_96" >
</item>
<item android:id="#+id/textnotes"
android:showAsAction="ifRoom"
android:title="#string/txt_description"
android:icon="#drawable/ic_action_edit_96" >
</item>
<item android:id="#+id/picnotes"
android:showAsAction="ifRoom"
android:title="#string/pic_description"
android:icon="#drawable/ic_action_camera_96" >
</item>
<item android:id="#+id/audionotes"
android:title="#string/aud_description"
android:showAsAction="ifRoom"
android:icon="#drawable/ic_action_pic_96">
</item>
<item android:id="#+id/right_drawer"
android:title="#string/right_slide_description"
android:showAsAction="ifRoom"
android:icon="#drawable/ic_drawer_96" >
</item>
</menu>
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
I have a menu item that is showing up on android 4.x but not on 2.x. Here is my menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" >
<item
android:id="#+id/menu_filter"
android:title="Filter"
app:showAsAction="always"/>
</menu>
This is my actionbar style
<style name="style1_actionBar" parent="#style/Widget.AppCompat.Light.ActionBar">
<item name="android:background">#color/blue_dark</item>
<item name="android:textColor">#color/white</item>
<item name="actionMenuTextAppearance">#color/white</item>
<item name="background">#color/blue_dark</item>
</style>
Any ideas?
Edit: removed double quote typo
Could it be the fact that I am showing only text, no icons? I'm kind of stuck here.
Whew, thanks for your help guys but I managed to figure it out. It wasn't a problem with the xml, it was a problem with the onCreateOptionsMenu function.
I was using this
new MenuInflater(getApplication()).inflate(R.menu.activity_wentry_editor, menu);
instead of this
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.activity_wentry_editor, menu);
Not entirely sure why this works but it does.
<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>
Please refer to the documentation. http://developer.android.com/guide/topics/ui/actionbar.html
Using XML attributes from the support library
Notice that 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.
In my case I had to add a few lines to onCreateOptionsMenu.
Android Studio didn't let me use android:showAsAction="ifRoom" while using appCompat.
app:showAsAction="ifRoom" wasn't working and I removed it without problems.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
for (int i = 0; i < menu.size(); i++) {
menu.getItem(i).setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
}
return super.onCreateOptionsMenu(menu);
}
If you want your app to support action bar below 3.0 you need to use app compact v7 from the support library.
Also check the link
Using the menu in an activity that extends the AppCompact it is necessary import the app context in the XML and use it:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<!-- "Mark Favorite", should appear as action button if possible -->
<item
android:id="#+id/action_favorite"
android:icon="#drawable/ic_favorite_black_48dp"
android:title="#string/action_favorite"
app:showAsAction="ifRoom"/>
<!-- Settings, should always be in the overflow -->
<item android:id="#+id/action_settings"
android:title="#string/action_settings"
app:showAsAction="never"/>
</menu>
What you need to do basically is add xmlns:app="http://schemas.android.com/apk/res-auto"to the menu element in yout XML and use the showAsAction in the following format: app:showAsAction="ifRoom".
This will show the icon in the action bar, if possible.
I have an ActionBar that should display the action buttons in a custom way. For this I created a custom view and attached it to the ActionBar.
One thing to mention is that I am using a menu.xml resoure file to load the options menu and display them on a smartphone, but do not display them on tablet, instead use a custom view. For this I market every menu item in the xml as: android:showAsAction="never"
Everything looks fine, except one little thing that still remains on the right of the ActionBar - the "More" button.
How can I remove it?
I tried this:
ActionBar bar = activity.getActionBar();
bar.removeAllTabs();
but the "more" button still remains there.
EDIT:
This is my menu.xml file:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/menu_username"
android:icon="#drawable/menu_username"
android:orderInCategory="0"
android:showAsAction="never"
android:title="#string/menu_username">
<menu>
<item
android:id="#+id/menu_logout"
android:title="#string/menu_logout"/>
</menu>
</item>
<item
android:id="#+id/menu_settings"
android:icon="#drawable/menu_settings"
android:orderInCategory="1"
android:showAsAction="never"
android:title="#string/menu_settings"/>
<item
android:id="#+id/menu_search"
android:icon="#drawable/menu_search"
android:orderInCategory="1"
android:showAsAction="never"
android:title="#string/menu_search"/>
</menu>
Please note I still want to inflate this menu on a smartphone, but don't want to use it on a tablet.
Setting showAsAction="never" will force an menu item into the overflow. Why not check in onCreateOptionsMenu(...) that the device is a tablet, and if it is just not inflate the menu? Something like this:
public boolean onCreateOptionsMenu(Menu menu) {
if (getResources().getConfiguration().smallestScreenWidthDp >= 600) {
//It's a tablet, don't inflate, only create the manual view
manualMenuCreation();
} else {
getMenuInflater().inflate(R.menu.menu, menu);
}
return true;
}
Don't forget smallestScreenWidthDp is only available in 3.2 or above, so you'll have to take that into consideration.