1

I'm facing this weird problem where I have a custom view (with custom drawing) overriding ToggleButton, and for some reason overriding the onDraw and draw methods do not prevent the parent class from drawing a part of it, which makes my view look like it's glitched.

This bug seems to be happening only with API level 25, on a physical device or on the emulator.

Using the following code for my custom toggle Button:

public class CustomToggleButton extends ToggleButton {
    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public CustomToggleButton(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    public CustomToggleButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public CustomToggleButton(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CustomToggleButton(Context context) {
        super(context);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        //nope, not drawing anything
    }

    @Override
    public void draw(Canvas canvas) {
        //nope, not drawing anything
    }
}

And the following simple XML layout:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="top|center_horizontal"
        android:text="Bottom left: buggy custom ToggleButton.\nBottom right: regular ToggleButton." />

    <test.com.togglebuttonbug.CustomToggleButton
        android:layout_width="48dp"
        android:layout_height="48dp"
        android:layout_gravity="bottom|left"
        android:textOff="B"
        android:textOn="A" />

    <ToggleButton
        android:layout_width="48dp"
        android:layout_height="48dp"
        android:layout_gravity="bottom|right"
        android:textOff="B"
        android:textOn="A" />
</FrameLayout>

The result is the following: API23 vs API25

If you look carefully at the screenshot, you can see that for the phone on the right, at the bottom left, instead of having nothing like the empty draw methods should do, you can see a piece of gray drawing. Would anybody have an idea of what could be causing this? Is that a bug on Android N?

androidseb
  • 1,257
  • 2
  • 14
  • 17

0 Answers0