ThemeableSwitchPreference.java 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. * Nextcloud Android client application
  3. *
  4. * @author Tobias Kaminsky
  5. * Copyright (C) 2017 Tobias Kaminsky
  6. * Copyright (C) 2017 Nextcloud GmbH.
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. package com.owncloud.android.ui;
  22. import android.content.Context;
  23. import android.content.res.ColorStateList;
  24. import android.graphics.Color;
  25. import android.os.Build;
  26. import android.preference.SwitchPreference;
  27. import android.util.AttributeSet;
  28. import android.view.View;
  29. import android.view.ViewGroup;
  30. import android.widget.Switch;
  31. import com.owncloud.android.R;
  32. import com.owncloud.android.utils.ThemeUtils;
  33. import androidx.annotation.RequiresApi;
  34. import androidx.core.graphics.drawable.DrawableCompat;
  35. /**
  36. * Themeable switch preference
  37. */
  38. public class ThemeableSwitchPreference extends SwitchPreference {
  39. public ThemeableSwitchPreference(Context context) {
  40. super(context);
  41. }
  42. public ThemeableSwitchPreference(Context context, AttributeSet attrs, int defStyleAttr) {
  43. super(context, attrs, defStyleAttr);
  44. }
  45. public ThemeableSwitchPreference(Context context, AttributeSet attrs) {
  46. super(context, attrs);
  47. }
  48. @Override
  49. protected void onBindView(View view) {
  50. super.onBindView(view);
  51. if (view instanceof ViewGroup && Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
  52. findSwitch((ViewGroup) view);
  53. }
  54. }
  55. @RequiresApi(Build.VERSION_CODES.JELLY_BEAN)
  56. private void findSwitch(ViewGroup viewGroup) {
  57. ColorStateList thumbColorStateList = null;
  58. ColorStateList trackColorStateList = null;
  59. for (int i = 0; i < viewGroup.getChildCount(); i++) {
  60. View child = viewGroup.getChildAt(i);
  61. if (child instanceof Switch) {
  62. Switch switchView = (Switch) child;
  63. if(thumbColorStateList == null && trackColorStateList == null) {
  64. int color = ThemeUtils.primaryAccentColor(getContext());
  65. int trackColor = Color.argb(77, Color.red(color), Color.green(color), Color.blue(color));
  66. int trackColorUnchecked = getContext().getResources().getColor(R.color.switch_track_color_unchecked);
  67. thumbColorStateList = new ColorStateList(
  68. new int[][]{new int[]{android.R.attr.state_checked}, new int[]{}},
  69. new int[]{color, Color.WHITE});
  70. trackColorStateList = new ColorStateList(
  71. new int[][]{new int[]{android.R.attr.state_checked},
  72. new int[]{}},
  73. new int[]{trackColor, trackColorUnchecked});
  74. // new int[]{trackColor, Color.parseColor("#4D000000")});
  75. }
  76. // setting the thumb color
  77. DrawableCompat.setTintList(switchView.getThumbDrawable(), thumbColorStateList);
  78. // setting the track color
  79. DrawableCompat.setTintList(switchView.getTrackDrawable(), trackColorStateList);
  80. break;
  81. } else if (child instanceof ViewGroup) {
  82. findSwitch((ViewGroup) child);
  83. }
  84. }
  85. }
  86. }