Disable Android menu group - java

I try to disable a menu group with the following code, but it doesn't work, menu items are still enabled. Can you tell me what's wrong please?
res/menu/menu.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/a" android:title="A"></item>
<item android:id="#+id/b" android:title="B">
<menu>
<item android:id="#+id/c" android:title="C" />
<item android:id="#+id/d" android:title="D" />
<group android:id="#+id/group_1">
<item android:id="#+id/e" android:title="E" />
<item android:id="#+id/f" android:title="F" />
</group>
</menu>
</item>
</menu>
Java:
public boolean onPrepareOptionsMenu (Menu menu) {
menu.setGroupEnabled (R.id.group_1, false); // does not work
menu.setGroupVisible (R.id.group_1, false); // does not work either
return super.onPrepareOptionsMenu (menu);
}
public boolean onCreateOptionsMenu (Menu menu) {
getMenuInflater ().inflate (R.menu.menu, menu);
return true;
}

Thanks to user432209's info, here is the answer:
menu.findItem (R.id.b).getSubMenu ().setGroupVisible (R.id.group_1, false);

I'm not sure if you can use a group like this, but try this (its worth a shot):
MenuItem item = menu.findItem(R.id.group_1);
item.setVisible(true);
item.setEnabled(false);
Edit: Your problem is your menu structure and how you create the menu inside onCreateOptionsMenu due to using parent/child menus.
You create a menu for the parent menu, not the child menu, so that is why the call to setGroupEnabled and setGroupVisible fail. You need to create an object in onCreateOptionsMenu that represents the child in order for that object to be passed into onPrepareOptionsMenu and your code to work.

The above-mentioned solution works well but it should be written within onCreateOptionsMenu, here is the example of it:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
// Inflate the menu
inflater.inflate(R.menu.menu, menu);
// Make the menu item visible and enable it.
MenuItem item = menu.findItem(R.id.group_1);
item.setVisible(true);
item.setEnabled(false);
}

Related

how to replace settings item with imagebutton

I am developing a navigation drawer activity in android studio, in menu folder there is a settings item at the left corner. I want to remove it and add an image button over there in action bar. Can anyone tell me how to add that image button and how to perform click event on that button in main activity.
Check below image for reference -
I hope this helps, add this inside your menu.xml layout -
The showAsAction attribute allows you to define how the action is displayed. For example, the ifRoom attribute defines that the action is only displayed in the action bar if there is sufficient screen space available.
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:showAsAction="always"
android:icon="#drawable/ic_settings"
android:title="Settings">
</item>
</menu>
just try this.
no need to add imagebutton you just need to add
android:showAsAction="always"
and icon as
android:icon="#drawable/Your_icon"
do this in the menu.xml file, in item setting
Navigate to main_menu.xml in your menu folder under res and modify it as
<?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">
<item android:id="#+id/image_button"
android:icon="#android:drawable/ic_menu_search"
android:title="Clickme"
app:showAsAction="always" />
</menu>
In your MainActivity
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
if(item.getItemId == R.id.image_button){
//do action on imagebutton click
return true;
}
return super.onOptionsItemSelected(item);
}

Android: implement search in appbar

In my application I have an app bar like the one in the image below
(source: jayway.com)
On the left of the appbar I have a TextView and on the right a SearchView and a menu.
How can I implement the search action?
I would like the searchview expanding to the entire width of the app bar and hiding the TextView on the left, when search icon is pressed.
Something like Youtube or Gmail apps.
please help me. Thanks.
The item (add it in res/menu/menu.xml):
<menu>
<!-- ... ->
<item android:id="#+id/action_search"
android:title="#string/search_title"
android:icon="#drawable/ic_search"
android:showAsAction="collapseActionView|ifRoom"
android:actionViewClass="android.widget.SearchView" />
</menu>
don't forget to override
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
MenuItem menuItemSearch = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView)
MenuItemCompat.getActionView(menuItemSearch);
return true;
}

how to force 3 dots in actionBar menu?

I have the following menu xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_totalCerdit"
android:showAsAction="always"
android:title="-100"/>
<item
android:id="#+id/actions_saved"
android:icon="#android:drawable/btn_star"
android:showAsAction="always"
android:title="#string/action_settings"/>
<!-- Settings, should always be in the overflow -->
<item
android:id="#+id/action_settings"
android:showAsAction="never"
android:title="#string/action_settings"/>
<item
android:id="#+id/action_full_text"
android:showAsAction="never"
android:title="#string/action_full_list"/>
<item
android:id="#+id/action_saved_text"
android:showAsAction="never"
android:title="#string/action_favorites_list"/>
<item
android:id="#+id/logout"
android:orderInCategory="100"
android:showAsAction="never"
android:title="#string/log_out"/>
</menu>
When deploying on Samsung Note2 The action Bar shows all items that don't have android:showAsAction="never". The ones with that attributes are show when pressing samsung built-in left bottom menu button.
When deploring on Nexus4 I see all items with android:showAsAction="never" as sub menus of "3 dots" item.
how can I make these 3 dots appear and behave in note2 the same as in nexus4?
In Compatibility Definition Document, Google said that Android 4.4+ device SHOULD NOT implement a dedicated physical button for the Menu function. So later device would be fine.
But it seems like NoteII makes a built in menu button for Overflow in Action bar (3 dots menu button). If you really want to do it on NoteII, there is an old hack that I found:
try {
ViewConfiguration config = ViewConfiguration.get(this);
Field menuKeyField = ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey");
if (menuKeyField != null) {
menuKeyField.setAccessible(true);
menuKeyField.setBoolean(config, false);
}
} catch (Exception e) {
// presumably, not relevant
}
Try this:
#Override
public boolean onPrepareOptionsMenu (Menu menu) {
return true;
}
Add this to all your Activity classes. Now you will see the same thing on both the Note 2 and the Nexus 4.
Just implement this method
#Override
public boolean onCreateOptionsMenu(Menu menu) {present.
getMenuInflater().inflate(R.menu.yourfile.xml, menu);
return true;
}

Menu item not added to actionbar

I have looked for a solution to the problem on Stack Overflow and google, but nothing seems to help. To clarify, I am talking about the one with the id "favourite"
Here is my menu 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.rasmase.smbcbrowser.MainActivity" >
<item android:id="#+id/favourite"
android:icon="#drawable/ic_favourite"
android:title="#string/favourite"
app:showAsAction="ifRoom" />
<item
android:id="#+id/go_to_comic"
android:orderInCategory="100"
android:title="#string/Go_to_comic"
app:showAsAction="never"/>
And my onCreateOptionsMenu:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
Anyone else have any idea what is wrong?
EDIT:
Turns out that I am retarded. The Icon was a black star on a black actionbar. Image for reference: http://i.stack.imgur.com/tVWm7.png

Create a menu/options when the settings/menu button is pressed on the physical phone

I have the standard code as it is for a menu:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
And what I want to do is have 2 options:
1 for settings
1 for about us
If you could please help me code this, I would appreciate it. Thanks!
You need to create activity_main.xml in your /res/menu dir. It would look something like this:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/settings"
android:title="Settings"
android:icon="#drawable/settings_icon" />
<item android:id="#+id/about"
android:title="About"
android:icon="#drawable/about_icon" />
</menu>
Note that you should also include icon resources settings_icon.png and about_icon.png in your /res/drawable dir.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.add_account, menu);
return true;
}

Categories