When displaying a background, AndEngine produces black bars above the image on certain devices. How do I support variable screen ratios for different devices using AndEngine?

  • \$\begingroup\$ Can you clarify this a bit? \$\endgroup\$ Feb 28, 2013 at 7:23
  • \$\begingroup\$ i am getting a black screen on top of my 9 patch image.When i run my code. In my small size device this image working well but when i am using tablet it display with some black screen on top of this image.I want to use this one image in different size of screen. All devices must be supported. \$\endgroup\$
    – Maan
    Feb 28, 2013 at 8:51

2 Answers 2


I believe what you're asking is how to get rid of letterboxing, like this:

enter image description here

By default, AndEngine assumes you want some fixed aspect ratio. It then uses letterboxing to handle devices with different display aspect ratios then what you're providing. The advantage is you have certainty about your layout.

There's more than one approach to get rid of them, but here's what I do. The following code takes a fixed height (in this case 320) and adjusts the width based on the aspect ratio of the device.

//Pick some value for your height.
float cameraHeight = 320;

//Get the display metrics object.
final DisplayMetrics displayMetrics = new DisplayMetrics();

//Populate it with data about your display.

//Calculate the aspect ratio ofthe device.
float aspectRatio = (float) displayMetrics.widthPixels / (float) displayMetrics.heightPixels;

//Multiply the aspect ratio by the fixed height.
float cameraWidth = Math.round(aspectRatio * CAMERA_HEIGHT);

//Create the camera using those values.
this.mCamera = new Camera(0, 0, cameraWidth, cameraHeight);
  • \$\begingroup\$ Not on both side blank (top and Bottom) but only on top side i am getting blank screen. \$\endgroup\$
    – Maan
    Feb 28, 2013 at 9:10
  • 1
    \$\begingroup\$ @Maan Nonetheless, does this resolve it? Also, might help if you post a screenshot. \$\endgroup\$ Feb 28, 2013 at 9:22
  • \$\begingroup\$ sorry ,i cant' send you screen shots as a image because i have only 2 reputation. \$\endgroup\$
    – Maan
    Feb 28, 2013 at 9:30
  • \$\begingroup\$ i am trying to implement the code which you give n to me but what is GameConstant. I am getting red mark on that line. \$\endgroup\$
    – Maan
    Feb 28, 2013 at 9:31
  • \$\begingroup\$ ya it's accepted \$\endgroup\$
    – Maan
    Feb 28, 2013 at 10:04

I used CropResolutionPolicy, a new policy developed and later further extended by members of the AndEngine community. Crop policy is similar to Ratio that comes with the AndEngine, but instead of padding, it scales the scene while keeping aspect ratio to cover the whole screen and crops the overflow.

Crop policy is illustrated on the following image from this website that explains all the AndEngine resolution policies.

enter image description here image used with permission of the author

  • \$\begingroup\$ This would be a better answer if it summarized the techniques explained in the link. \$\endgroup\$
    – user1430
    Apr 15, 2014 at 15:40

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .