Tyler Pfaff Tyler Pfaff - 1 year ago 40
Android Question

Can you set the background color on a toggle button without covering up the toggle?

I have a ToggleButton. I want the background to be clear, like in the default alarm app for the days of the week. The code below covers the toggle with the clear color. Is there any way to keep the toggle and change the background color without rolling my own toggle button? If not, that would be pretty poorly throughout, imo. Also, do I really have to define a clear color here or is there a built in clear color I could be using in my xml?


This is my colors.xml

<?xml version="1.0" encoding="utf-8"?>
<color name="clear">#ffffff00</color>

This is my color state list xml in the

<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android">
android:drawable="@color/clear" />
android:drawable="@color/clear" />

Answer Source

I want the background to be clear..

Transparent? Yes, android does have a color defined for that. You can access it as:


Or, in code:


@android:color/transparent is defined in res/values/colors.xml:

<color name="transparent">#00000000</color>

The alpha bits (first and second) are zero.

But, your definition for color clear:

<color name="clear">#ffffff00</color>

is not transparent. You can visualize transparent as any color with its alpha set to zero. In your definition of clear, the alpha bits are full-blown to ff - 255 - opaque.

Your color definition produces this:

enter image description here

Is there any way to keep the toggle and change the background color without rolling my own toggle button?

The thing is: the background color and the toggle is one drawable. The whole 'on' state is represented by one single drawable, and so is the 'off' state. You cannot simply change the color without losing the toggle feedback. To change anything about the default ToggleButton's background, you will have to provide alternate drawables for each state.

I want the background to be clear, like in the default alarm app for the days of the week..

Setting the background to transparent will not work then. I would suggest that you go through the source code and resources involved in the making of a ToggleButton. For example, the on and off states of a ToggleButton are represented by drawable resources. So, if you decide to change the background, you will need to provide ToggleButton with at least two drawables: one for each state.

Look at how the default alarm app does it. The ToggleButtons being used for days are defined as:


The drawable toggle_underline is a state-selector:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:state_window_focused="true"
    <item android:state_checked="true"

As you can see, when the ToggleButton is set to on or checked ( or when it is pressed), @drawable/toggle_underline_activated is set to the background. Otherwise, @drawable/toggle_underline_normal is used - state is off.

toggle_underline_activated and toggle_underline_normal are both 9-patch drawables.


enter image description here


enter image description here

You can get these drawables (and more) here: Link.

So, you can either create your own 9 patch drawables with transparent background, and use them with a state-selector drawable - or you can look at the default alarm clock project and use the drawables from its drawable-XXXX folders.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download