I have an application which works fine on apis 4.0+ but on older apis menus are not showing up. I use the last version of the appcompat library.
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.main, menu);
return true;
}
Fragments :
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater)
{
inflater.inflate(R.menu.interval_timer, menu);
super.onCreateOptionsMenu(menu, inflater);
}
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" >
<item
android:id="#+id/action_interval_training"
android:title="#string/action_interval_training"
app:showAsAction="never"/>
<item
android:id="#+id/action_advanced_session"
android:title="#string/action_advanced_session"
app:showAsAction="never"/>
</menu>
Thank you
EDIT : it seems that it doesnt work only if i set showAsAction="never" ...
On devices with a hardware menu key (such as all Gingerbread devices), that hardware button is still used to trigger the overflow menu - where all items with showAsAction="never" appear.
Related
My SearchView is working fine.. but it is accepting the same behavior when other menuItems are clicked. When I click cart icon then too SearchView is getting called. How do I avoid that?
Here is my code for the menu:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto">
<!-- Search Widget -->
<item
android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"
yourapp:showAsAction="ifRoom|collapseActionView"
yourapp:actionViewClass="android.support.v7.widget.SearchView"/>
<item
android:id="#+id/action_wishlist"
android:icon="#drawable/ic_shopping_cart"
android:title="#string/action_cart"
yourapp:showAsAction="ifRoom|collapseActionView"
yourapp:actionViewClass="android.support.v7.widget.SearchView"/>
<item
android:id="#+id/action_cart"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"
yourapp:showAsAction="ifRoom|collapseActionView"
yourapp:actionViewClass="android.support.v7.widget.SearchView"/>
</menu>
and code for SearchView:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_main, menu);
MenuItem searchItem= menu.findItem(R.id.action_search);
SearchView searchView =(SearchView) MenuItemCompat.getActionView(searchItem);
searchView.setIconifiedByDefault(false);
return super.onCreateOptionsMenu(menu);
}
I want a different behavior when cart icon is clicked but it just does the same action as when clicked on the search icon. I want to limit the SearchView only to the SearchView icon. On the cart Icon i want to perform something else. Please help
I just had to remove:
yourapp:showAsAction="ifRoom|collapseActionView"
yourapp:actionViewClass="android.support.v7.widget.SearchView"
from other menu items which are not SearchView
I have made an option menu in my application and for some reason it works on one of my smartphones and not the other. The smartphone the option menu works on is android lollipop 5.0 the smartphone it doesn't work on is jellybean 4.1. I'm not sure if this has anything to do with it but I need the option menu to work on both phones. ANyone any ideas on why this is happening
My Code is as fallows
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);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_Menu) {
return true;
}
return super.onOptionsItemSelected(item);
}
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="saveourcar.soc.MainActivity">
<item
android:id="#+id/action_Menu"
android:orderInCategory="100"
android:title="Menu"
app:showAsAction="never" >
<menu>
<item
android:id="#+id/instructions"
android:title="Instructions"
android:icon="#drawable/rench"/>
<item
android:id="#+id/hotels"
android:title="Hotels"
android:icon="#drawable/hotel"/>
</menu>
</item>
</menu>
Try this:
In onCreateView method:
setHasOptionsMenuEnabled(true);
or this on menu layout:
android:showAsAction="always"
I followed this tutorial to build up a Sliding Tab application :
http://www.android4devs.com/2015/01/how-to-make-material-design-sliding-tabs.html
Now my Question:
How can I change my Menu icons in my actionbar when I change to another Tab ?For example in Tab1 you can see a Search Icon and when you change to Tab2 this gets invisible but there comes a Add icon.
My code till now :
#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);
menu.add("menu_search").setIcon(R.drawable.ic_search_white_24dp).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
menu.add("menu_contact").setIcon(R.drawable.ic_add_white_24dp).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
return super.onCreateOptionsMenu(menu);
}
You can define menu in Fragment instead of Activity:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// ...
setHasOptionsMenu(true);
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_main, menu);
super.onCreateOptionsMenu(menu, inflater);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_item_1:
// handling menu item click
break;
}
return super.onOptionsItemSelected(item);
}
Of course each Fragment will have its own menu with corresponding icons.
This is an example (menu_main.xml):
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/menu_item_1"
android:showAsAction="ifRoom"
android:title="#string/ic_menu_label"
android:icon="#drawable/ic_menu_icon"
android:visible="true"
app:showAsAction="ifRoom" />
</menu>
I'm building an Android app using the AppCompat_v7 library and I'm having troubles with the action bar.
I have an AppBarActivity which extends ActionBarActivity - I'm using the same actionbar for all of my activities. MainActivity extends AppBarActivity. I've defined four actionbar icons, two of which I'd like to be displayed at a time. I've been careful to define my own namespace, ie. app:showAsAction="always"/>
In my app, I toggle the visibility of these icons in onPrepareOptionsMenu()
MenuItem contactOn = menu.findItem(R.id.contact_toggle_button_on);
MenuItem contactOff = menu.findItem(R.id.contact_toggle_button_off);
contactOn.setVisible(!useContacts);
contactOn.setEnabled(!useContacts);
contactOff.setVisible(useContacts);
contactOff.setEnabled(useContacts);
Do I need to specify a namespace here somehow? Because it doesn't seem that these methods are doing anything, except, curiously, rearranging their names in the overflow list. Also, only three of the four buttons show up there, which I don't understand either but I expect that's a different problem.
UPDATE: posting code & xml
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inf = new MenuInflater(this);
inf.inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
MenuItem contactOn = menu.findItem(R.id.contact_toggle_button_on);
MenuItem contactOff = menu.findItem(R.id.contact_toggle_button_off);
contactOn.setVisible(!useContacts);
contactOn.setEnabled(!useContacts);
contactOff.setVisible(useContacts);
contactOff.setEnabled(useContacts);
MenuItem locationOn = menu.findItem(R.id.location_toggle_button_on);
MenuItem locationOff = menu.findItem(R.id.location_toggle_button_off);
locationOn.setVisible(useLocation);
locationOn.setEnabled(useLocation);
locationOff.setVisible(!useLocation);
locationOff.setEnabled(!useLocation);
return true;
}
And the 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="com.example.skortchm.strangerapp.MainActivity" >
<!-- Contacts Toggle, should appear as action button -->
<item
android:id="#+id/contact_toggle_button_on"
android:icon="#drawable/ic_action_call"
android:title="#string/contact_toggle_on"
app:showAsAction="always"/>
<item
android:id="#+id/contact_toggle_button_off"
android:icon="#drawable/ic_action_call_off"
android:title="#string/contact_toggle_off"
app:showAsAction="always"/>
<!-- Location Toggle, should appear as action button -->
<item
android:id="#+id/location_toggle_button_on"
android:icon="#drawable/ic_action_location_searching"
android:title="#string/location_toggle_on"
app:showAsAction="always"/>
<item
android:id="#+id/location_toggle_button_off"
android:icon="#drawable/ic_action_location_off"
android:title="#string/location_toggle_off"
app:showAsAction="always"/>
<!-- Settings, should always be in the overflow -->
<!-- Add gear icon here -->
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:title="#string/action_settings"
app:showAsAction="never"/>
</menu>
You need to use getMenuInflater to inflate the menu.
You are creating an instance for MenuInflater, that is not right . you have to use the instance associated with the Activity
Change your onCreateOptionsMenu as
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
The android native menu is not opening when pressing menu button while using PhoneGap. When I am pressing menu button, onCreateOptionsMenu() method is getting called but the menu is not getting created.
public class PhoneGapActivity extends DroidGap {
#Override
public void onCreate(Bundle savedInstanceState) {
super.setBooleanProperty("showTitle", true);
super.onCreate(savedInstanceState);
super.loadUrl("file:///android_asset/www/index.html");
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
super.onCreateOptionsMenu(menu);
Log.d("Menu","Menu option called");
getMenuInflater().inflate(R.menu.phone_gap, menu);
return true;
}
}
The log is getting printed, that means the function is getting called.
XML file for menu: When the android:showAsAction is set to always its showing in the action bar, otherwise its not working.
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_settings"
android:icon="#drawable/ic_menu_settings_holo_light"
android:showAsAction="always"
android:title="#string/action_settings"/>
<item
android:id="#+id/more"
android:icon="#drawable/ic_menu_moreoverflow_normal_holo_light"
android:showAsAction="never"
android:title="#string/more"/>
</menu>
What could be the problem?
Solved it. The bug was in PhoneGap 2.6. I downloaded PhoneGap version 2.5 and it worked normally.