Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I add a background color to an Android SwitchPreference? [duplicate]

I am using a PreferenceFragment to inflate an xml file with a single SwitchPreference. How can I make the background color of that preference including the title of the SwitchPreference to match the image below. I have tried setting the background but I am only able to set the background color of the switch icon.

enter image description here

like image 728
Alex Wilson Avatar asked Jan 30 '26 20:01

Alex Wilson


1 Answers

styles.xml (make this style in style.xml file in values folder)

<resources>

<style name="SwitchTheme" parent="Theme.AppCompat.Light">
    <!-- switch on thumb & track color -->
    <item name="colorControlActivated">#02c754</item>

    <!-- switch off thumb color -->
    <item name="colorSwitchThumbNormal">#f1f1f1</item>

    <!-- switch off track color -->
    <item name="android:colorForeground">#42221f1f</item>
</style>

</resources>

your_layout_activity.xml

<android.support.v7.widget.SwitchCompat
    android:id="@+id/switch_on_off"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentEnd="true"
    android:layout_alignParentRight="true"
    android:layout_gravity="center"
    android:checked="true"
    android:gravity="center"
    android:paddingLeft="30dp"
    android:theme="@style/SwitchTheme"
    app:switchMinWidth="55dp"/>

this source is worked for me. Try with this you will get understand

like image 93
Lakmal Weerasekara Avatar answered Feb 01 '26 09:02

Lakmal Weerasekara



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!