3

I'm developing an application and I need to disable the virtual buttons when the application started to run since there are buttons from the application.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.activity_1st_main);
    
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
}

@SuppressWarnings({ "unchecked", "rawtypes" })
private final List hijackKeys = new ArrayList(Arrays.asList(
        KeyEvent.KEYCODE_VOLUME_DOWN, KeyEvent.KEYCODE_VOLUME_UP,
        KeyEvent.KEYCODE_BACK, KeyEvent.KEYCODE_HOME));

@Override
public boolean dispatchKeyEvent(KeyEvent event) {
    if (hijackKeys.contains(event.getKeyCode())) {
        return true;
    } else {
        return super.dispatchKeyEvent(event);
    }
}

enter image description here

Nimantha
  • 6,405
  • 6
  • 28
  • 69
androidBoomer
  • 3,357
  • 6
  • 32
  • 42

3 Answers3

3

This is the best solution for this.

public class BaseActivity extends Activity {
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);

            Log.d("Focus debug", "Focus changed !");

        if(!hasFocus) {
            Log.d("Focus debug", "Lost focus !");

            Intent closeDialog = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
            sendBroadcast(closeDialog);
        }
    }
} // all credit goes here: http://www.juliencavandoli.com/how-to-disable-recent-apps-dialog-on-long-press-home-button/
androidBoomer
  • 3,357
  • 6
  • 32
  • 42
2

Override Back Button with below code

@Override
public void onBackPressed() {
    // TODO Auto-generated method stub
    //super.onBackPressed();  // nothing to do here
}

And now for Menu Button try like this

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
//  getMenuInflater().inflate(R.menu.main, menu);   // comment this line to disable menu button
    return true;
}

And best approach to disable menu button just remove onCreateOptionsMenu method from your activity class...

Chirag Ghori
  • 4,231
  • 2
  • 20
  • 35
0

Add this in your manifest.xml for your main activity:

<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.LAUNCHER" />
<category android:name="android.intent.category.HOME" />

source found from Stack Overflow.

Try this way

@Override
public void onAttachedToWindow() {
    //only desable home button
    this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
    super.onAttachedToWindow();
}
Nimantha
  • 6,405
  • 6
  • 28
  • 69
Dinithe Pieris
  • 1,822
  • 3
  • 32
  • 44