Toggling Icons In ActionBarActivity - java

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;
}

Related

Actionmode Bar doesn't cover whole titlebar

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.

Java android Add dynamically item to menu

I want to added to menu item but a user can choose what he want to see or hide , but I don't know how I can do it . I did this :
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:title="NARZĘDZIA">
<menu>
<item
android:id="#+id/show_point"
android:icon="#drawable/point"
android:title="Pokaż wybraną pozycję obiektu" />
<item
android:id="#+id/sync"
android:icon="#drawable/sync"
android:title="Pojedyńcze odpytanie" />
<item
android:id="#+id/show_chooce"
android:icon="#drawable/show_chooce"
android:title="Pokaż wybrane obiekty" />
<item
android:id="#+id/my_location"
android:icon="#drawable/position2"
android:title="Moja lokalizacja" />
</menu>
</item>
<item android:title="APLIKACJA">
<menu>
<item
android:id="#+id/settigs"
android:icon="#drawable/set"
android:title="Ustawienia aplikacji" />
<item
android:id="#+id/about"
android:icon="#drawable/about"
android:title="O programie" />
</menu>
</item>
</menu>
Now I want to create a Activity in which user can choose what it will be show , but I don't have idea how I can hide this item on menu
you can change java file
//menu intialation
#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.clear();
MenuItem item =
menu.add(Menu.FIRST, R.id.dashboard_nodifycation, 3, "NODIFICATION");
item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
menu.add(Menu.FIRST, R.id.System_log, 4, "LOGOUT");
dbHelper = new Database_for_GCM_data(Activity_Drawer_Dashboard.this);
int unread_count = Integer.parseInt(dbHelper.getUnreadCount());
// calling function for nodification inflater
item.setIcon(buildCounterDrawable(unread_count, R.drawable.white_nodification, getApplicationContext()));
return true;
}
public class MyActivity extends AppCompatActivity {
private Menu mToolbarMenu;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
//init menu here
mToolbarMenu = menu;
getMenuInflater().inflate(R.menu.toolbar, menu);
setToolbarMenuItemVisibilties();
return true;
}
private void setToolbarMenuItemVisibilties() {
final MenuItem menuItem1 = mToolbarMenu.findItem(R.id.item1);
final MenuItem menuItem2 = mToolbarMenu.findItem(R.id.item2);
final MenuItem menuItem3 = mToolbarMenu.findItem(R.id.item3);
final MenuItem menuItem4 = mToolbarMenu.findItem(R.id.item4);
//Set visibilities
menuItem1.setVisible(true);
menuItem2.setVisible(true);
menuItem3.setVisible(false);
menuItem4.setVisible(false);
}
}
you can change visibilities any time in activity, but you have to inflate it first

Android: Searchview opens even onClick of other menu items with different id

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

Menus not showing up on older apis

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.

Native android menu not showing in Phonegap

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.

Categories

Resources