I tried to hide item and group with below code it is not working, can someone please help me with this code. I tried for item and group both not working.
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.navigation_menu, menu);
MenuItem shareItem = menu.findItem(R.id.login);
// show the button when some condition is true
if (true) {
shareItem.setVisible(false);
}
menu.setGroupVisible(R.id.common_menu, false);
return true;
}
Try this following code:
call this function whereever you want hideItem()
private void hideItem()
{
Menu nav_Menu = navigationView.getMenu();
nav_Menu.findItem(R.id.nav_adduser).setVisible(false);
}
Related
I don't know how to declare MenuItem object and i call on every method the MenuItem so my code look like:
private Menu menu;
private void updateMenuIconForWifi(){
MenuItem menuB = menu.findItem(R.id.action_bluetooth);
MenuItem menuItemW = menu.findItem(R.id.action_wifi);
menuB.setVisible(false);
menuItemW.setIcon(R.drawable.ic_network_wifi_black_24dp);
}
private void changeIconToDefaultWifi(){
MenuItem menuB = menu.findItem(R.id.action_bluetooth);
MenuItem menuItemW = menu.findItem(R.id.action_wifi);
menuB.setVisible(true);
menuItemW.setIcon(R.drawable.ic_wifi_white_24dp);
}
private void updateMenuIconForBluetooth() {
MenuItem menuB = menu.findItem(R.id.action_bluetooth);
MenuItem menuItemW = menu.findItem(R.id.action_wifi);
menuItemW.setVisible(false);
menuB.setIcon(R.drawable.ic_bluetooth_connected_black_24dp);
}
private void changeIconToDefaultForBluetooth() {
MenuItem menuB = menu.findItem(R.id.action_bluetooth);
MenuItem menuItemW = menu.findItem(R.id.action_wifi);
menuB.setIcon(R.drawable.ic_bluetooth_white_24dp);
menuItemW.setVisible(true);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
this.menu = menu;
menuInflater.inflate(R.menu.menu_settings, menu);
for (int j = 0; j < menu.size(); j++) {
MenuItem item = menu.getItem(j);
item.setShowAsActionFlags(MenuItem.SHOW_AS_ACTION_ALWAYS);
}
return true;
}
but this look so ugly.
How to declare the MenuItems once? when i try adding some value like
private MenuItem menuB, menuItemW;
and then call this like in onCreate
menuB = menu.findItem(R.id.action_bluetooth);
menuItemW = menu.findItem(R.id.action_wifi);
i got a error
First read about onPrepareOptionsMenu(Menu menu)
Each time the user presses the Menu on their Android device while inside one of your activities, the onPrepareOptionsMenu method is called. The first time the menu is shown (i.e. only once), the onCreateOptionsMenu method is called.
Basically, the onPrepareOptionsMenu method is where you should make any changes such as enabling/disabling certain menu items, or changing the menu item text depending on the circumstances.
So do this (Don't use onCreateOptionsMenu(Menu menu) )
//Dynamically create context Menu
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
menu.clear(); //Clear view of previous menu
MenuInflater inflater = getMenuInflater();
if(condition_true)
inflater.inflate(R.menu.menu_one, menu);
else
inflater.inflate(R.menu.menu_two, menu);
return super.onPrepareOptionsMenu(menu);
}
you can save your menuItem once during onCreateOptionsMenu, and access it subsequently.
private MenuItem menuB, menuItemW;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_settings, menu);
menuB = menu.findItem(R.id.action_bluetooth);
menuItemW = menu.findItem(R.id.action_wifi);
//the rest of your codes
return true;
}
So, that´s what I wanna know. How can I set the visibility of the menu programatically in Android?? This is how I have my menu:
public boolean onCreateOptionsMenu(Menu menu){
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
public boolean onOptionsItemSelected (MenuItem item){
switch (item.getItemId()){
case R.id.menuregistrar:
break;
case R.id.menusalir:
break;
}
return true;
}
But this code is not on the onCreate, so I don´t know how to set one item visible or invisible programmatically (in my case, I want the "menuregistrar" to be invisible once I have registered my application and forever.
Put this method in your Activity
public boolean onPrepareOptionsMenu(Menu menu)
{
MenuItem register = menu.findItem(R.id.menuregistrar);
if(userRegistered)
{
register.setVisible(false);
}
else
{
register.setVisible(true);
}
return true;
}
in shorter version you could write:
MenuItem register = menu.findItem(R.id.menuregistrar);
register.setVisible(!userRegistered); //userRegistered is boolean, pointing if the user has registered or not.
return true;
I would simplify Adil's solution even further with the following:
public boolean onPrepareOptionsMenu(Menu menu)
{
MenuItem registrar = menu.findItem(R.id.menuregistrar);
registrar.setVisible(!isRegistered);
return true;
}
Simply do one thing get the id of the item of menu from this line:
Menu menu =navigationView.getMenu();
MenuItem nav_dashboard = menu.findItem(R.id.nav_dashboard);
and than make it visible it accourding to you by this line:
nav_dashboard.setVisible(true/false);
Menu Object has a property to set the visibility of a menu's item using setVisible(boolean)//
Example
private Menu menu_change_language;
...
...
#Override
public boolean onCreateOptionsMenu(Menu menu) {
...
...
menu_change_language = menu;
...
...
return super.onCreateOptionsMenu(menu);
}
use code below for hiding Menu Item:
if(menu_change_language != null){
menu_change_language.findItem(R.id.menu_change_language)
.setVisible(false);
}
Use public boolean onPrepareOptionsMenu (Menu menu) it is called everytime you press the menu button and do your stuff there. or use your oncreateoptionsmenu() in different activities to inflate different menus - this one is called only once.
Cheers
If you want to change the visibility inside the onOptionsItemSelected whenever you click the menu
#Override
public boolean onOptionsItemSelected(MenuItem item) {
item.setVisible(true);
return true;
}
OR
for item in the menu that you didn't click on
private Menu globalMenuItem;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu."menu Xml Name", menu);
globalMenuItem = menu;
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
globalMenuItem.findItem(R.id."id of the menu item").setVisible(true);
return true;
}
I have a menu which is inflated from main_menu.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/act_sync"
android:showAsAction="always"
android:actionLayout="#layout/sync_action"
android:icon="#android:drawable/ic_popup_sync" />
</menu>
and here is the code in the activity:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
MyMessageHandler.debug("menu item selected");
switch(item.getItemId()){
case R.id.act_sync:
sync();
return true;
}
return super.onOptionsItemSelected(item);
}
But onOptionsItemSelected is not called when I touch the menu item. When I remove the actionLayout attribute of the menu item, it works fine. How can I fix this? Thanks.
you should use below snippet ( Just for reference )
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_menu, menu);
final Menu m = menu;
final MenuItem item = menu.findItem(R.id.ActionConnection);
item.getActionView().setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
sync();
}
});
return true;
}
In addition to the answer provided by Vipul Shah.
Make sure you disable the clickable option of all items in your action layout:
android:clickable="false"
Otherwise, it may steal the click so that you still cant receive onClick call back.
So, that´s what I wanna know. How can I set the visibility of the menu programatically in Android?? This is how I have my menu:
public boolean onCreateOptionsMenu(Menu menu){
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
public boolean onOptionsItemSelected (MenuItem item){
switch (item.getItemId()){
case R.id.menuregistrar:
break;
case R.id.menusalir:
break;
}
return true;
}
But this code is not on the onCreate, so I don´t know how to set one item visible or invisible programmatically (in my case, I want the "menuregistrar" to be invisible once I have registered my application and forever.
Put this method in your Activity
public boolean onPrepareOptionsMenu(Menu menu)
{
MenuItem register = menu.findItem(R.id.menuregistrar);
if(userRegistered)
{
register.setVisible(false);
}
else
{
register.setVisible(true);
}
return true;
}
in shorter version you could write:
MenuItem register = menu.findItem(R.id.menuregistrar);
register.setVisible(!userRegistered); //userRegistered is boolean, pointing if the user has registered or not.
return true;
I would simplify Adil's solution even further with the following:
public boolean onPrepareOptionsMenu(Menu menu)
{
MenuItem registrar = menu.findItem(R.id.menuregistrar);
registrar.setVisible(!isRegistered);
return true;
}
Simply do one thing get the id of the item of menu from this line:
Menu menu =navigationView.getMenu();
MenuItem nav_dashboard = menu.findItem(R.id.nav_dashboard);
and than make it visible it accourding to you by this line:
nav_dashboard.setVisible(true/false);
Menu Object has a property to set the visibility of a menu's item using setVisible(boolean)//
Example
private Menu menu_change_language;
...
...
#Override
public boolean onCreateOptionsMenu(Menu menu) {
...
...
menu_change_language = menu;
...
...
return super.onCreateOptionsMenu(menu);
}
use code below for hiding Menu Item:
if(menu_change_language != null){
menu_change_language.findItem(R.id.menu_change_language)
.setVisible(false);
}
Use public boolean onPrepareOptionsMenu (Menu menu) it is called everytime you press the menu button and do your stuff there. or use your oncreateoptionsmenu() in different activities to inflate different menus - this one is called only once.
Cheers
If you want to change the visibility inside the onOptionsItemSelected whenever you click the menu
#Override
public boolean onOptionsItemSelected(MenuItem item) {
item.setVisible(true);
return true;
}
OR
for item in the menu that you didn't click on
private Menu globalMenuItem;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu."menu Xml Name", menu);
globalMenuItem = menu;
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
globalMenuItem.findItem(R.id."id of the menu item").setVisible(true);
return true;
}
My XML menu definition sets the item R.id.menu_refresh's enabled state to false. When the app runs the menu item is greyed and disabled. Why is this code in the app not enabling the item?
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
MenuItem refresh = menu.getItem(R.id.menu_refresh);
refresh.setEnabled(true);
return true;
}
What am I missing?
Try menu.findItem() instead of getItem(). getItem() takes an index from [0, size) while findItem() takes an id.
this is what I do in my activity for the menu handling ...
//Android Activity Lifecycle Method
// This is only called once, the first time the options menu is displayed.
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.mainmenu, menu);
return true;
}
//Android Activity Lifecycle Method
// Called when a panel's menu is opened by the user.
#Override
public boolean onMenuOpened(int featureId, Menu menu)
{
MenuItem mnuLogOut = menu.findItem(R.id.main_menu_log_out_id);
MenuItem mnuLogIn = menu.findItem(R.id.main_menu_log_in_id);
MenuItem mnuOptions = menu.findItem(R.id.main_menu_options_id);
MenuItem mnuProfile = menu.findItem(R.id.main_menu_profile_id);
//set the menu options depending on login status
if (mBoolLoggedIn == true)
{
//show the log out option
mnuLogOut.setVisible(true);
mnuLogIn.setVisible(false);
//show the options selection
mnuOptions.setVisible(true);
//show the edit profile selection
mnuProfile.setVisible(true);
}
else
{
//show the log in option
mnuLogOut.setVisible(false);
mnuLogIn.setVisible(true);
//hide the options selection
mnuOptions.setVisible(false);
//hide the edit profile selection
mnuProfile.setVisible(false);
}
return true;
}
//Android Activity Lifecycle Method
// called whenever an item in your options menu is selected
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
// Handle item selection
switch (item.getItemId())
{
case R.id.main_menu_log_in_id:
{
ShowLoginUI();
return true;
}
case R.id.main_menu_log_out_id:
{
ShowGoodbyeUI();
return true;
}
case R.id.main_menu_options_id:
{
ShowOptionsUI();
return true;
}
case R.id.main_menu_profile_id:
{
ShowProfileUI();
return true;
}
default:
return super.onOptionsItemSelected(item);
}
}
I like this approach because it makes the code nice and modular
Use menu.findItem() instead of getItem().
because findItem is used to find item by id.