Android: Dynamically change location of Options Menu

1k views Asked by At

I have a drawing activity that creates a custom 'canvas' View that occupies the entire screen, allowing the user to draw on it. I have a requirement to lock the Activity to the portrait orientation, as the drawing app is a sketchpad replacement, and the user would have the need to rotate the 'pad' to draw without the pad flipping around on them. This of course means that the Options Menu is locked to that orientation as well and depending on the user, the menu could be popping out of the top, bottom or sides. I'd like to have the Options Menu always be located at the bottom relative to the user... some options I am considering and would appreciate any suggestions or info:

  1. Allow orientation changes, so the menu moves as needed, then use the SensorManager to listen for the orientation, and manually rotate the canvas View back to the 'correct' orientation (portrait). This would be ugly, as there could be a delay in the rotation of the canvas, and it introduces some potentially complex calculations, etc due to the X/Y being swapped.
  2. Restrict orientation changes, then use the SensorManager to listen for the orientation, and manually rotate the Options Menu back to the bottom. No idea if this is possible.
  3. Lock orientation, and create a custom View that intercepts the menu button and displays a 'fake' Options Menu that I need to manage, etc. Really really don't want to have to replace built-in OS functionality, could be a nightmare to maitain for different versions of OS in future, etc.

Anyone have any ideas on how to handle this? I'd love if #2 was possible, but can't find anything related to that.

Thanks,

Paul

EDIT:

Have confirmed that #2 is not possible, so I guess my options are #1, #3, unless anyone else has any ideas?

1

There are 1 answers

0
Unpossible On BEST ANSWER

My solution is as follows:

  • Allow normal orientation changes in the Android Manifest for the Activity.
  • In the custom 'canvas' view constructor, fire up a tracking field, mDisplay, that gets the display information via:

Code:

mDisplay = ((WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
  • In onDraw(), check to see if the display rotation is something other than 0, and rotate the canvas back to the 0 rotation value:

Code:

if (mDisplay.getRotation() > 0) {
    canvas.save(Canvas.HAS_ALPHA_LAYER_SAVE_FLAG);
    canvas.rotate(-mDisplay.getRotation(), px, py);
    ...
    do stuff
    ...
    canvas.restore();
}

Not sure that it's the best solution, but it seems to work.