SurfaceView detaining focus

by djauto23 » Wed, 08 Jul 2009 01:02:01 GMT

Sponsored Links
 I'm making a game which uses a SurfaceView to display the main
graphics. I want to use the button labelled "MENU" in the emulator,
and I have implemented onCreateOptionsMenu(Menu menu) in the

Trouble is, the MENU button isn't working. Only if I first press and
hold Home, then select my game, will the MENU button display the menu
as expected. Also, if the first time after that I press one of the
arrow keys (19-22), it will not respond, and the MENU button stops
working again. The second time an arrow key is pressed it will respond
as usual.

It seems like the SurfaceView is "detaining" focus, so once catched,
it won't give it back to whatever listens to the MENU button.

Any ideas as to what might be causing this? I've set setFocusable
(true) and setFocusableInTouchMode(true) in the SurfaceView, the last
one causing the MENU button to not work at all.

Thanks for any suggestions.


SurfaceView detaining focus

by Dianne Hackborn » Wed, 08 Jul 2009 01:07:40 GMT

 This isn't an intrinsic problem -- apps like YouTube have a surface view and
show the menu fine.  And there isn't enough information here to provide much

Dianne Hackborn
Android framework engineer

Note: please don't send private questions to me, as I don't have time to
provide private support, and so won't reply to such e-mails.  All such
questions should be posted on public forums, where I and others can see and
answer them.


Sponsored Links

SurfaceView detaining focus

by djauto23 » Wed, 08 Jul 2009 04:32:04 GMT

 n Jul 7, 7:07pm, Dianne Hackborn <> wrote:

Yes, several other examples I've looked at, including the LunarLander
game does this, so it was my expectation too that this should work.
I've compared just about everything I can think of with the
LunarLander sourcecode, but nothing of consequence has popped up yet.

I've made one more observation: onWindowFocusChanged in my SurfaceView
gets called only when the MENU button works, or in other words, only
after having selected my game/app from the press-and-hold-home-button
menu. If I then hit one of the DPAD buttons, which my SurfaceView
listens to, onWindowFocusChanged will not get called no more when I
hit the MENU button.

It's a bit hard to explain this problem, and not obvious what info is
needed to answer/discuss the problem. I will paste the sourcecode
underneath, hoping it won't cause dismay. It's kinda greasy, but
that's not what I'm seeking advice on right now ;) - the SurfaceView - the default activity


public class MySurfaceView extends SurfaceView implements
SurfaceHolder.Callback {

private SurfaceHolder holder;
private MySurfaceViewThread mySurfaceViewThread;
private boolean hasSurface;
private Bitmap bit;
private Bitmap bitUser;
private Bitmap bitShadow;
private Bitmap wall;

private Bitmap face;

private Game game;

Bitmap bitResized;

public MySurfaceView(Context context, AttributeSet attrs) {
super(context, attrs);

public void onWindowFocusChanged(boolean hasWindowFocus) {
Log.d(this.getClass().getName(), "changed focus");

private void initialize() {

// Create a new SurfaceHolder and assign this class as its
holder = getHolder();
hasSurface = false;
mySurfaceViewThread = new MySurfaceViewThread(holder, this);

BitmapDrawable d = (BitmapDrawable)getResources().getDrawable

bit = d.getBitmap();

BitmapDrawable s = (BitmapDrawable)getResources().getDrawable

face = s.getBitmap();

int width = face.getWidth();
int height = face.getHeight();
int newWidth = 240;
int newHeight = 343;

float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;

Matrix matrix = new Matrix();

matrix.postScale(scaleWidth, scaleHeight);

bitResized = Bitmap.createBitmap(face, 0, 0,
width, height, matrix, true);

BitmapDrawable e = (BitmapDrawable)getResources().getDrawable
bitUser = e.getBitmap();

BitmapDrawable f = (BitmapDrawable)getResources().getDrawable
wall = f.getBitmap();

BitmapDrawable g = (BitmapDrawable)getResources().getDrawable
bitShadow = g.getBitmap();

Other Threads

1. Why no support for existing phones?

Why is there no support for existing phones?

There are a lot of Smartphones out there with real crappy software,
why android not compile for these devices?


2. Much To Do About Nothing - News on Google Branded Phone

Like another Bigfoot sighting, news of a Google manufactured phone
came out on TechCrunch -

Unless this is chaulked up to slow Sunday news, its odd they would
rekindle this, especially on heresay from the Google execs on a Google
branded phone - not even a direct quote or elaboration.

IMHO - I can see Google branded phones by other manufactures like how
the Microsoft sticker is branded on most computers using its OS, but I
dont see any time soon Google making the phones themselves.


3. Scheduler +ringmode for Phones

4. Any phone?

5. Supported Gadgets

6. Submitting user login details to a web site

7. Map and Overlay