Hi,

I know how to detect clicks and touches on the views, and i know how to make menus appear when they hit the menu key. My phone also has a little ball like button that you can press down on. How would an activity capture this event, say if you wanted to use it for confirmation of something, like a move. they touch the screen to indicate a move but i want a menu option of confirm move and if they chose it they would touch the screen for moves but click that ball down on phone to confirm and I'd send the move.

is this an on click listener? if so what event is it?
what is that button called? i cant google it because i google button and all i get is pages on the android button that can be added with code to pages.

Also whats the back or dismiss button called? How could i tell if they backed out of the program? If so i want to close it perhaps. better than it never closing like now unless they go to task killer app :)

Mike

Dani AI

Generated

Short answer: the physical “ball” and the Back/Menu buttons arrive as key events. Capture them with the KeyEvent APIs (Activity or View callbacks) and handle the back button via the activity back handler. was pointing the right way with the UI-events docs; use the KeyEvent constants to detect which physical key was pressed and choose whether the focused view or the Activity should handle it.

Example (Activity-level): override onKeyDown or dispatchKeyEvent to catch the confirm press. Key codes to check: KeyEvent.KEYCODE_DPAD_CENTER, KeyEvent.KEYCODE_ENTER, KeyEvent.KEYCODE_MENU, KeyEvent.KEYCODE_BACK. Remember: key events go first to the currently focused view — if that view consumes the event the Activity won’t see it. Use dispatchKeyEvent to intercept earlier if needed.

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER || keyCode == KeyEvent.KEYCODE_ENTER) {
        // handle the "ball"/center press: confirm pending move
        confirmMove();
        return true;
    }
    return super.onKeyDown(keyCode, event);
}

Handle Back and lifecycle: override onBackPressed() (preferred) to intercept a back/dismiss and call finish() or cancel the pending action, and use onPause()/onStop() to save or clear state when the activity leaves the foreground. Do not rely solely on a custom exit — let the activity lifecycle run normally. For details on key constants and Activity key hooks see the official docs: KeyEvent reference and [Activity.onBackPressed](https://developer.android.com/reference/android/app/Activity#onBackPressed()).

Recommended Answers

All 2 Replies

thanks, read up on those pages.

Mike

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.