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. Basic RPC - which one to use ? JSON / XML / SOAP / HTTP etc etc etc.

Hi there - I need to do some very basic RPC stuff passing around a few
objects ( very simply serialized ) as well as the usual standard
authentication etc etc. I'm looking for an RPC stack which is both

1) Client side : Natively supported under android (or failing that -
with a minimal of reqd libraries)
2) Server side : Easily configured to run under tomcat - and perhaps
under other servers as well (python / perl perhaps ?)

Although I have read numerous postings on this (use xyz / use abc) I
haven't found an end to end solution that has both the client code on
it as well as the server side setup - I don't mind running a
standalone tomcat to solely look after the android connections if it
makes it easier. I've started writing my interface classes in the
client code and a dummy implementation so that my client can pretend
it's talking to something somewhere but I'm after an established
mature mechanism so I can just fit this in. The more code examples -
btw - the better...

Thanks in advance,


2. Part of CNET CiUI iPhoneUI libs working in Android

Just to let everyone know that I was able to get part of the CNET
iPhone CiUI UI lib working on Android using Webkit WebVIew to embed
webkit, see the video of screenshots:

3. position UI components

4. Web-Cam with Android

5. Adwords options for Android Phones...

6. port c++ layout engine

7. Android on other carriers 3G networks