Android Nougat: TextureView doesn't support displaying a background drawable

The following are snippets from the source code for View for Android Nougat:

/**
 * Allow setForeground/setBackground to be called (and ignored) on a textureview,
 * without throwing
 */
static boolean sTextureViewIgnoresDrawableSetters = false;

In the single-argument constructor (called from all the others):

        // Prior to N, TextureView would silently ignore calls to setBackground/setForeground.
        // On N+, we throw, but that breaks compatibility with apps that use these methods.
        sTextureViewIgnoresDrawableSetters = targetSdkVersion <= M;

In the View constructor where your exception is thrown:

...
        switch (attr) {
            case com.android.internal.R.styleable.View_background:
                background = a.getDrawable(attr);
                break;
...
    if (background != null) {
        setBackground(background);  // <--- this is the problematic line, apparently "background" is not null here
    }

The actual definition of setBackground:

/**
 * Set the background to a given Drawable, or remove the background. If the
 * background has padding, this View's padding is set to the background's
 * padding. However, when a background is removed, this View's padding isn't
 * touched. If setting the padding is desired, please use
 * {@link #setPadding(int, int, int, int)}.
 *
 * @param background The Drawable to use as the background, or null to remove the
 *        background
 */
public void setBackground(Drawable background) {
    //noinspection deprecation
    setBackgroundDrawable(background);
}

Then the override of setBackgroundDrawable in TextureView:

@Override
public void setBackgroundDrawable(Drawable background) {
    if (background != null && !sTextureViewIgnoresDrawableSetters) {
        throw new UnsupportedOperationException(
                "TextureView doesn't support displaying a background drawable");
    }
}

So what you can piece together from all that is: 1) You have a target SDK N (Nougat) - obvious from your build file; 2) The constructor from View determines a non-null background (I cannot explain this part at the moment).

That's all it takes for this to be an actual problem. I do not see that you have managed to define a drawable in your xml, so overriding setBackground or setBackgroundDrawable seems to be the most sensible possibility to resolve the issue to me. There may be another workaround (or maybe "suggested usage" would be a better terminology) whereby you can manage to coerce the background variable in the constructor to remain null.


If you look at the source for texture view for API 24 you will see the following:

/**
 * Subclasses of TextureView cannot do their own rendering
 * with the {@link Canvas} object.
 *
 * @param canvas The Canvas to which the View is rendered.
 */
@Override
public final void draw(Canvas canvas) {
    // NOTE: Maintain this carefully (see View#draw)
    mPrivateFlags = (mPrivateFlags & ~PFLAG_DIRTY_MASK) | PFLAG_DRAWN;

    /* Simplify drawing to guarantee the layer is the only thing drawn - so e.g. no background,
    scrolling, or fading edges. This guarantees all drawing is in the layer, so drawing
    properties (alpha, layer paint) affect all of the content of a TextureView. */

    if (canvas.isHardwareAccelerated()) {
        DisplayListCanvas displayListCanvas = (DisplayListCanvas) canvas;

        HardwareLayer layer = getHardwareLayer();
        if (layer != null) {
            applyUpdate();
            applyTransformMatrix();

            mLayer.setLayerPaint(mLayerPaint); // ensure layer paint is up to date
            displayListCanvas.drawHardwareLayer(layer);
        }
    }
}

The comment in the body of draw() gives the rationale for the change you have seen. This is the only documentation that I have found. Compare this to TextureView from API 23:

/**
 * Subclasses of TextureView cannot do their own rendering
 * with the {@link Canvas} object.
 *
 * @param canvas The Canvas to which the View is rendered.
 */
@Override
public final void draw(Canvas canvas) {
    // NOTE: Maintain this carefully (see View.java)
    mPrivateFlags = (mPrivateFlags & ~PFLAG_DIRTY_MASK) | PFLAG_DRAWN;

    applyUpdate();
    applyTransformMatrix();
}

API 24 also introduced overrides for the "set background" methods that are not overridden in API 23. Setting a background is now clearly discouraged and is just not allowed. If you are seeing the unsupported operation exception and you are not explicitly setting a background then it is probably sneaking in through your styles. Try setting android:background="@null" in your XML to force the background to be null to get around the error. You can also add the following code to your custom view to retain the functionality on those versions that support setting a background:

@Override
public void setBackgroundDrawable(Drawable background) {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N && background != null) {
        setBackgroundDrawable(background);
    }
}

It is unclear how to replace the functionality that you have lost for API 24+ or if you even need it but just want to have the background tool in your arsenal.


Just to mention, not just TextureView: I found, that GridLayout also doesn't support displaying a background drawable since API 24.

I tried:

A) gridLayout.setBackgroundResource(R.drawable.board_960x960px_border_in_bg);

B) Resources res = getResources(); Drawable drawable = res.getDrawable(R.drawable.board_960x960px_border_in_bg); gridLayout.setBackground(drawable);

Neither of the above seems to be working above API 23.

However, TableLayout's background won't disappear even at API 24+, so I rewrote all my relevant code from GridLayout to TableLayout and now it's OK.