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.
Related
getActionView() is retuning null. What am i doing wrong?
I am extending Activity and using android:minSdkVersion="11" android:targetSdkVersion="19"
<item
android:id="#+id/search"
android:actionViewClass="android.widget.SearchView"
android:icon="#drawable/ic_action_search"
android:showAsAction="collapseActionView|ifRoom"
app:showAsAction="always"
android:title="#string/search"/>
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
searchItem = menu.findItem(R.id.search);
mSearchView = (SearchView) searchItem.getActionView();
mSearchView.setQueryHint("Search");
return true;
}
manifest
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data
android:name="android.app.searchable"
android:resource="#xml/searchable" />
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
</activity>
I saw that you have app:showAsAction="always" the app namespace means that you are using Appcompat v7 library ...
Appcompat library has it owns method for menu items as static method in MenuCompat/MenuItemCompat classes (and you should use 'em like instead menu.methodXXX() use MenuCompat.methodXXX(menu) )
Now, to define a actionViewClass(and others attributes added in api newer then 11) in menu you should use the app namespace for this instead android namespace
so android:actionViewClass should become app:actionViewClass
in the code you should use MenuItemCompat.getActionView(searchItem) instead searchItem.getActionView()
remeber to add namespace app in root element of menu xml file like xmlns:app ="http://schemas.android.com/apk/res-auto"
also small hint (as you are using 11 as min sdk your code should works fine but ...) replace android.widget.SearchView to android.support.v7.widget.SearchView as it(standard SearchView) not works in the same way on different API versions from 11 to newest one(also you will get method not found if you use methods added in API > 11 to SearchView on devce with API 11)
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 :)
I have two activities that I want this navigation to happen, they are VendorsActivity and QuestionsActivity. The following how my AndroidManifest.xml looks like:
(I am not using the full name of my activities like com.hello.world.MyActivity as I am defined package attribute in manifest node.)
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".VendorsActivity"
android:label="#string/vendors_activity_title" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".TestsActivity"
android:label="#string/tests_activity_title"
android:parentActivityName=".VendorsActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".VendorsActivity" />
</activity>
<activity
android:name=".QuestionsActivity"
android:label="#string/questions_activity_title" >
</activity>
</application>
And in TestsActivity, I am calling getActionBar().setDisplayHomeAsUpEnabled(true); method from within onCreate method.
The problem is, it won't work unless I implement the following method in .TestsActivity class:
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case android.R.id.home:
NavUtils.navigateUpFromSameTask(TestsActivity.this);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
But Android Developer Guide says that I don't have to handle the Up button's event as mentioned at the very bottom of hit page: https://developer.android.com/training/basics/actionbar/adding-buttons.html
Because the system now knows MainActivity is the parent activity for
DisplayMessageActivity, when the user presses the Up button, the
system navigates to the parent activity as appropriate—you do not need
to handle the Up button's event.
Edit and Answer:
As Android Developer Guide says:
Beginning in Android 4.1 (API level 16), you can declare the logical
parent of each activity by specifying the android:parentActivityName
attribute in the element.
If your app supports Android 4.0 and lower, include the Support
Library with your app and add a element inside the
. Then specify the parent activity as the value for
android.support.PARENT_ACTIVITY, matching the
android:parentActivityName attribute.
So I think my problem was because of two reasons:
Running the app on a proper emulator. I was targeting a higher version but the emulator was running on API 14 (Android 4.0) so it didn't know how to handle android:parentActivityName attribute.
Targeting the right API level in Project Build Target properties as shown below:
If you receive an error similar to
E/NavUtils﹕ getParentActivityIntent: bad parentActivityName
or
does not have a parent activity name specified. (Did you forget to add the android.support.PARENT_ACTIVITY <meta-data> element in your manifest?)
You will probably need to change android:value to the full path.
So instead of
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity" />
do
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.package.SecondActivity" />
android:parentActivityName attribute supported only after API level.
One alternative is using support-library:v7 combined with NavUtils.
There is a great training material about this topic (include compatibility issue).
please check
- http://developer.android.com/training/implementing-navigation/ancestral.html
That being said, i am posting my code below because it is working :-
MainActivity.Java
package com.example.activitydatasend;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button click = (Button) findViewById(R.id.click);
click.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent(MainActivity.this,Second.class);
intent.putExtra("first", "first");
intent.putExtra("second", "second");
startActivity(intent);
}
});
}
#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;
}
}
SecondActivity.java
package com.example.activitydatasend;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class Second extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
Bundle bundle = getIntent().getExtras();
String a = bundle.getString("first");
String b = bundle.getString("second");
System.out.println(a+b);
getActionBar().setDisplayHomeAsUpEnabled(true);
}
#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;
}
}
AndroidManifext.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.activitydatasend"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
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.example.activitydatasend.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>
<activity
android:name="com.example.activitydatasend.Second"
android:label="#string/app_name"
android:parentActivityName="com.example.activitydatasend.MainActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/click"
android:text="Click" />
</RelativeLayout>
second.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Second Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
you can add metadata in manifest to support pre JellyBean OS.
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".VendorsActivity" />
What API version are you using? In my experience, for older versions it is necessary to handle the up button event yourself, whereas with later versions it's done for you.
I haven't looked into which exact version changes this, but I know that API 13 doesn't handle it for you, while API 16 does.
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:
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" />