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. Availability and installation

Dear Androiders :-),
I have some questions it may not related to development but at least it
relates to Android,, Anybody knows if the HTC G1 will be available outside
US?? and if there is any approximate timing for an international release??

Another thing,, Can I somehow Install Android on a normal HTC Touc Pro for
example just remove the Windows mobile and install android?? any Idea???

Thanks in advance
Hazem Farahat
Tel: 010 7447783


2. Custom view scrolling

I am trying to implement a custom view which can scroll horizontally
and vertically. I have called
but no scroll bars are displayed. I have overridden
computeHorizontalScrollExtent etc. but none of these methods are

3. Google ads embedded in Android applications

4. How to use SmsManager.sendDataMessage() to send the message

5. Custom Preference and Focus

6. List of Android Apps

7. How to go back to an application?