android eclipse menu enabled/disabled - android

I create a menu in an activity with the following code:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu1, menu);
return true;
}
But so, I can open and close the menu any time.
I want that the menu can be opened if a boolean is true,
if the boolean is false, the user should not be able
to open the menu...

just put a if statement in your code
#Override
public boolean onCreateOptionsMenu(Menu menu) {
if(yourbooleanvariable)
getMenuInflater().inflate(R.menu.menu1, menu);
return true;
}
If you want to preserve the value of boolean vairable use sharedpreferences here is the link
How to use SharedPreferences in Android to store, fetch and edit values

I guess you can just add an ifstatement to the function:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
bool x = true;
if(x){
getMenuInflater().inflate(R.menu.menu1, menu);
return true;
} else {
return false;
}
}

Related

Android - remove three dots in SearchView

How can I remove the three dots to the right of my SearchView ?
set false in onPrepareOptionsMenu
#Override
public boolean onPrepareOptionsMenu (Menu menu) {
return false;
}
Maybe this helps you
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_activity, menu);
MenuItem searchMI = (MenuItem) menu.findItem(R.id.menu_search);
searchMI.setOnActionExpandListener(new OnActionExpandListener(){
#Override
public boolean onMenuItemActionCollapse(MenuItem menuItem) {
// Hide menu icon
return true;
}
#Override
public boolean onMenuItemActionExpand(MenuItem menuItem) {
// Show menu icon
return true;
}
});
return true;
}
You can show or hide icons like this:
menu.findItem(R.id.yourActionId).setVisible(true);
menu.findItem(R.id.yourActionId).setVisible(false);
You need to hide all others menu items one by one...
#Override
public boolean onMenuItemActionExpand (MenuItem item){
menu.getItem(MENU_MAIN_REFRESH).setVisible(false);
menu.getItem(MENU_MAIN_SETTINGS).setVisible(false);
menu.getItem(MENU_MAIN_EXIT).setVisible(false);
return true;
}

Changing a Toolbar appearance dynamically [duplicate]

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

Options Menu not displayed in android

I have written one application. I want to show the settings option on the login screen of an application. Now i have an activity LoginActivity as shown below
class LoginActivity extends BaseLoginActivity
{
showLoginDialog();
/* some code here*/
#Override
public boolean onCreateOptionsMenu(Menu menu) {
populateMenu(menu);
return super.onCreateOptionsMenu(menu);
}
protected void populateMenu(Menu menu) {
menu.add(Menu.NONE, SETTINGS, Menu.NONE, "Settings").setIcon(
android.R.drawable.ic_menu_manage);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case SETTINGS:
startSettingActiviy();
return (true);
}
return (super.onOptionsItemSelected(item));
}
public void startSettingActiviy() {
Intent i = new Intent(this, SettingsActivity.class);
startActivity(i);
}
/* some code here*/
}
when i press on the menu option i cannot see the menu. I tried to debug the code, it is not even reaching the oncreateOptionsMenu function. Please let me know what is missing here.
try this
#Override
public boolean onCreateOptionsMenu(Menu menu) {
populateMenu(menu);
return true;
}
and this too
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case SETTINGS:
startSettingActiviy();
return (true);
}
return true;
}
hope this will help you:)
Try out this way:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
populateMenu(menu);
return true;
}
Give it a try with onPrepareOptionsMenu(Menu menu) instead
Also,
Minimum SDK version could be the cause. If you reduce it to 13-, you should probably see the menu show up again. Good article on this subject: POST

Set visibility in Menu programmatically android

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

How to implement a onMenuItemClickListener in android

How do I implement an onMenuItemClickListener?
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
getMenuInflater().inflate(R.menu.gameoptions, menu);
menu.findItem(R.id.menu_item).setIntent(new Intent(this, QMenuActivity.class));
menu.findItem(R.id.back_item).setOnMenuItemClickListener;
return true;
}
I want the back_item once clicked on to call a function within the page, how do I do it?
I'm assuming you want to access a non-static function in your activity. Without too much information from you, try something like what's below:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuItem item = menu.findItem(R.id.back_item);
item.setOnMenuItemClickListener(new OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
YourActivity.this.someFunctionInYourActivity();
return true;
}
});
return true;
}
item.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener()
change the code to include MenuItem after new.

Categories

Resources