2

I want to disable menu button. I read a lot of articles and nothing works.
I can't even detect menu button pressed event. Overriding methods like onKeyDown(), onKeyUp(), onPrepareOptionsMenu(), onCreateOptionsMenu() didn't worked. I am really desperate here, please help.

public class MainActivity extends Activity{
TranslateAnimation animateTopLayer;
Button btnOk;
ImageView ivLockMechanism;
ViewGroup rlTopLayer;
FramesSequenceAnimation anim;

@Override
public void onAttachedToWindow() {
    getWindow().addFlags(WindowManager.LayoutParams. FLAG_DISMISS_KEYGUARD);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Intent lockServiceIntent = new Intent(getApplicationContext(),LockService.class);
    startService(lockServiceIntent);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.test);
    rlTopLayer = (ViewGroup) findViewById(R.id.rlTopLayer);
    ivLockMechanism = (ImageView) findViewById(R.id.ivLockMechanism);
    btnOk = (Button) findViewById(R.id.btnOk);
    btnOk.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            rlTopLayer.startAnimation(animateTopLayer);
        }
    });
    Bitmap b = BitmapFactory.decodeResource(getResources(), R.drawable.pg_0);
    ivLockMechanism.setImageBitmap(b);
    anim = AnimationsContainer.getInstance().unlockMechanismAnimation(ivLockMechanism);
    animateTopLayer = new TranslateAnimation(0, 500,0, 0);
    animateTopLayer.setDuration(1000);
    animateTopLayer.setFillAfter(true);
    animateTopLayer.setAnimationListener(new AnimationListener() {

        @Override
        public void onAnimationStart(Animation animation) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onAnimationRepeat(Animation animation) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onAnimationEnd(Animation animation) {
            anim.start();
        }
    });
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event)  {
    if (  Integer.valueOf(android.os.Build.VERSION.SDK) < 7 
            && keyCode == KeyEvent.KEYCODE_BACK
            && event.getRepeatCount() == 0) {
        onBackPressed();
    }

    if (keyCode == KeyEvent.KEYCODE_MENU){
        return false;
    } 

    return super.onKeyDown(keyCode, event);
}

@Override
public void onBackPressed() {
    return;
}

@Override
protected void onPause() {
    super.onPause();
    finish();
}

}

prowebphoneapp
  • 87
  • 1
  • 11

1 Answers1

6

This works for me:

public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_MENU) {
        return true;
    }
    return super.onKeyDown(keyCode, event);
}

Return true to prevent this event from being propagated further, or false to indicate that you have not handled this event and it should continue to be propagated.

@Override
 public boolean onPrepareOptionsMenu (Menu menu) {
     return false;
 }
ObAt
  • 2,337
  • 3
  • 24
  • 44
  • Won't even enter this method when i press menu button. – prowebphoneapp Jan 24 '13 at 13:24
  • Try to debug your app, is the method called? If not something is wrong with your code – ObAt Jan 24 '13 at 14:00
  • Does your class extends Activity? – ObAt Jan 24 '13 at 14:20
  • Of course, app is 90 % finished, just need to disable menu btn. – prowebphoneapp Jan 24 '13 at 14:35
  • Ok, method is called on menu long pressed, but when i just touch menu button (it opens recent apps) it doesn't. Can you help me detect this event? – prowebphoneapp Jan 25 '13 at 09:29
  • That's weird, because Android uses another button to lauch recent apps. It's not possible to detect this key (see: http://stackoverflow.com/questions/12478826/how-to-detect-recent-apps-system-button-clicks-honeycomb) – ObAt Jan 25 '13 at 09:52
  • I talking about exactly that button whole time, and it can be disabled, there is apps that did that. – prowebphoneapp Jan 25 '13 at 09:55
  • I found this, it's the only working solution: http://stackoverflow.com/questions/11882581/android-is-it-possible-to-disable-the-long-click-of-home-button-to-avoid-the-t/12652490#12652490. – ObAt Jan 25 '13 at 11:36
  • This was the solution for my problem. However, in my case I had a TabMenu containing a few Activities. It took me a while to try this code in ALL views, and then it did the trick! – Teo Inke Jun 06 '14 at 06:36