Search code examples
androidandroid-togglebutton

Custom background for ToggleButton hides textOn and textOff


I'm styling ToggleButtons, and I got an image to use as the new background, for all 6 states (default on, default off, pressed on, pressed off, disabled on, disabled off) as 9-patches. The problem is that when applied the textOn and textOff properties seem to be ignored, they work with the default style, but not with this one.

This is what the 9-patch for one of the looks like: enter image description here

And this is the style applied in the Theme:

<style name="Widget.Button.Toggle" parent="android:Widget.Button.Toggle">
        <item name="android:background">@drawable/toggle_button_selector</item>
        <item name="android:textColor">@color/white</item>
        <item name="android:textSize">20sp</item>
</style>

This is the relevant portion in the layout:

<ToggleButton
    android:id="@+id/toggleButton"
    android:textOn="@string/settings_mediospago_valor_personalizado"
    android:textOff="@string/settings_mediospago_valor_por_defecto"
    android:gravity="center"
    android:layout_width="200dp"
    android:layout_height="50dp"
    />

And this is how it looks:

enter image description here

Any pointers?


Solution

  • Well, this is embarrassing, but I learnt something new.

    First, the content area in the 9-Patch must be defined for all images in a given selector, for it to work properly. Second, not selecting a content area does not appear to be equal to selecting everything as content.

    My problem in this case was the first one, I had at least one image without a content area defined, I had very wrongly assumed that I could test the selector by just selecting the content area for a single state image (I would later replicate for all images). My laziness was costly.