Action Items not showing in ActionBar with showAsAction="ifRoom" - android

I'm trying to add some Action Items to my Support Action Bar. In my activity, I have also tabs added to the Action Bar.
This is an excerpt of the activity:
public class ShowEmails extends ActionBarActivity implements ShowEmailsFragmentInteractionListener {
private IMAPClientService service;
private boolean bound;
private ActionBar ab;
private MailDBHelper mdbhelper;
private SQLiteDatabase db;
private Intent client_service;
<.........................>
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.client_service = new Intent(this, IMAPClientService.class);
this.mdbhelper = new MailDBHelper(this.getApplicationContext(), MailDBHelper.MAIL_DB_NAME, null, MailDBHelper.MAIL_DB_VERSION);
this.db = this.mdbhelper.openWriteable();
this.ab = this.getSupportActionBar();
this.ab.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
this.ab.show();
Tab t = ab.newTab().setText(R.string.all_emails)
.setTabListener(new TabListener<ShowEmailsFragment>(this, "all", ShowEmailsFragment.class));
ab.addTab(t);
new LoadTabsInBackground().execute();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.show_emails_bulk_action, menu);
return super.onCreateOptionsMenu(menu);
}
}
The class LoadTabsInBackground adds some tabs to the ActionBar after doing some database operation.
This is the menu resource I'm inflating:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" >
<item
android:id="#+id/email_refresh"
android:orderInCategory="1"
android:showAsAction="ifRoom"
android:title="#string/action_refresh"
android:icon="#drawable/ic_menu_refresh"/>
<item
android:id="#+id/email_bulk_seen"
android:orderInCategory="20"
android:showAsAction="ifRoom"
android:title="#string/action_seen"
android:icon="#android:drawable/sym_action_email"/>
<item
android:id="#+id/email_bulk_delete"
android:orderInCategory="40"
android:showAsAction="ifRoom"
android:title="#string/action_delete"
android:icon="#android:drawable/ic_menu_delete"/>
</menu>
And here is an excerpt from AndroidManifest.xml, where you can sse the theme I'm using is Theme.AppCompat.Light:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="it.dndonline.battleclient4android"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="9"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/Theme.AppCompat.Light" >
<.......................>
<activity
android:name="it.dndonline.battleclient4android.Activities.ShowEmails"
android:label="#string/title_activity_show_folders"
android:theme="#style/Theme.AppCompat.Light" >
</activity>
<......................................>
</application>
</manifest>
Everything seems correct to me, unfortunately, although tabs are loaded correctly, this means that ActionBar is properly working, none of the menu items are loaded in the action bar. Setting the showAsAction value to always doesn't change anything.
I'm testing it on Android 2.3.3.

I found the problem. There was an error in the menu .xml file. In fact, I've added a new namespace:
xmlns:app="http://schemas.android.com/apk/res-auto"
But then, I still refer the property as if it belongs to the android namespace:
android:showAsAction="ifRoom"
The right way to refer this property is to use:
app:showAsAction="ifRoom"
Cause it belongs to the namespace app.
Here is the relevant part in documentation:
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. (A custom XML namespace should
be based on your app name, but it can be any name you want and is only
accessible within the scope of the file in which you declare it.)

I found the same error, and i resolve it like this
<item
android:id="#+id/action_refresh"
android:orderInCategory="100"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:showAsAction="ifRoom"
android:icon="#android:drawable/ic_delete"
android:title="Delete"/>
add,xmlns:app="http://schemas.android.com/apk/res-auto" in the middle of the code, works for me :)

Related

How to show action bar in a tab group activity?

I have a tab group activity containing 4 tabs. tab1, tab2, tab3, tab4 and two child activities in tab1. How to display action bar in a child activity2 of tab1. But the action bar is not displaying. I want the action bar to be displayed in one child activity.
activity of tab1
public class EntriesTabActivity extends TabGroupActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
startChildActivity("EntriesContentActivity", new Intent(this,EntriesContentActivity .class));
}
}
ChildActivity
public class EntriesContentActivity extends ActionBarActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// construct the tab host
setContentView(R.layout.entries_content);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.action_menu_entry, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
My manifest file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.iga.allergy"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" >
</uses-permission>
<!-- We must declare these two permissions -->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="com.android.vending.CHECK_LICENSE" />
<application
android:name="com.iga.allergy.AJ_Constant"
android:allowBackup="true"
android:icon="#drawable/app_icon"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.iga.allergy.MainActivity"
android:label="#string/app_name"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.iga.allergy.TabController"
android:screenOrientation="portrait" />
<activity
android:name="com.iga.allergy.EntriesTabActivity"
android:screenOrientation="portrait"
android:windowSoftInputMode="adjustResize" />
<activity
android:name="com.iga.allergy.EntriesContentActivity"
android:screenOrientation="portrait" />
<activity>
</application>
</manifest>
styles.xml
<resources>
<!--
Base application theme, dependent on API level. This theme is replaced
by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
-->
<style name="AppBaseTheme" parent="Theme.AppCompat.Light">
<!--
Theme customizations available in newer API levels can go in
res/values-vXX/styles.xml, while customizations related to
backward-compatibility can go here.
-->
</style>
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>
</resources>
I feel like you are developping with support library since you want actionbar for api8.have you included appcompat_v7 and android-support-v4 to your project properly?
ActivityGroup inherits android.app.activity and I dont think you can add multiple actionbar to childs or fragments but only to the parent activity.so to my point of view its impossible to add an actionbar to child activities.besides activityGroup is deprecated we should use Fragment and FragmentManager APIs instead.

action bar not showing with support library

I am trying to implement an activity with an action bar with the v7 support library. But the action bar just does not show. The menu items are showing as a normal menu list when i touch the menu button. What am i doing wrong?
Manifest.xml :
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".ChatSession_ActionBarActivity"
android:theme="#style/Theme.AppCompat.Light">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
chatsession_menu.xml :
<?xml version="1.0" encoding="utf-8" ?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/dummy1"
android:title="dummy1 menu option"
android:showAsAction="always"
android:icon="#drawable/ic_action_search">
</item>
</menu>
ChatSession_ActionBarActivity :
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuInflater;
public class ChatSession_ActionBarActivity extends ActionBarActivity{
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.actionbaractivity_chatsession);
getSupportActionBar().show(); //this line is not required, but just giving it a try
}
#Override
public boolean onCreateOptionsMenu(Menu menu){
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.chatsession_menu, menu);
return true;
}
}
You need to have
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto" >
<item
android:id="#+id/dummy1"
android:title="dummy1 menu option"
yourapp:showAsAction="always"
android:icon="#drawable/ic_action_search">
</item>
</menu>
From the docs
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.

Android actionbar not visible

I am trying to add an option menu in actionbar to my existing app but it is not working. If I create a new project with "hello world" default app I can see the button in action bar. The onCreateOptionMenu() method seems never caught in debug with breakpoint, what's wrong ??
I'm working with API 14 on both app and this is my MainActivity code.
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
public class Principale extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ecran_d_acceuil);
}
public boolean onCreateOptionMenu(Menu menu) {
getMenuInflater().inflate(R.menu.principale, menu);
return true;
}
}
My menu.xml code
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:showAsAction="never"
android:title="#string/action_settings"/>
</menu>
manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="14" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.myapp.Principale"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.myapp.Home"
android:label="#string/title_activity_home" >
</activity>
<activity
android:name="com.example.myapp.ListDir"
android:label="#string/title_activity_list_dir" >
</activity>
</application>
</manifest>
In my case, what fixed the issue, was the answer of this question; to remove
android:theme="#style/AppTheme"
from the tag in the Manifest.
Have you tried to set the app theme to a theme that supports the ActionBar, like Theme.Holo.Light?
Also, in your Activity you can try getActionBar().show();.
Principale extends ActionBarActivity should take care of the issue
You have extended Activity. Try extending ActionBarActivity in java code
For your information, I solved my problem of the actionBar by going on the layout XML file and then pressing on View Option -> Show Layout Decorations.
Hope it will solve your problem.
Cheers.
Spent 3 hours and eventually fixed it! Forget removing android:theme="#style/AppTheme" from your manifest. It makes run-time error.
This is how I fixed the problem. Your activity should extend AppCompatActivity instead of Activity.
Check Your manifest file In the Activity declaration if this line is there then delete it.
android:theme="#style/AppTheme.NoActionBar"
on create of your activity.
getSupportActionBar().setDisplayShowCustomEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(false);
customNav = LayoutInflater.from(this).inflate(
R.layout.action_bar_customvieew, null);
getSupportActionBar().setCustomView(customNav);
your main activity needs to be like this:
import android.os.Bundle;
import android.app.ActionBar;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar actionBar = getActionBar();
actionBar.hide();
actionBar.show();
actionBar.setSubtitle("subtitle");
actionBar.setTitle("title");
}
#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, menu);
return true;
}
#Override public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_refresh:
Toast.makeText(this, "Menu Item 1 selected", Toast.LENGTH_SHORT)
.show();
break;
default:
break;
}
return true;
}
}
and your main.xml like this:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_refresh"
android:orderInCategory="100"
android:showAsAction="always"
android:icon="#drawable/ic_action_search"
android:title="Refresh"/>
<item
android:id="#+id/action_settings"
android:title="Settings">
</item>
</menu>
this is a good and simple guide to creating an action bar:
http://www.vogella.com/tutorials/AndroidActionBar/article.html
What solved my issue in this regard was, in the design view, click the eye icon (view options) and select Show Layout Decorations.
Make sure you don't use:
requestWindowFeature(Window.FEATURE_NO_TITLE);
in your layout .xml file.
In my case
Action bar menu was not visible
when used
public class MainActivity extends Activity {
}
but was visible when used
public class MainActivity extends ActionBarActivity{
}
I had the same problem and I confirm that if you delete android:theme="#style/AppTheme" from AndroidManifest.xml, it will solve your problem.
You should extend AppCompatActivity.
you have to call the method setHasOptionsMenu(true); in the OnCreate Method
i am using Samsung young 2 as android emulator and i have the same problem ,i reslov this by removing
android:theme ="" from the manifest.xml and changing the theme to another theme support action bar
getSupportActionBar().show();
Simply paste this code in your java onCreate function.
My Android studio version is 4.2.1 for Windows 64-bit (933 MiB). In my case, I did not have the "Show Layout Decorations" option in the design view. What I did is I solved my problem of the action bar by going on the layout XML file and then pressing on View Option -> Show System UI
ActionBar visibility in android studioļ¼š

How to show option menu in android 4.2

I am trying to create menu option in my test application.
I am able to see the menu when I set the theme in manifest to just default (The menu shows up in the top). If I set the theme in manifest to NoTitleBar. I can't see the menu option?
I want to get the menu when I set theme "NoTitleBar" in manifest.
How to fix it?
Below are things that I have used in my test application:
with Manifest:
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#android:style/Theme.NoTitleBar" >
<activity
android:name="com.ssn.menuoptions.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
And
Menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/menu_preferences"
android:title="Preferences" />
</menu>
Java file:
#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, menu);
return true;
}
Is it possible to get something like this:
How do I make menu to show up down?
Thanks!
Add following code below Activity.setContentView();
setContentView(R.layout.activity_detail);
// set option menu if has no hardware menu key
boolean hasMenu = ViewConfiguration.get(this).hasPermanentMenuKey();
if(!hasMenu){
//getWindow().setFlags(0x08000000, 0x08000000);
try {
getWindow().addFlags(WindowManager.LayoutParams.class.getField("FLAG_NEEDS_MENU_KEY").getInt(null));
}
catch (NoSuchFieldException e) {
// Ignore since this field won't exist in most versions of Android
}
catch (IllegalAccessException e) {
Log.w("Optionmenus", "Could not access FLAG_NEEDS_MENU_KEY in addLegacyOverflowButton()", e);
}
}
If you don't want an action bar in your app, then you can just set up a button with this handler:
myButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) { openOptionsMenu(); }
});
Now myButton works like the menu key. This is an easy way to migrate a fullscreen app to 4.x, though longer term a more elegant solution should be sought.
Using the notitlebar theme removes the actionbar where the menu button is. You'd probably have to make a custom theme to have your menu in it.
If you're not doing anything about it, you can always at least use this attribute: android:showAsAction="Never" so that users with phones that have a menu button can pop up the menu with their button.
Add in Android Manifest android:targetSdkVersion="10"
<uses-sdk
android:targetSdkVersion="10" />

how to force overflow menu in Actionbar using Actionbarsherlock on < 4.0 devices [duplicate]

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
ActionBarSherlock & HoloEverywhere - Forcing Overflow?
I am using ActionbarSherLock library to display Actionbar on pre-Gingerbread devices..
I want Overflow menu on actionbar.. I have searched a lot and i come to know that devices that has hardware Menu Button then Overflow Menu will not display..
and i also looked into ActionbarSherlock sample demo for that solution,bt still i can't get solution..
I have created demo for actionbar,when emulate on device which has no Menu Button then it will display overflow menu but if device has Menu button then overflow menu will not displaying..
Following is my Configuration :-
public class MainActivity extends SherlockActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
setTheme(R.style.Theme_Sherlock);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater=getSupportMenuInflater();
inflater.inflate(R.menu.activity_main, menu);
// return true;
return super.onCreateOptionsMenu(menu);
}
}
Manifest :-
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="demo.menu.actionbar_using_lib"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<uses-feature android:name="android.hardware.touchscreen" android:required="false"/>
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/Theme.Sherlock"
>
<activity
android:name=".MainActivity"
android:label="#string/title_activity_main"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
Menu Configuration :-
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/save"
android:title="#string/menu_save"
android:icon="#drawable/ic_action_search"
android:showAsAction="ifRoom|withText"/>
<item android:id="#+id/setting"
android:title="#string/menu_settings"
android:icon="#drawable/ic_action_search"
android:showAsAction="ifRoom|withText"/>
<item android:id="#+id/search"
android:title="#string/menu_search"
android:icon="#drawable/ic_action_search"
android:showAsAction="never"/>
<item android:id="#+id/refresh"
android:title="#string/menu_refersh"
android:icon="#drawable/ic_action_search"
android:showAsAction="never"/>
<item android:id="#+id/edit"
android:title="#string/menu_edit"
android:icon="#drawable/ic_action_search"
android:showAsAction="never"/>
<item android:id="#+id/delete"
android:title="#string/menu_delete"
android:icon="#drawable/ic_action_search"
android:showAsAction="never"/>
I want to display Overflow menu..so plz Suggest me ..
Thanks in Advance..
NOTE: With this suggestion, I am not recommending using ForceOverFlow to any reader. This is simply listing a possibility of making it work (forcing it to work rather). To each his own. Some may want it and like it too. Others may not.
I am probably speculating, but perhaps, this may do it for you.
You can think of this a hack, but I used it before to force the Overflow menu in one of my apps earlier and it works.
try {
ViewConfiguration config = ViewConfiguration.get(MainPage.this);
Field menuKeyField = ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey");
if (menuKeyField != null) {
menuKeyField.setAccessible(true);
menuKeyField.setBoolean(config, false);
}
} catch (Exception e) {
e.printStackTrace();
}
Also, the second link by MisterSmith has a solution of sorts with my answer in it. Commonsware has put down some thought about forcing the OverFlow menu here: How To Control use of OverFlow Menu in ICS
EDIT: While typing out this suggestion, you added a comment. To respond to that, I would like to point out that Jake Wharton took out .ForceOverFlow themes. I haven't tried it with version 4.2.0, but with a custom theme, it just might work. If you absolutely must use ForceOverFlow, you might have to use an older version. Read my answer here: https://stackoverflow.com/a/13180285/450534. Something might just make it work.
EDIT 2: As pointed out by the OP in a comment, the Demos Sample APK, in fact, does ForceOverFlow the action bar in Action Modes. I have a feeling, after checking the relevant Java files on github, that the answer to that lies perhaps in 3 Java files.
The Activity ActionModes adds menu items in a very unconventional manner: https://github.com/JakeWharton/ActionBarSherlock/blob/master/samples/demos/src/com/actionbarsherlock/sample/demos/ActionModes.java (Line 53)
The ActionMode Java file in the ABS Library: https://github.com/JakeWharton/ActionBarSherlock/blob/master/library/src/com/actionbarsherlock/view/ActionMode.java
The MenuItem Java file again part of the ABS Library: https://github.com/JakeWharton/ActionBarSherlock/blob/master/library/src/com/actionbarsherlock/view/MenuItem.java

Categories

Resources