---
title: "How to style ToggleButton so that the background color is WHITE?"  
description: "How to style ToggleButton so that the background color is WHITE?"  
author: "Mark M"  
published: 2015-02-03  
updated: 2015-02-03  
canonical: https://www.mindstick.com/forum/12937/how-to-style-togglebutton-so-that-the-background-color-is-white  
category: "mobile development"  
tags: ["xml", "android", "java"]  
reading_time: 2 minutes  

---

# How to style ToggleButton so that the background color is WHITE?

I have tried the following

View view = (View) findViewById (id);

view.setBackgroundColor (Color.WHITE);

The above destroys the [property](https://www.mindstick.com/blog/205/property-notification-in-c-sharp) of the ToggleButton. I can no longer make out if it is [TextView](https://www.mindstick.com/forum/23237/how-do-i-center-text-horizontally-and-vertically-in-a-textview-in-android) or ToggleButton. Clicking the [button](https://www.mindstick.com/articles/63/how-to-add-button-in-datagridview-in-csharp-dot-net) produces [onClick](https://www.mindstick.com/forum/12718/onclick-for-hyperlink) but the UI change to show the selected bar is absent.

ToggleButton btn = (ToggleButton) findViewById (id);

btn.setBackgroundColor (Color.WHITE);

This also produces the same error. Using XML styling also [causes](https://yourviews.mindstick.com/view/293/renovation-causes-wastage-of-resources) the same error. I am using [Android Studio](https://www.mindstick.com/blog/10942/android-studio-vs-eclipse) v1.0.2 and the target is a lollipop emulator. I tried the same in 4.4.4 mobile. I see the same behavior.

I don't want to use images. Kindly don't [suggest me](https://www.mindstick.com/interview/23114/can-you-suggest-me-few-top-directory-submission-site) that. I have gone through the [threads](https://www.mindstick.com/blog/11678/threads-in-java) that are available in stackoverflow - nothing [works for me](https://answers.mindstick.com/qa/110735/how-do-i-create-a-study-schedule-that-works-for-me).

Can [anyone help me](https://www.mindstick.com/forum/331/can-anyone-help-me-out-how-to-use-session-concept)?

## Replies

### Reply by Lillian Martin

It will be something like this as an xml file in drawables that you want as the background for the toggle button. You need to add the various colors to a color file in res/values.

```
<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android">     <item android:state_checked="true" android:state_pressed="true">        <layer-list xmlns:android="http://schemas.android.com/apk/res/android" >            <item android:top="2dp">                <shape android:shape="rectangle">                    <solid android:color="@color/toggle_button_pressed" />                </shape>            </item>            <item android:top="2dp" android:left="10dp" android:right="10dp">                <shape android:shape="rectangle">                    <solid android:color="@color/toggle_button_checked" />                </shape>            </item>            <item android:top="2dp" android:bottom="2dp" android:left="10dp" android:right="10dp">                <shape android:shape="rectangle">                    <solid android:color="@color/toggle_button_pressed" />                </shape>            </item>        </layer-list>        </item>     <item android:state_pressed="true">        <layer-list xmlns:android="http://schemas.android.com/apk/res/android" >            <item android:top="2dp">                <shape android:shape="rectangle">                    <solid android:color="@color/toggle_button_pressed" />                </shape>            </item>            <item android:top="2dp" android:left="10dp" android:right="10dp">                <shape android:shape="rectangle">                    <solid android:color="@color/toggle_button_unchecked" />                </shape>            </item>            <item android:top="2dp" android:bottom="2dp" android:left="10dp" android:right="10dp">                <shape android:shape="rectangle">                    <solid android:color="@color/toggle_button_pressed" />                </shape>            </item>        </layer-list>        </item>     <item android:state_checked="true">        <layer-list xmlns:android="http://schemas.android.com/apk/res/android" >           <item>                <shape android:shape="rectangle">                    <solid android:color="@color/toggle_button_shadow" />                </shape>            </item>            <item android:bottom="2dp">                <shape android:shape="rectangle">                    <solid android:color="@color/toggle_button_unpressed" />                </shape>            </item>            <item android:bottom="2dp"android:left="10dp" android:right="10dp">                <shape android:shape="rectangle">                    <solid android:color="@color/toggle_button_checked" />                </shape>            </item>            <item android:bottom="4dp"android:left="10dp" android:right="10dp">                <shape android:shape="rectangle">                    <solid android:color="@color/toggle_button_unpressed" />                </shape>            </item>        </layer-list>    </item>     <item>        <layer-list xmlns:android="http://schemas.android.com/apk/res/android" >           <item>                <shape android:shape="rectangle">                    <solid android:color="@color/toggle_button_shadow" />                </shape>            </item>            <item android:bottom="2dp">                <shape android:shape="rectangle">                    <solid android:color="@color/toggle_button_unpressed" />                </shape>            </item>            <item android:bottom="2dp"android:left="10dp" android:right="10dp">                <shape android:shape="rectangle">                    <solid android:color="@color/toggle_button_unchecked" />                </shape>            </item>            <item android:bottom="4dp"android:left="10dp" android:right="10dp">                <shape android:shape="rectangle">                    <solid android:color="@color/toggle_button_unpressed" />                </shape>            </item>        </layer-list>    </item> </selector>
```

I haven't checked it so I don't know if it works. Please let me know if I made any errors.


---

Original Source: https://www.mindstick.com/forum/12937/how-to-style-togglebutton-so-that-the-background-color-is-white

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
