How to parametrize options of a menu on Android? - android

I want to show/hide some options of a menu on an Android application depending of some strings that I will set to true/false on strings.xml file.
As an example, I will have these strings on strings.xml file:
<string name="option1">false</string>
<string name="option2">true</string>
<string name="option3">true</string>
So in this case only option2 and option3 have to be shown.
On the other side, I mean, in code, I tried the following:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
getMenuInflater().inflate(R.menu.main, menu);
if((getResources().getString(R.string.option1)).equals("false")){
menu.getItem(R.id.option1).setVisible(false);
}
return true;
}
But option1 is still being shown on the menu.
What should I do to parametrize these options? I could just remove these options but I would like that in the future if my clients want these options active again, I could do it easily changing false value to true.
EDIT: Here is my menu:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item
android:id="#+id/option1"
android:title="Option 1" />
<item
android:id="#+id/option2"
android:title="Option 2" />
<item
android:id="#+id/option3"
android:title="Option 3" />
....
</group>
</menu>
LAST UPDATE
I have notice that I was getting NullPointerException because the name of the menu was wrong. I have fixed it and now it does not give me any error.
But my problem comes because I have two different layouts that contains two different menus (one in each layout) and I inflate each layout depending of one string that comes on the login.
For example, considering that the string to inflate the first layout is "hello" I display the layouts as follows on onCreate function of my MainActivity:
if("hello".equals(stringForLayout)){
setContentView(R.layout.activity_main);
}else{
setContentView(R.layout.activity_main_secondary);
}
How can I parametrize the options of both menus so if the first one is being shown and I want to hide one option, I would be able to refer to that option of that menu?
Thanks in advance!

If you use Strings (e.g. s1 & s2), you have to compare them like this:
if(s1.equals(s2)) {...}
Btw.: You can also look at Shared Preferences in the android documentation to get a cleaner solution. There you can define boolean variables for the different menu options.

Finally, I found where my problems were.
The problem came because I did not notice that onCreateOptionsMenu and onPrepareOptionsMenu were not used so I deleted them.
Further, as I was using my menu inside a NavigationView I had to use, after inflate the NavigationView on my onCreate function of my MainActivity class, the following statement:
navigationView.getMenu().findItem(R.id.option1).setVisible(false);
There, I did not have any problem to hide my menu item succesfully.

Try this if you want to hide a specific menu item for a specific activity.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuItem mSearch = menu.findItem(R.id.mSearch);
mSearch.setVisible(false);
invalidateOptionsMenu();
}

Related

the setting options in another activity

in my app on the top right of the action bar, I have the setting as three dots (as the usual setting). but when I click on it, it shows a white rectangle that has the word "setting" on it! But I don't want that!! I want just the three dots and when I click it I want it to take me to another activity right away(without showing the option setting on a rectangle).
this my code for the setting xml:
<item
android:id="#+id/action_settings"
android:orderInCategory="0"
android:title="#string/action_settings"
android:textColor="#color/colorBackground"
app:showAsAction="never" />
</menu>
The three dots you're talking about are usually there if you don't have enough room in your action bar and you want some of your menu items to show in a menu instead of directly displaying it to the action bar. The best thing for you is to create another item and set app:showAsAction to always and call a method using onOptionsItemSelected event:
<item
android:id="#+id/your_id"
android:title="your title"
app:showAsAction="always" />
Call it like this:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.your_id:
//code here...
}
return super.onOptionsItemSelected(item);
}
Currently your app has a default menu that has a settings item added. You can create your own menu options.
Create a new menu.xml file that contains 1 item whose image is the three dots.
In your class, inside the onOptionsItemSelected method, call the activity that you want to open.
Hope this helps :)

Dynamic radio options in the Action Bar (Android)

I thought I should ask a new question for this, but for some context I was able to get the label in position thanks to the good people at How to get a label in the Android action bar
.
So now that I've got that, I want the user to be able to tap the Administrator button and then change it to a different mode (probably just "Administrator", "User", "Guest" to start with but there may be more in the future).
How can I get a list of radio boxes to appear when the button in the top right is clicked? Ideally I want to be able to define those various modes dynamically from within the Java class so that if a new type gets added to the database it will be automatically picked up.
If anyone could point me in the right direction I'd appreciate it. I have seen a few examples from Googling, but unfortunately none of them involved the sort of customised drawable I'm using - and none of them had dynamically populated radio options either.
Thanks
You probably want to have a menu which contains your different items.
Do something like this to create the menu and inflate your xml:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.my_menu, menu);
return true;
}
And then for handling the click events:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.admin:
switchToAdminUI();
return true;
case R.id.guest:
switchToGuestUI();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
And your my_menu.xml could look something like this:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/menu_overflow"
android:icon="#drawable/abs_ic_menu_moreoverflow_material"
android:showAsAction="always">
<menu>
<item
android:id="#+id/admin"
android:showAsAction="never"
android:icon="#drawable/ic_admin"
android:title="#string/admin"/>
<item
android:id="#+id/guest"
android:showAsAction="never"
android:icon="#drawable/ic_guest"
android:title="#string/guest"/>
</menu>
</item>
</menu>
You can see the android:showAsAction="always" above, which means that it will always show as an action bar icon, and then you put the sub menu items in there.
Try it out and you can also read more about menus here https://developer.android.com/guide/topics/ui/menus.html

Android ActionbarSherlock show menu button at top

I am trying to implement ActionBarSherlock in my application and I need to show the menu at the top of my application, integrated in action bar. The problem is that it's not working properly. I am using it like this :
public boolean onCreateOptionsMenu(Menu menu) {
menu.add("Refresh")
.setIcon(R.drawable.ic_action_refresh)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
menu.add("Settings")
.setIcon(R.drawable.ic_action_settings)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
return true;
}
So the thing which I want to achieve is to show at the right side of my action bar a refresh icon and a menu icon, which should open the default menus when clicked. I checked ForcedOverflowItem example in ActionbarSherlock Demos, but it's not working as I want. I need to look the same as in Android 2.+ and in Android 4.+.
Any advices / helps / suggestions how can I get this to work?
You can do an xml file line this.
I saw this in some other post but I cannot track it rigth now.
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/more"
android:showAsAction="always"
android:icon="#drawable/abs__ic_menu_moreoverflow_holo_dark">
<menu>
<item
android:id="#+id/remove"
android:showAsAction="withText|never"
android:title="#string/remove">
</item>
</menu>
</item>
</menu>
Then just inflate it like a normal menu.
public boolean onCreateOptionsMenu(Menu menu) {
getSupportMenuInflater().inflate(R.menu.overlow, menu);
return true;
}
From what I read in ActionbarSherlock's documentation you cannot force the menu icon to appear in Android 4.+. When the device has a menu button the menu icon does not appear. I guess the guy who wrote ActionbarSherlock knows the subject matter well ;-)
Please read the dorjeduck's answer. If you want have same experience on all devices you have to add custom menu with his submenus. Here this the code sumple:
SubMenu sub = menu.addSubMenu("More");
sub.setIcon(R.drawable.abs__ic_menu_moreoverflow_holo_dark);
sub.add(0, MENU_SETTINGS, 0, "Settings");

Why the pictures/icon don't show up in the menu?

I followed this tutorial to create a menu
but my menu looks differently:
How can I create a menu with images?
This is my code:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/preferences"
android:icon="#drawable/preferences"
android:title="Preferences" />
<item android:id="#+id/help"
android:title="Help"
android:icon="#drawable/ic_action_search" />
</menu>
#Override
public boolean onCreateOptionsMenu(Menu menu) {
/*menu.add(Menu.NONE, PREF_ID, Menu.NONE, "Preferences")
.setIcon(R.drawable.preferences).setAlphabeticShortcut('e');
return (super.onCreateOptionsMenu(menu));*/
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.activity_ygo_main, menu);
return true;
}
First of all I want to say : Say Goodbye to the Menu Button
Your code have no problem, and it should be showing the icons if the drawables are there in correct folder,Working fine on Android 2.2.
The Menu features says :
1. Context menus: Do not support item shortcuts and item icons.
2. Options menus: The icon menus do not support item check marks and only show the item's condensed title. The expanded menus (only available if six or more menu items are visible, reached via the 'More' item in the icon menu) do not show item icons, and item check marks are discouraged.
3. Sub menus: Do not support item icons, or nested sub menus.
No problem with your code, Problem may be with the API level you are using, but still want to suggest that don't use Menu anymore.
Android no longer requires a dedicated Menu button, some devices don’t have one, and you should migrate away from using it.
If you use some following attribute in manifest file then it's will be show your icon....
<activity android:name=".ui.CategoryActivity"
android:label="#string/app_name"
**android:theme="#android:style/Theme.NoTitleBar"**></activity>
It's work fine for me...:)
**must be enter.

Trouble changing icons on ActionBar

My application calls a web service on startup to see if the user is logged in. If they are logged in it needs to show a logout icon. If they aren't logged in, it needs to show a login icon. If their version of the application doesn't support logging in, it need to continue showing no icon.
How do I get the icon to show when the web service returns? I can't call invalidateOptionsMenu() because I'm using 2.3. Also, if I try to add it in onPrepareOptionsMenu() it shows up on the menu when you press the menu button instead of on the ActionBar.
I've done something similar this way:
In your action bar menu, have both the log in and log out items already there (I would advise putting them in an xml instead of creating it in the code so you can easily assign the items an id). Then just hold a reference to your menu when you create it so you can modify it later.
Menu myActionBarMenu;
/**
* Creates action bar items.
*/
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menuNameHere, menu);
myActionBarMenu = menu;
}
Then, when you find out the user is logged in, set the visibility of the log in item to false like this:
myActionBarMenu.findItem(R.id.logInAction).setVisible(false);
myActionBarMenu.findItem(R.id.logOutAction).setVisible(true);
reverse the visibility in the case the user is logged out.
This would be what your xml menu would look like:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/logInAction"
android:showAsAction="ifRoom"
android:title="Log In"/>
<item
android:id="#+id/logOutAction"
android:showAsAction="ifRoom"
android:title="Log Out"/>
</menu>
I use this code to add menu items to the ActionBar:
/*************************************/
/* Create the actionbar options menu */
/*************************************/
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
menu.add("Settings")
.setIcon(R.drawable.ic_menu_moreoverflow_normal_holo_light)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
return true;
}
You should be able to add an if statement in there and change the icon accordingley

Categories

Resources