Forráskód Böngészése

themeable switch preference

tobiasKaminsky 8 éve
szülő
commit
938e222e8f

+ 89 - 0
src/main/java/com/owncloud/android/ui/ThemeableSwitchPreference.java

@@ -0,0 +1,89 @@
+/*
+ * 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.preference.SwitchPreference;
+import android.support.v4.graphics.drawable.DrawableCompat;
+import android.util.AttributeSet;
+import android.view.View;
+import android.view.ViewGroup;
+import android.widget.Switch;
+
+import com.owncloud.android.utils.ThemeUtils;
+
+
+/**
+ * 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) {
+            findSwitch((ViewGroup) view);
+        }
+    }
+
+    private void findSwitch(ViewGroup viewGroup) {
+        for (int i = 0; i < viewGroup.getChildCount(); i++) {
+            View child = viewGroup.getChildAt(i);
+
+            if (child instanceof Switch) {
+                Switch switchView = (Switch) child;
+
+                int color = ThemeUtils.primaryAccentColor();
+                int trackColor = Color.argb(77, Color.red(color), Color.green(color), Color.blue(color));
+
+                // setting the thumb color
+                DrawableCompat.setTintList(switchView.getThumbDrawable(), new ColorStateList(
+                        new int[][]{new int[]{android.R.attr.state_checked}, new int[]{}},
+                        new int[]{color, Color.WHITE}));
+
+                // setting the track color
+                DrawableCompat.setTintList(switchView.getTrackDrawable(), new ColorStateList(
+                        new int[][]{new int[]{android.R.attr.state_checked}, new int[]{}},
+                        new int[]{trackColor, Color.parseColor("#4D000000")}));
+
+                break;
+            } else if (child instanceof ViewGroup) {
+                findSwitch((ViewGroup) child);
+            }
+        }
+    }
+}

+ 10 - 4
src/main/res/xml/preferences.xml

@@ -29,7 +29,7 @@
 	<PreferenceCategory
 		android:title="@string/drawer_folder_sync"
 		android:key="folder_sync">
-		<android.preference.SwitchPreference
+		<com.owncloud.android.ui.ThemeableSwitchPreference
 			android:title="@string/auto_upload_on_wifi"
 			android:key="synced_folder_on_wifi"/>
         <Preference
@@ -93,9 +93,15 @@
     </PreferenceCategory>
 
 	<PreferenceCategory android:title="@string/prefs_category_details" android:key="details">
-		<android.preference.SwitchPreference android:title="@string/prefs_passcode" android:key="set_pincode" />
-		<android.preference.SwitchPreference android:title="@string/prefs_fingerprint" android:key="use_fingerprint" />
-		<android.preference.SwitchPreference android:title="@string/prefs_show_hidden_files" android:key="show_hidden_files" />
+		<com.owncloud.android.ui.ThemeableSwitchPreference
+			android:title="@string/prefs_passcode"
+			android:key="set_pincode"/>
+		<com.owncloud.android.ui.ThemeableSwitchPreference
+			android:title="@string/prefs_fingerprint"
+			android:key="use_fingerprint"/>
+		<com.owncloud.android.ui.ThemeableSwitchPreference
+			android:title="@string/prefs_show_hidden_files"
+			android:key="show_hidden_files"/>
 	</PreferenceCategory>
 
 	<PreferenceCategory android:title="@string/prefs_category_more" android:key="more">