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.
Related
In my app I need to use the ActionMode.
I have followed several Guides and Tutorials (https://medium.com/asos-techblog/style-actionmode-on-android-5e613fa77c32, https://developer.android.com/guide/topics/ui/menus e.g.) on implementing my Contextual Action Menu but as you can see in this image:
https://i.stack.imgur.com/ag2Mp.jpg
There is part of the titleBar still visible on the bottom. My question is if there is anything i can do to change this behaviour.
I also tried to change the style of my ActionModeBar but when i change the styling it becomes invisible (only showing the icons) and the titlebar is completely visible.
I have not made any changes to the ActionMode except the menu that contains the buttons:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/stopCab"
android:title="Stop contextual action bar"
app:showAsAction="ifRoom"
android:icon="#drawable/ic_baseline_cancel_24"/>
<item android:id="#+id/deleteCab"
android:title="Delete selected items"
app:showAsAction="ifRoom"
android:icon="#drawable/ic_delete_white_24dp"/>
</menu>
Initializing the ActionMode:
private ActionMode.Callback actionModeCallback = new ActionMode.Callback() {
#Override
public boolean onCreateActionMode(ActionMode actionMode, Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.contextual_action_bar_menu, menu);
return true;
}
#Override
public boolean onPrepareActionMode(ActionMode actionMode, Menu menu) {
return false;
}
#Override
public boolean onActionItemClicked(ActionMode actionMode, MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.deleteCab:
//TODO: do something
return true;
case R.id.stopCab:
//TODO: do something
return true;
default:
return false;
}
}
#Override
public void onDestroyActionMode(ActionMode actionMode) {
FieldChooserActivity.this.actionMode = null;
}
};
And starting the ActionMode:
FieldChooserActivity.this.actionMode = startActionMode(FieldChooserActivity.this.actionModeCallback);
In my Style the AppTheme parent is Theme.AppCompat.Light.DarkActionBar
And each Activity has
android:theme="#style/AppTheme.NoActionBar">
in AndroidManifest.
Maybe this is the fault why i cant change the style without the ActionMode becoming invisible.
I hope someone can help me with this matter and thanks in advance!
I found a solution that fixed my problem (where the ActionMode bar doesn't cover the Toolbar).
As i looked at the layout of my activity i saw that in the Toolbar is a height attribute called '?attr/actionBarSize':
<androidx.appcompat.widget.Toolbar
android:id="#+id/fieldChooserToolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay">
I then added the height to a custom style for my ActionMode:
<style name="ActionModeTheme" parent="AppTheme">
<item name="windowActionModeOverlay">true</item>
<item name="actionModeCloseDrawable">#drawable/ic_delete_white_24dp</item>
<item name="actionMenuTextColor">#android:color/white</item>
<item name="height">?attr/actionBarSize</item>
<item name="background">#color/colorSecondary</item>
<item name="android:src">#drawable/ic_baseline_close_24</item>
</style>
The last part was that i added following line to my AppTheme-styling:
<item name="actionModeStyle">#style/ActionModeTheme</item>
And then it worked!
The second problem where my ActionMode bar was invisible was most likely caused by adding the actionModeStyling to the AppTheme.NoActionBar.
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>
Im trying to do a menu with only one icon, but instead of that icon, i am getting the typican three dots, and inside my option. So, i only want one icon. That is my XML code:
<?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"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.bartek.gestionpea.MainActivity">
<item
android:id="#+id/ayuda"
android:icon="#android:drawable/ic_dialog_info"
android:title="Ayuda"
app:showAsAction="ifRoom"/>
</menu>
Here goes my class:
public class MainActivity extends Activity {
PeniaSQLiteHelper usdbh;
private SQLiteDatabase db;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getActionBar().setTitle("GESTION");
Button btnCrearPena = (Button) findViewById(R.id.btncrearpena);
btnCrearPena.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(MainActivity.this, CrearNueva.class);
startActivity(i);
}
});
}
#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_principal, menu);
return true;
}
#Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
switch (item.getItemId()) {
case R.id.ayuda:
return true;
}
return super.onMenuItemSelected(featureId, item);
}
}
You are using some things for the native action bar (e.g., inheriting from Activity) and some things for the appcompat-v7 action bar backport (e.g., app:showAsAction). Choose one and stick with it:
If you wish to use the native action bar, use android:showAsAction
If you wish to use the appcompat-v7 action bar backport, inherit from AppCompatActivity (in the latest appcompat-v7) or ActionBarActivity (for prior versions of appcompat-v7)
Since you use the standard Activity, try this:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/ayuda"
android:icon="#android:drawable/ic_dialog_info"
android:title="Ayuda"
android:showAsAction="always" />
</menu>
You can modify the app theme (in themes.xml):
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<item name="android:actionOverflowButtonStyle">#style/MyActionButtonOverflow</item>
</style>
<!-- Style to replace actionbar overflow icon. set item 'android:actionOverflowButtonStyle' in AppTheme -->
<style name="MyActionButtonOverflow" parent="android:style/Widget.Holo.Light.ActionButton.Overflow">
<item name="android:src">#drawable/your_icon_here</item>
<item name="android:background">?android:attr/actionBarItemBackground</item>
<item name="android:contentDescription">"options"</item>
</style>
More info here
Note that when using appcompat themes, you sometimes need to set xml attibutes twice:
once with and once without the android namespace
<item name="android:windowNoTitle">true</item>
<item name="windowNoTitle">true</item>
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.
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;
}