123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- /*
- * Nextcloud Android client application
- *
- * @author Tobias Kaminsky
- * Copyright (C) 2017 Tobias Kaminsky
- * Copyright (C) 2017 Nextcloud GmbH.
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Affero General Public License for more details.
- *
- * You should have received a copy of the GNU Affero General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
- package com.owncloud.android.ui;
- import android.content.Context;
- import android.content.res.ColorStateList;
- import android.graphics.Color;
- import android.os.Build;
- import android.preference.SwitchPreference;
- import android.util.AttributeSet;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.Switch;
- import com.owncloud.android.R;
- import com.owncloud.android.utils.ThemeUtils;
- import androidx.annotation.RequiresApi;
- import androidx.core.graphics.drawable.DrawableCompat;
- /**
- * Themeable switch preference
- */
- public class ThemeableSwitchPreference extends SwitchPreference {
- public ThemeableSwitchPreference(Context context) {
- super(context);
- }
- public ThemeableSwitchPreference(Context context, AttributeSet attrs, int defStyleAttr) {
- super(context, attrs, defStyleAttr);
- }
- public ThemeableSwitchPreference(Context context, AttributeSet attrs) {
- super(context, attrs);
- }
- @Override
- protected void onBindView(View view) {
- super.onBindView(view);
- if (view instanceof ViewGroup && Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
- findSwitch((ViewGroup) view);
- }
- }
- @RequiresApi(Build.VERSION_CODES.JELLY_BEAN)
- private void findSwitch(ViewGroup viewGroup) {
- ColorStateList thumbColorStateList = null;
- ColorStateList trackColorStateList = null;
- for (int i = 0; i < viewGroup.getChildCount(); i++) {
- View child = viewGroup.getChildAt(i);
- if (child instanceof Switch) {
- Switch switchView = (Switch) child;
- if(thumbColorStateList == null && trackColorStateList == null) {
- int color = ThemeUtils.primaryAccentColor(getContext());
- int trackColor = Color.argb(77, Color.red(color), Color.green(color), Color.blue(color));
- int trackColorUnchecked = getContext().getResources().getColor(R.color.switch_track_color_unchecked);
- thumbColorStateList = new ColorStateList(
- new int[][]{new int[]{android.R.attr.state_checked}, new int[]{}},
- new int[]{color, Color.WHITE});
- trackColorStateList = new ColorStateList(
- new int[][]{new int[]{android.R.attr.state_checked},
- new int[]{}},
- new int[]{trackColor, trackColorUnchecked});
- // new int[]{trackColor, Color.parseColor("#4D000000")});
- }
- // setting the thumb color
- DrawableCompat.setTintList(switchView.getThumbDrawable(), thumbColorStateList);
- // setting the track color
- DrawableCompat.setTintList(switchView.getTrackDrawable(), trackColorStateList);
- break;
- } else if (child instanceof ViewGroup) {
- findSwitch((ViewGroup) child);
- }
- }
- }
- }
|