I'm trying to make the actionbar menu (onCreateOptionsMenu) open ONLY on a long-click. How would I achieve this? At the moment I have it working for just a single short press/click using the following code:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
// TODO: Only onlongclick should the menu be revealed
getMenuInflater().inflate(R.menu.my_menu_id, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_home:
open_home();
return true;
case R.id.menu_how_to:
open_how_to();
return true;
case R.id.menu_rate:
open_rate();
return true;
case R.id.menu_about:
open_about();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
I'd like the menu to ONLY open for a long click (sort of like a hidden/trick feature). I've looked into onLongClickListener but can't get it to work. Thanks for your help! Really appreciate it.
I ended up figuring this one out myself. There's 2 ways of doing it. Either a context menu or a alert dialog menu. The alert dialog menu allow icons whereas the context menu does not.
See my answer here:
Open Actionbar Menu on Longclick - Android
Related
I am making a app after Login my Login submenu item should be disable until the person is logout, after logout, Login item should be enable again!!!
My main activity contains a Action bar is it possible to enable or
disable my code in Login Activity?
i have tried a code but didn't achieve any response on the basis of getstatus item should be disable !!
Login Activity Code
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_main, menu);
if (getstatus==1) {
removeSomeItems(menu);
Toast.makeText(getApplication(),"logout called",Toast.LENGTH_SHORT).show();
return true;
}
else
{
return false;
}
}
protected void removeSomeItems(Menu menu){
MenuItem Overflow = menu.findItem(R.id.action_login);
Overflow.setVisible(false); // I want this item to be available eventually
menu.removeItem(R.id.action_logout); // I want this item to be unavailable for this activity
}
Can any one suggest me proper way to achieve this scenario ?
PRINT SCREEN
I'm willing to block to the user the possibility to open the menu using menu key.
The menu must be opened only from the menu button in my application.
So I deleted this part of code:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
Now if I press the key button it happens nothing and that's what I wanted.
The problem is that if I press my menu button in app, the menu doesn't show up anymore...
So what do?
I thought to add the
getMenuInflater().inflate(R.menu.main, menu);
in my listener on the menu button but here is the error and I don't understand what to do... any advice?
Thank you!
Do it with Style templates. If SDK < Jelly Bean use style with ActionBar.
You have to add the onCreateOptionsMenu part. But override the onKeyDown event to disable the menu key. Like this:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
return keyCode == KeyEvent.KEYCODE_MENU || super.onKeyDown(keyCode, event);
}
I am not sure but this should work
Define this variable in your main class (outside all subclasses) -
public class ... extends ... {
boolean menu = false;
...
}
And then whenever you click on menu button on your print screen, do the following -
#Override
public void onClick(View v){
menu = true;
openOptionMenu();
}
And in your onCreateOptionMenu, do this -
#Override
public boolean onCreateOptionsMenu(Menu menu) {
if(menu){
getMenuInflater().inflate(R.menu.main, menu);
}
menu = false;
return true;
}
I'm using sherlock action bar.
I have 2 items on the action bar. When the item is chosen (active), I want to change the icon's image.
This is my code on Java
#Override
public boolean onPrepareOptionsMenu (Menu menu){
MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.menutes, menu);
todaySched=menu.findItem(R.id.todaySched);
if(todaySched.isEnabled()){
todaySched.setIcon(R.drawable.calendarselected);
}
return true;
}
but when I do this the icon become double, and the icon won't change neither.
Can someone help?
Use the on onOptionsItemSelected method
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.todaySched:
// put your code here to change the icon
return true;
default:
return super.onOptionsItemSelected(item);
}
}
you may need to include the correct namespace for the ActionBar Sherlock library to ensure it Overrides the correct menu item. So the start of the method will look like this:
#Override
public boolean onOptionsItemSelected(com.actionbarsherlock.view.MenuItem item)
When I touch this "Edit" button I want it to change to "Done". Is there a way to do this?
I tried this but it only works when opening the menu on the right side:
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
// change the text here.
return super.onPrepareOptionsMenu(menu);
}
I assume that, at some point, you inflate the menu, yes?
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.some_menu_layout, menu);
return true;
}
So you can go into your menu (in this case, res/menu/some_menu_layout.xml), and find the android:id of the menu item you want to find. You should then be able to use menu.findItem(theId).setTitle("Done");. (You may have to save your Menu variable as a local member to do this.)
I'm trying to get my options menu to show up in the new Android 3.0 system bar. I can get this behavior to work with the Notepadv3 tutorial. However, when implementing nearly the identical functions, the menu shows in the above action bar, but not the system bar. Any ideas?
Attached is the appropriate code for my application:
public class AACUser extends Activity {
private static final int ADMIN_ID = Menu.FIRST;
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
menu.add(0, ADMIN_ID, 0, R.string.menu_admin);
return true;
}
#Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
switch(item.getItemId()) {
case ADMIN_ID:
enterAdminMode();
return true;
default:
return super.onMenuItemSelected(featureId, item);
}
}
}
I've also tried implementing these menu functions as recomended by the creating menu documentation:
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.user_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.menu_adminMode:
enterAdminMode();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
but no difference: it still displays the option in the overflow of the action bar, but not the system bar.
As of Android 3.0, the options menu is not required (if not deprecated). If you target API 11 (Android 3.0) or later, it is assumed that your app will work without an options menu, so the system bar will not display one. The options menu in the system bar is only there for backwards compatibility with old applications. New applications should have another affordance for accessing options, such as that provided by the action bar.
I think that the menu button in the system bar is for backward compatibility purposes only. So if you want to design your app for Honeycomb then the menu should be and will be in the action bar.