i have problem putting the menu there, in the upper right corner:
android manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="it.mio.app"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="11"
android:targetSdkVersion="15" />
<application android:allowBackup="false"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#android:style/Theme.Light">
<activity android:name=".Lista_preventivi"
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>
Java:
public class Lista_preventivi extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.lista_preventivi);
}
#Override
public boolean onCreateOptionsMenu(Menu menu){
getMenuInflater().inflate(R.menu.menu, menu);
return true;
}
}
menu xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/action_settings"
android:showAsAction="ifRoom"
android:title="#string/action_settings" />
</menu>
all works, but the menu appear only if i click the physical button on my phone, and it appear at the bottom like a normal menu.
ho can i have the icon menu in the upper right action bar, and show the menu there?
Thanks!
The theme which your are applying to the application is android:theme="#android:style/Theme.Light". it doesn't have an actionbar.
Change it to something which has an action bar like,
Theme.Sherlock.Light (Need to add ABS library)
Theme.Holo.Light
The code is just fine.
Add:
android:showAsAction="always" in your manifest file
If your sdk version is >3.0 .Else you have no other option other than implementing Sherlock Home Api.
Related
here is my code
MainActivity
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; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
}
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_settings" android:title="#string/action_settings"
android:orderInCategory="100" app:showAsAction="never" />
<item android:id="#+id/one" android:title="oneone"
android:orderInCategory="100" app:showAsAction="always" />
<item android:id="#+id/two" android:title="twotwo"
android:orderInCategory="100" app:showAsAction="ifRoom" />
<item android:id="#+id/three" android:title="threethree"
android:orderInCategory="100" app:showAsAction="ifRoom" />
<item android:id="#+id/four" android:title="fourfour"
android:orderInCategory="100" app:showAsAction="ifRoom" />
</menu>
manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.sanjith.actionbartwo" >
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme"
>
<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>
</activity>
</application>
</manifest>
This code gives me three buttons on the actionbar and one on the overflow.
I want to get all the buttons on the bottom actionbar.I have searched the internet for this but i haven't found a solution.
Please help.
Try this http://developer.android.com/guide/topics/ui/actionbar.html#SplitBar
<manifest ...>
<activity uiOptions="splitActionBarWhenNarrow" ... >
<meta-data android:name="android.support.UI_OPTIONS"
android:value="splitActionBarWhenNarrow" />
</activity>
</manifest>
I have encountered this problem before unfortunately the action bar doesn't split by code you need to fill all the room available so the action bar would split by itself with your permission of course but there are no possible way to split it if it still have space
I have trouble seeing the icons in the ActionBar.
I also read around here in StackOverFlow, but nothing...
Here is my action.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" >
<item
android:id="#+id/addAct"
android:icon="#android:drawable/ic_input_add"
android:background="#null"
app:showAsAction="always"
android:title="#string/add"
/>
<item
android:id="#+id/searchAct"
android:icon="#android:drawable/ic_menu_search"
app:showAsAction="ifRoom"
android:title="Cerca"
android:actionViewClass="android.widget.SearchView"
/>
<item
android:id="#+id/visualizza"
android:title="#string/txtVisualizza"
app:showAsAction="always"
>
</item>
Here, is AndroiManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<uses-sdk
android:minSdkVersion="11"
android:maxSdkVersion="21"/>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<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>
</activity>
<activity
android:name=".WriteActivity"
android:label="#string/title_activity_write" >
</activity>
</application>
Here, MainActivity:
public class MainActivity extends Activity { ...
#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);
return super.onCreateOptionsMenu(menu);
}
In the xml code, I have used the app for ShowAsAction, but this not seems be the problem.
Thanks for any help :)
You are developing app for material design ( Target SDK = 21).
It does not support Activity.
So change extends Activity to extends ActionBarActivity.
Now your menu will be visible.
And you can edit action bar using getSupportActionBar().
I am trying to implement action bar(appcompatv_7) on API version 8. Although there is no error in my code but the action bar won't show up, instead it will appear in text format by pressing menu button.
Here is the java code:
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; this adds items to the action bar if it is present.
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
}
Code for menu:
<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="com.example.usingsherlockkk.MainActivity" >
<item
android:id="#+id/item1"
android:icon="#android:drawable/ic_menu_add"
android:showAsAction="ifRoom"
android:title="Title1">
</item>
<item
android:id="#+id/item2"
android:icon="#android:drawable/ic_menu_delete"
android:showAsAction="ifRoom"
android:title="Title2">
</item>
<item
android:id="#+id/reset"
android:icon="#android:drawable/ic_menu_revert"
android:showAsAction="ifRoom"
android:title="Revert">
</item>
<item
android:id="#+id/item4"
android:icon="#android:drawable/ic_menu_edit"
android:showAsAction="ifRoom"
android:title="Title4">
</item>
<item
android:id="#+id/item5"
android:icon="#android:drawable/ic_menu_help"
android:showAsAction="ifRoom"
android:title="Title5">
</item>
</menu>
Manifest file code:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.usingsherlockkk"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/Theme.AppCompat.Light"
android:uiOptions="splitActionBarWhenNarrow" >
<activity
android:name="com.example.usingsherlockkk.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>
Guys my problem is that all the menu items are displayed in overflow where only the title appears. I checked it with big/small screen devices but it remains the same.
I hope I have explained my problem well enough. Let me know if something is hazy
Try this code
#Override
protected void onCreate(Bundle arg0) {
getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
super.onCreate(arg0);
setContentView(R.layout.activity_main);
}
Ok guys I got the solution to my very own problem.
The problem was in defining the action items in the "menu.xml".
Android documentation says you cannot use "android:showAsAction" on older devices as it does not exist in older versions. You have to use a custom namespace. Instead of saying "android:showAsAction", use "app:showAsAction" and you are good to go. Here is the attachment from official page:
<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>
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.
I hope it helps someone out there.
The way I did it is as follows:
There was a line in AndroidManifest.xml file
android:theme="#style/AppTheme.NoActionBar"
I simply changed it to
android:theme="#style/AppTheme"
and problem was solved.
I tried to implement an action bar in my application.
menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/itemAdd"
android:showAsAction="ifRoom|withText"
android:title="ADD">
</item>
<item
android:id="#+id/itemRefresh"
android:showAsAction="ifRoom|withText"
android:title="REFRESH">
</item>
<item
android:id="#+id/itemHelp"
android:title="HELP">
</item>
</menu>
And created menu
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
getMenuInflater().inflate(R.menu.menu, menu);
return true;
}
But it does not show the action bar even if minSdkVersion is 11. What is the reason?
Remove your theme for your actionbar activity
in androidManifest file. Now it will work...
<application
android:allowBackup="true"
android:icon="#drawable/tasktodo"
android:label="#string/app_name"
>
Don't add any theme in your application manifest file. If you added one, please remove and try running it...
change Activity to AppCompatActivity in your class. That should be the easiest if you want to add it fast.. I'll add the code for someone who is new to Android OS:
public class YourActivity extends Activity
into
public class YourActivity extends AppCompatActivity
You have to set the style of your Activity to Theme.Holo or one of its variants for the ActionBar to show. If you want to keep backwards-compatibility, call setTheme in onCreate of your Activity:
setTheme(android.R.style.Theme_Holo);
An application with a Manifest like this
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.Actionbartest"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="7" android:targetSdkVersion="11" />
<application android:label="#string/app_name" android:icon="#drawable/ic_launcher">
<activity android:name="MyActivity"
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>
Menu.xml like this
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/itemAdd"
android:showAsAction="ifRoom|withText"
android:title="ADD">
</item>
<item
android:id="#+id/itemRefresh"
android:showAsAction="ifRoom|withText"
android:title="REFRESH">
</item>
<item
android:id="#+id/itemHelp"
android:title="HELP">
</item>
</menu>
And Activity like this
package com.example.Actionbartest;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
public class MyActivity extends Activity {
/**
* Called when the activity is first created.
*/
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
getMenuInflater().inflate(R.menu.menu, menu);
return true;
}
}
Looks like this.
Are you sure your phone or emulator is running Android 3.0 or above? If not, you will end up with your screenshot.
To enable The Actionbar on older devices, you should use the AppCompat/support library (https://developer.android.com/tools/support-library/features.html)
android:allowBackup="true"
android:icon="#drawable/ic2"
android:label="#string/app_name"
android:theme="#android:style/Theme.Holo.Light.DarkActionBar"
this works. Put it in your
I import import android.support.v7.app.AppCompatActivity;
then edit to public class MainActivity extends AppCompatActivity
Add to dependencies
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:25.0.0'
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android" ... >
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".MyActivity" android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<!-- Put the RTA label here ... -->
<meta-data android:name="RTA" android:value="RTA-5042-1996-1400-1577-RTA" />
</activity>
</application>
</manifest>
I am trying to enable ActionBar in an Activity.
Here is my Manifest file.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.assistant.lab.royale"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="11" android:targetSdkVersion="15"/>
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".LabAssistant"
android:label="#string/app_name"
android:theme="#android:style/Theme.Holo">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
and here's the code :
.....
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.abmenu, menu);
return true;
}
.....
I'm following the guide over here. The ActionBar menu should show up now according to the guide but all I'm getting is the old options menu. Why?
Edit : menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/menuReset"
android:alphabeticShortcut="s"
android:icon="#android:drawable/ic_menu_preferences"
android:showAsAction="withText"
android:title="#string/menu_reset"/>
<item
android:id="#+id/menuAbout"
android:alphabeticShortcut="b"
android:icon="#android:drawable/ic_menu_info_details"
android:showAsAction="withText"
android:title="#string/menu_about"/>
</menu>
Did you include showAsAction in your abmenu.xml items?
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/menu_save"
android:icon="#drawable/ic_menu_save"
android:title="#string/menu_save"
// This line //
android:showAsAction="ifRoom|withText" />
</menu>