android – Change ToggleButton background to Clear/Dark Gray-ThrowExceptions

Exception or error:

I’m using two toggle buttons on my application, when I run it on the emulator the button has black text, and a light gray background. As we can see in the next image:

Clear ToggleButton

When I run it on my real device the button has another style, it has white text, and a black background. As shown in the next image.

Dark ToggleButton

My QUESTION IS: How to change the style of a ToggleButton to it always stay with a clear gray background, as in the first image? Is it defined by a Theme?

The xml code used for the button was:

<ToggleButton
    android:id="@+id/toggleButtonSimulado"
    android:layout_gravity="center|top"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textOn="On"
    android:textOff="Off" />
How to solve:

Style of the button or any other view changes in the real device according to the current theme selected on that device. To prevent this you need to specify background color and text color explicitly. Use a gradient as the background of the button. For example:

<ToggleButton
    android:id="@+id/toggleButtonSimulado"
    android:layout_gravity="center"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/gray_gradient"
    android:textOn="On"
    android:textOff="Off"
    android:textColor="@android:color/black" />

###

You can use your own backgrounds for the buttons.
Search btn_toggle_off.9.png/btn_toggle_on.9 on your sdk install dir(android-sdk-mac_86/platforms/android-9/data/res/drawable-hdpi).
And make sure to copy the selector located on drawable folder

Or you can try:

androiddrawables

androiddrawablesexplorer

Hope this help.

Leave a Reply

Your email address will not be published. Required fields are marked *