Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change color of Switch in Android

I'm trying to change the color of my switch in Android. I realize that I will need new 9patches. I went over to http://android-holo-colors.com/ and selected my color and selected (Switch Jelly Bean). To use Switch Jelly Bean I had to use: https://github.com/BoD/android-switch-backport. To import it into my project I had to add:

<item name="switchStyle">@style/Widget.Holo.CompoundButton.Switch</item>

to my styles, and then in xml I have to use the switch like so:

<org.jraf.android.backport.switchwidget.Switch
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

Now everything with the switch works fine. Next, I took everything that was output from the android holo color generator and put it into the proper files:

  • drawable (2 selector files)
  • drawable-hdpi (9patch files)
  • drawable-xhdpi (9patch files)
  • drawable-xxhdpi (9patch files)

then I added to my xml:

<org.jraf.android.backport.switchwidget.Switch
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:thumb="@drawable/apptheme_switch_inner_holo_light"
android:track="@drawable/apptheme_switch_track_holo_light" />

but it is still the original blue color. I believe I'm doing everything correctly. Everything compiles (xml, java). Note: I AM importing org.jraf.android.backport.switchwidget.Switch in my java also. Any ideas?

like image 664
EGHDK Avatar asked Feb 12 '14 16:02

EGHDK


People also ask

How do I change the color of my switch?

You can then use simple styling to change the color of your components. 'on' is colorAccent , 'off' is colorSwitchThumbNormal . If using the AppCompat theme, you need to use SwitchCompat rather than Switch . If using SDK 23+, you can use android:thumbTint and android:thumbTintMode with a ColorStateList .

How can change background color of button in android?

To set Android Button background color, we can assign android:backgroundTint XML attribute for Button in layout file with the required Color Value. To programmatically set or change Android Button background color, we may call pass the method Button.


Video Answer


2 Answers

As per this, (direct answer by BoD copy-paste):

  • You must not use android:thumb and android:track, but instead, app:thumb and app:track And you must add the following to the root of your xml document:

    xmlns:app="http://schemas.android.com/apk/res-auto"

like image 170
velis Avatar answered Oct 02 '22 14:10

velis


Easiest way in Android Lollipop and above,

<style name="AppTheme" parent="MaterialTheme.Light">
    ...
    <item name="android:colorControlActivated">@color/color_switch</item>
</style>
like image 25
emen Avatar answered Oct 02 '22 14:10

emen