Please help me understand. I read somewhere saying that if I set up android:targetSdkVersion="14.
These 3-dots will be gone.
Why these 3-dots still show up on mine?
They don't show up until I come back from another activity. I pressed the menu key after I came back from another activity, they showed up....
I am using actionbar menus so I don't need these ugly 3-dots on my app.
How can I remove them completely?
Thanks
Manifest:
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/MyAppActionBarTheme" >
MainActivity:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the actionbar menu; this adds items to the action bar.
getMenuInflater().inflate(R.menu.actionbar_menu, menu);
return super.onCreateOptionsMenu(menu);
}
Add the following function to your activity:
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
MenuItem settingsItem = menu.findItem(R.id.action_settings);
settingsItem.setVisible(false);
return false;
}
Edit:
Sorry try with return false instead of return true!
in menu.xml u can add android:showAsAction="" with these values never,ifRoom,always,withText,collapseActionView you can use them in | combinations.
in you case you can use android:showAsAction="always"
If you go into you ~res/menu/main.xml there should be some code within the tag
<item>
//setting button code is here
</item>
Just delete all that. Then go into your main activity and delete the corresponding code which should be in onOptionsItemSelected().
C'est voila.
I just thought i should share the easiest process of removing the three dots...
go to the menu folder
go to the second dropdown .xml
view the design on the right pane after clicking on the layout you will see a checkbox which says visible check and uncheck it to make it invisible.
then the 3dots is gone
Related
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();
}
I need help figuring the right way to control the actionbar.
At the onCreate function i added:
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setCustomView(R.layout.actionbar);
And added actionbar.xml to the layout folder with 3 items in it: TextView (app name) and 2 ImageViews.
It works, i mean i can see this new actionbar, but i'm pretty sure i'm doing it the wrong way, in tutorials im reading they're talking about adding those items to a menu.xml file which i couldn't find, and using onOptionsItemSelected function to set the behaviour.
As I'm a begginer, would appreciate an explanation of what it is that i'm doing, why it's wrong and how to do it correctly with the menu.xml
Ok, first you are right, Android have an out of the box solution to put "Action Button" and Title on the Action Bar
1.Title
When you create an Activity, it will add some code into your AndroidManifest.xml, here is the example:
<activity
android:name="AddAddressActivity"
android:label="#string/title_activity_add_address" >
</activity>
so you change your title here by changing android:label, the best practice is you need to put all your string asset under res/values/string.xml like this
<string name="title_activity_add_address">Add New Address</string>
Action Button
First add this override method to your 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.address_list, menu);
return true;
}
Later, you create address_list.xml under res/menu folder, and inside it you can put the list of your Action Button there
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.vi8e.giant.AddressListActivity">
<item
android:id="#+id/action_add"
android:orderInCategory="100"
android:showAsAction="always"
android:icon="#drawable/ic_action_new"
android:title="#string/action_save"/>
Android:title is to put your menu title, this title will show up when you long press the menu
Android:icon here is the image that you want to show for the menu, put the image under res/drawable
Edit: forgot to mention about how to trigger your menu
you can put onOptionsItemSelected method on your Activity, here is the example
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_add:
//do something
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
note: Action Bar has a limited ammount of space, so if you have a lot of menu, it will be collapse into "three dots" icon on the top-right corner
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");
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
I'm using the standard onCreateOptionsMenu, but on my Nexus w/ICS, when I tap the vertical menu button in the upper right corner, the dropdown context menu is 3/4 off the screen. Basically, I only see the first 3-4 letters in each menu items text.
Any ideas? Maybe I didn't get that memo!! I dont want to setup an ActionBar. I'd think by default, this should work fine?!?
Again, its pretty much standard menu code for < 3.0 SDK.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
if (!isPro()) {
// add menu for ad-free Banner (launches Market)
inflater.inflate(R.menu.menu_pro, menu);
}
inflater.inflate(R.menu.menu, menu);
return true;
}
Using standard menu XML...
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/help"
android:icon="#drawable/ic_menu_help"
android:title="#string/menu_help"/>
<item
android:id="#+id/email"
android:icon="#drawable/email"
android:title="#string/menu_email"/>
I cant really say why its cutting off on the top, but I added this to my manifest.xml.
android:theme="#android:style/Theme.Black"
This basically reverts the style back to 2.x menus and places the vertical "dots" on the bottom right.
I actually prefer this over the default compatibility because I still have my icon menus as opposed to plain text-only dropdown menus.
I'd still like to know why the "default" conversion of my menu is screwed up though if anyone can chime in I'll award the solution to that person! (=
You can try to add this to you manifest:
supports-screens android:anyDensity="true"