Change on color of a Switch
As of now it is better to use SwitchCompat from the AppCompat.v7 library. You can then use simple styling to change the color of your components.
values/themes.xml:
<style name="Theme.MyTheme" parent="Theme.AppCompat.Light">
<!-- colorPrimary is used for the default action bar background -->
<item name="colorPrimary">@color/my_awesome_color</item>
<!-- colorPrimaryDark is used for the status bar -->
<item name="colorPrimaryDark">@color/my_awesome_darker_color</item>
<!-- colorAccent is used as the default value for colorControlActivated,
which is used to tint widgets -->
<item name="colorAccent">@color/accent</item>
<!-- You can also set colorControlNormal, colorControlActivated
colorControlHighlight, and colorSwitchThumbNormal. -->
</style>
ref: Android Developers Blog
EDIT:
The way in which it should be correctly applied is through android:theme="@style/Theme.MyTheme"
and also this can be applied to parent styles such as EditTexts, RadioButtons, Switches, CheckBoxes and ProgressBars:
<style name="My.Widget.ProgressBar" parent="Widget.AppCompat.ProgressBar">
<style name="My.Widget.Checkbox" parent="Widget.AppCompat.CompoundButton.CheckBox">
Change color of Switch in Android
As per this, (direct answer by BoD copy-paste):
You must not use
android:thumb
andandroid:track
, but instead,app:thumb
andapp:track
And you must add the following to the root of your xml document:xmlns:app="http://schemas.android.com/apk/res-auto"
Change the Color of a Switch - FormBuilderSwitch - flutter
You can use bool to do that
Example:
bool switched = false; //Based on which state you want it to be on init
Widget to trigger switch function
FormBuilderSwitch(
onChanged: (bool) {
setState(() {
bool = !bool;
switched = bool;
});
},
)
and here's an example of color change based on bool
Container(
color: switched? Colors.white : Colors.blue,
)
Update :
Here's the code
FormBuilderSwitch(
name: "public",
title: Text("Wird noch ein Mitspieler gesucht?"),
initialValue: false,
controlAffinity: ListTileControlAffinity.leading,
decoration: InputDecoration(border: InputBorder.none),
onChanged: (bool) {
setState(() {
bool = !bool;
switched = bool;
});
},
),
Container(
height: 20,
width: 20,
color: switched ? Colors.black54 : Colors.blue,
),
The outputs
https://i.stack.imgur.com/x3j35.png
https://i.stack.imgur.com/XUBpy.png
Update:
FormBuilderSwitch(
name: "public",
title: Text("Wird noch ein Mitspieler gesucht?"),
initialValue: false,
controlAffinity: ListTileControlAffinity.leading,
activeColor: Colors.red
decoration: InputDecoration(border: InputBorder.none),
onChanged: (bool) {
setState(() {
bool = !bool;
switched = bool;
});
},
),
Container(
height: 20,
width: 20,
color: switched ? Colors.red : Colors.black,
),
Change color of Switch while turning OFF react-native
onTintColor has been deprecated try the following.
<Switch
trackColor={{true: 'red', false: 'grey'}}
onValueChange={this.toggleSwitch}
value={true}/>
this works
Changing the background color of a Switch
The background is called a track
and you can change the color of it by using
app:trackTint="@color/darkThemeGreyTextColor"
The "button" is called a thumb
and you can change the color of it with
app:thumbTint="@color/heartRed"
I didn't see these attributes because I was using the android:
prefix instead of the app:
prefix to see the available options.
How to Change Color of Knob in Bootstrap 5 Switch
It will work if you change the fill
color correctly.
That means you can use a hex color with the appropriate escape %23 for the #
character.
For example: #cc2222
fill='%23cc2222'/%3e%3c/svg%3e"
Or, you can used a named color...
For example: red
fill='red'/%3e%3c/svg%3e"
Or, you can use an RGB color with the appropriate escapes for parens (%28,%29)...
For example: rgba(100, 0, 0, .25)
fill='rgba%28100, 0, 0, 0.25%29'/%3e%3c/svg%3e"
Demo
How can I change the checked background color of this Bootstrap 4 toggle switch?
You can simply altered every possible properties that can affect the color like
.custom-control-input:checked
,.custom-control-label::before
,.custom-control-input:active
and.custom-control-input:focus
but you have to pay attention on altering .custom-control-input::after
because it will destroy the pointer inside the toggle switch
Example
.custom-control-input:focus~.custom-control-label::before {
border-color: red !important;
box-shadow: 0 0 0 0.2rem rgba(255, 47, 69, 0.25) !important;
}
.custom-control-input:checked~.custom-control-label::before {
border-color: red !important;
background-color: red !important;
}
.custom-control-input:active~.custom-control-label::before {
background-color: red !important;
border-color: red !important;
}
.custom-control-input:focus:not(:checked)~.custom-control-label::before {
border-color: red !important;
}
.custom-control-input-green:not(:disabled):active~.custom-control-label::before {
background-color: red !important;
border-color: red !important;
}
<link rel="stylesheet" type="text/css" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
<div class="custom-control custom-switch">
<input type="checkbox" class="custom-control-input" id="darkSwitch" />
<label class="custom-control-label" for="darkSwitch">Dark Mode</label>
</div>
How to change the color of a SwitchCompat from AppCompat library
AppCompat tinting attributs:
First, you should take a look to appCompat lib article there and to different attributs you can set:
colorPrimary: The primary branding color for the app. By default, this is the color applied to the action bar background.
colorPrimaryDark: Dark variant of the primary branding color. By default, this is the color applied to the status bar (via statusBarColor) and navigation bar (via navigationBarColor).
colorAccent: Bright complement to the primary branding color. By default, this is the color applied to framework controls (via colorControlActivated).
colorControlNormal: The color applied to framework controls in their normal state.
colorControlActivated: The color applied to framework controls in their activated (ex. checked, switch on) state.
colorControlHighlight: The color applied to framework control highlights (ex. ripples, list selectors).
colorButtonNormal: The color applied to framework buttons in their normal state.
colorSwitchThumbNormal: The color applied to framework switch thumbs in their normal state. (switch off)
If all custom switches are the same in a single activity:
With previous attributes you can define your own theme for each activity:
<style name="Theme.MyActivityTheme" parent="Theme.AppCompat.Light">
<!-- colorPrimary is used for the default action bar background -->
<item name="colorPrimary">@color/my_awesome_color</item>
<!-- colorPrimaryDark is used for the status bar -->
<item name="colorPrimaryDark">@color/my_awesome_darker_color</item>
<!-- colorAccent is used as the default value for colorControlActivated,
which is used to tint widgets -->
<item name="colorAccent">@color/accent</item>
<!-- You can also set colorControlNormal, colorControlActivated
colorControlHighlight, and colorSwitchThumbNormal. -->
</style>
and :
<manifest>
...
<activity
android:name=".MainActivity"
android:theme="@style/Theme.MyActivityTheme">
</activity>
...
</manifest>
If you want to have differents custom switches in a single activity:
As widget tinting in appcompat works by intercepting any layout inflation and inserting a special tint-aware version of the widget in its place (See Chris Banes post about it) you can not apply a custom style to each switch of your layout xml file. You have to set a custom Context that will tint switch with right colors.
--
To do so for pre-5.0 you need to create a Context that overlays global theme with customs attributs and then create your switches programmatically:
ContextThemeWrapper ctw = ContextThemeWrapper(getActivity(), R.style.Color1SwitchStyle);
SwitchCompat sc = new SwitchCompat(ctw)
As of AppCompat v22.1
you can use the following XML
to apply a theme to the switch widget:
<RelativeLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
...>
<android.support.v7.widget.SwitchCompat
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:theme="@style/Color1SwitchStyle"/>
Your custom switch theme:
<style name="Color1SwitchStyle">
<item name="colorControlActivated">@color/my_awesome_color</item>
</style>
--
On Android 5.0 it looks like a new view attribut comes to life : android:theme
(same as one use for activity declaration in manifest). Based on another Chris Banes post, with the latter you should be able to define a custom theme directly on a view from your layout xml:
<android.support.v7.widget.SwitchCompat
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:theme="@style/Color1SwitchStyle"/>
To change the track color of a SwitchCompat
Thanks to vine'th I complete my answer with a link to SO answer that explains how to specify the Foreground of the Track when Switch is Off, it's there.
Related Topics
Local Image Caching Solution for Android: Square Picasso, Universal Image Loader, Glide, Fresco
How to Size an Android View Based on Its Parent's Dimensions
Using Global Exception Handling on Android
Android Design Support Library for API 28 (P) Not Working
Android Studio Error Installing Gradle
Why Is Android Geocoder Throwing a "Service Not Available" Exception
How to Create a Transparent Demo Screen for an Android App
Making Data Persistent in Android
Continue Service Even If Application Is Cleared from Recent App
How to Integrate Eclipse with Gradle in Android Project
How to Use Accelerometer to Measure Distance for Android Application Development
Permission Denial: This Requires Android.Permission.Interact_Across_Users_Full
How to Write Character & in Android Strings.Xml
When Should I Use the Assets as Opposed to Raw Resources in Android