SurfaceHolder.setFormat(PixelFormat.RGBA_8888) fails on some devices and not others

I have a game app with the following Views structure. First I have an empty FrameLayout like so:

FrameLayout game_frame_layout = new FrameLayout(getApplicationContext());

Then I add two views to it like so:

  • onItemClickListener not firing on custom ArrayAdapter
  • Does Proguard guarantee to provide the same mapping if no source has changed?
  • automatically solve Android build Error:Frame pixels must be either solid or transparent (not intermediate alphas). - Found at pixel #4 along top edge
  • JavaScript how to check User Agent for Mobile/Tablet
  • clipChildren is not working
  • getting Google oauth authorization token from Android- return with invalid_scope/ Unknown error
  • game_frame_layout.addView(customView);

    The customView is for displaying all sorts of moving game graphics while the butView displays some ImageButtons on top of the moving grapics. The customView is an instance of a class CustomView which extends SurfaceView.

    CustomView includes the following code

        void updateView() 
            final SurfaceHolder holder = getHolder();
                    Canvas canvas = holder.lockCanvas();
                    if (canvas != null) 
            catch (Exception e) 

    The line holder.setFormat(PixelFormat.RGBA_8888); is a recent addition (see here). Without that line, my animated graphics appear to be in a format with too few colours (by experiment I deduced it was “RGB_565”), so I get some banding artifacts. When I added the setFormat line, the graphics appear perfectly (without banding) on my Samsung Galaxy Tab 10.1 (Android 3.1)… but on three other devices: a Samsung GT-l9100 (4.1.2), a Nexus 7 ME370T 4.4.2 and a HTC One X 4.2.2 I only see the buttons corresponding to butView against an entirely black background. There is no indication in the logs that the program has crashed.

    Any ideas?

    Related posts:

    Google+ profile like scrolling Effect
    Error ERROR_RECOGNIZER_BUSY with offline speech recognition
    How to self uninstall an app?
    Android LinearLayout fill-the-middle
    Bad performance with Guava Cache on Android
    BuildConfig file in android - Purpose and Possibilities
  • What is the concept behind
  • Check if service is running on Android?
  • Cannot start Android Emulator: “Unable to load VM from snapshot”
  • Comments in Android Layout xml
  • What is the purpose of Android's <merge> tag in XML layouts?
  • Get the field value with a Cursor
  • 5 Solutions collect form web for “SurfaceHolder.setFormat(PixelFormat.RGBA_8888) fails on some devices and not others”

    Probably not all android devices support a 32/24 bit pixel format so that’s why 565 is always working while 888 might fail.
    You might try to understand if it’s a problem with the hardware acceleration trying to disable it

    Set the pixel format before the setContentView() on the activity:


    What happens when you change the order you add them to the game_frame_layout?
    First the butView and then the customView:


    I assume the butView won’t be visible, but i’m curious whether the customView renders its content properly.

    Your application doesn’t seems to be working post ICS. ICS saw major revisions in the graphics department, the most significant being introduction of TextureView which is designed to address the shortcomings of SurfaceView. Try extending TextureView instead of SurfaceView for your CustomView class. For more info visit Android 4.0 Graphics and Animation

    I’m curious if you set butView‘s background to transparent.

    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.