I am not sure why the action bar does not appear on my main menu, i already cleaned my project but it still doesn't work.
the main xml
<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"/>
<item android:id="#+id/mainmenu"
android:title="Main Menu"
android:orderInCategory="1"
android:showAsAction="always"/>
<item android:id="#+id/play_actionbar"
android:title="Play"
android:orderInCategory="2"
android:showAsAction="always"/>
<item android:id="#+id/admin_actionbar"
android:title="Admin"
android:orderInCategory="3"
android:showAsAction="always"/>
<item android:id="#+id/video_actiobar"
android:title="Video"
android:orderInCategory="4"
android:showAsAction="always"/>
the java code
#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;
}
try this
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
// TODO Auto-generated method stub
MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.main, (Menu) menu);
return super.onCreateOptionsMenu(menu);
}
change the code to this way and check it works
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
check the link for more how it works (http://www.androidhive.info/2013/11/android-working-with-action-bar/)
In your Manifest, for your activity, make sure you declare it has Actionbar:
<activity
android:name="com.test.activities.MyActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:theme="#android:style/Theme.Holo.Light.DarkActionBar" >
You can use ActionBarsherlock or AppCompat for supporting actionbar for android versions below 3.0 .
For using ABS(actionbarsherlock)
You can extend your activity by SherlockActivity
and change your method to
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
// TODO Auto-generated method stub
MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.main, (Menu) menu);
return super.onCreateOptionsMenu(menu);
}
as answered by #Amer Hadi
check here for more details ActionBar
try 100% working on 2.2 to 4.4. just add support libraries.
activity_main_actions.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" >
<!-- Refresh -->
<item
android:id="#+id/action_refresh"
android:icon="#drawable/ic_action_refresh"
android:title="#string/action_refresh"
app:showAsAction="always"/>
<!-- Help -->
<item
android:id="#+id/action_help"
android:icon="#drawable/ic_action_help"
android:title="#string/action_help"
app:showAsAction="never"/>
<!-- Check updates -->
<item
android:id="#+id/action_check_updates"
android:icon="#drawable/ic_action_refresh"
android:title="#string/action_check_updates"
app:showAsAction="never"/>
code in activity:
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.activity_main_actions, menu);
return super.onCreateOptionsMenu(menu);
}
Related
I am going to inflate a menu in my first Activity.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return true;
}
The R.menu.main:
<item
android:id="#+id/action_serach"
android:title="Search"
android:orderInCategory="100"
android:icon="#drawable/ic_search"
app:showAsAction="ifRoom" />
It doesn't work, but it inflates the another XML file named R.menu.menu_home.
The R.menu.menu_home:
<menu
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/action_star"
android:title="Search"
android:orderInCategory="100"
android:icon="#drawable/ic_star_half"
app:showAsAction="ifRoom"
/>
</menu>
But in my second Activity, it works, whether it's R.menu.main or R.menu.menu_home.
why?
I think the problem is that you have to Override the class method, so add #Override before.
Here you can get more info about the Override annotation.
I have solve this problem.It's because i replace the framelayout with a fragment in the activity and the onCreateOptionsMenu() method is in the fragment.
Use this code.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return true;
}
I want to add an action to the action bar, but it appears in my action bar as a drop down list.
How can I add button to the action bar?
My code is:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="Add"
showAsAction="ifRoom"/>
</menu>
#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);
}
If you are using android.app.Activity simply change showAsAction="always" to "android:showAsAction="always".
If you are using android.support.v7.app.Activity change the code as follows:
<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:icon="#drawable/ic_action_search"
android:title="Add"
app:showAsAction="ifRoom"/>
</menu>
You just simply add the one more item into menu and inflate that menu into your activity ....
like below you will add your button and access that button into your activity...
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/refresh"
android:icon="#android:drawable/stat_notify_sync"
showAsAction="ifRoom"/>
<item
android:id="#+id/action_bar_button_cancel"
android:focusableInTouchMode="true"
android:icon="#android:drawable/ic_menu_close_clear_cancel"
showAsAction="ifRoom"/>
</menu>
Inflating the menu like below.....
#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);
}
Accessing method in action bar item is....
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.refresh:
//logic
return true;
case R.id.action_bar_button_cancel:
//logic
return true;
default:
return super.onOptionsItemSelected(item);
}
}
that's if you have any doubt comment
I have an activity that extends the ActionBarActivity that's included in the support package revision 18. I have a menu item that contains a submenu and it works fine when I load up the app. However, if I call supportInvalidateOptionsMenu() for some reason the submenu doesn't pop up any more. The related code would be the xml for the menu
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:myapp="http://schemas.android.com/apk/res-auto" >
<item
android:id="#+id/menu_search"
android:icon="#drawable/ic_search"
android:title="#string/menu_search"
myapp:actionViewClass="android.support.v7.widget.SearchView"
myapp:showAsAction="always|collapseActionView"/>
<item
android:id="#+id/menu_now_playing"
android:icon="#drawable/ic_nowplaying"
android:title="#string/menu_nowplaying"
myapp:showAsAction="always"/>
<item
android:id="#+id/menu_station_overflow"
android:icon="#drawable/ic_overflow"
android:title="#string/more"
myapp:showAsAction="always">
<menu>
<item
android:id="#+id/menu_favorite"
android:icon="#drawable/ic_favorite"
android:title="#string/favorite"/>
</menu>
</item>
</menu>
And then the code to create the menu
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_activity, menu);
return super.onCreateOptionsMenu(menu);
}
I should note that this problem occurs on Gingerbread devices but there are no problems on android 4.x. Does anyone have any idea what might be going on here?
Here is a work-around (as we had the same issue). Any menu items that need to be modified later, we put into instance variables, example:
private MenuItem stationMenuItem;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_activity, menu);
stationMenuItem = menu.findItem(R.id.menu_station_overflow);
return super.onCreateOptionsMenu(menu);
}
public void doStuff(boolean menuVisible) {
if (stationMenuItem != null) {
stationMenuItem.setVisible(menuVisible);
}
}
This is NOT an ideal solution, but something that will work till this is fixed. Changes to menu items SHOULD happen in onPrepareOptionsMenu(Menu menu) after calling supportInvalidateOptionsMenu()
Do not return super.onCreateOptionsMenu(menu); because that will always return false. Just return true.
I don't know what to do with this problem. I changed it a lot but it still don't show sherlock action bar items.
here is my code:
public boolean onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu);
MenuInflater minflater = this.getSupportMenuInflater();
minflater.inflate(R.menu.activity_main, (com.actionbarsherlock.view.Menu) menu);
return super.onCreateOptionsMenu(menu);
}
I use Sherlock Activity.
if any one can help me please notice me.
Thanks
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater minflater = getSupportMenuInflater();
minflater.inflate(R.menu.activity_main, menu);
return true;
}
And make sure you have these imports:
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
You should try and avoid these: minflater.inflate(R.menu.activity_main, (com.actionbarsherlock.view.Menu) menu);
And declare your activity_main.xml like this (pseudo code):
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/menuStatusUpdate"
android:icon="#drawable/ic_action_status"
android:showAsAction="ifRoom|withText"
android:title="Status Update">
</item>
<item
android:id="#+id/menuPhotoUpload"
android:icon="#drawable/ic_action_gallery"
android:showAsAction="ifRoom|withText"
android:title="Photo">
</item>
</menu>
You can play with this attribute: android:showAsAction="ifRoom|withText" and replace the ifRoom|withText with always
I have this code to create the menu:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.tip_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case MNU_PREV:
animateTextViewsPrev();
break;
case MNU_NEXT:
animateTextViewsNext();
break;
}
return true;
}
And the XML:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/prev_tip" android:title="#string/prevTip"></item>
<item android:id="#+id/next_tip" android:title="#string/nextTip"></item>
</menu>
In a smartphone with Android 2.1 the menu is visible but in other mobile whit 4.1.1 is invisible.
Somebody now how to solve it?
What is you target Android, good to know, in android 4.0 them has redesign the menu layout.
I think you is missing super.onCreateOptionsMenu(menu); in the call onCreateOptionsMenu
In my code I has,
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
I was dealing with the same problem.. read some queries and documentation.. Hope this might help you.
Here's my XML file for a menu..
<item
android:id="#+id/action_send_feedback"
android:orderInCategory="100"
android:showAsAction="always"
android:title="#string/action_send_feedback"/>
<item
android:id="#+id/action_share_app"
android:orderInCategory="100"
android:showAsAction="ifRoom"
android:title="#string/action_share_app"
android:icon="#drawable/ic_action_share" />
<item
android:id="#+id/action_rate_app"
android:orderInCategory="100"
android:showAsAction="never"
android:title="#string/action_rate_app"/>
JAVA Code goes here..
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
For android phones which have option button (at the bottom of the phone) the menu item which are showAsAction="never" comes when the button is pressed.. or else they will be shown normally on the action bar options menu..
Ref: http://developer.android.com/guide/topics/ui/menus.html#options-menu
You can simply change the "targetSdkVersion" to 10 in manifest file
It needs the ID in the java! :)