Browse Source

More progress with tinted check box and radio button, not 100% working yet.

Aidan Follestad 10 years ago
parent
commit
f27dc7b7ac

+ 4 - 2
library/src/main/java/com/afollestad/materialdialogs/MaterialDialogAdapter.java

@@ -45,15 +45,17 @@ class MaterialDialogAdapter extends ArrayAdapter<CharSequence> {
             case SINGLE: {
                 @SuppressLint("CutPasteId")
                 MDRadioButton radio = (MDRadioButton) view.findViewById(R.id.control);
-                radio.setChecked(dialog.mBuilder.selectedIndex == index);
+                boolean selected = dialog.mBuilder.selectedIndex == index;
                 radio.setColorFilter(dialog.mBuilder.widgetColor);
+                radio.setChecked(selected);
                 break;
             }
             case MULTI: {
                 @SuppressLint("CutPasteId")
                 MDCheckBox checkbox = (MDCheckBox) view.findViewById(R.id.control);
-                checkbox.setChecked(dialog.selectedIndicesList.contains(index));
+                boolean selected = dialog.selectedIndicesList.contains(index);
                 checkbox.setColorFilter(dialog.mBuilder.widgetColor);
+                checkbox.setChecked(selected);
                 break;
             }
         }

+ 70 - 9
library/src/main/java/com/afollestad/materialdialogs/internal/MDCheckBox.java

@@ -3,10 +3,18 @@ package com.afollestad.materialdialogs.internal;
 import android.annotation.TargetApi;
 import android.content.Context;
 import android.graphics.PorterDuff;
+import android.graphics.drawable.AnimatedStateListDrawable;
 import android.graphics.drawable.Drawable;
+import android.graphics.drawable.DrawableContainer;
 import android.os.Build;
 import android.util.AttributeSet;
+import android.util.Log;
 import android.widget.CheckBox;
+import android.widget.CompoundButton;
+
+import java.lang.reflect.Field;
+import java.util.ArrayList;
+import java.util.List;
 
 /**
  * @author Aidan Follestad (afollestad)
@@ -15,33 +23,86 @@ public class MDCheckBox extends CheckBox {
 
     public MDCheckBox(Context context) {
         super(context);
+        init();
     }
 
     public MDCheckBox(Context context, AttributeSet attrs) {
         super(context, attrs);
+        init();
     }
 
     public MDCheckBox(Context context, AttributeSet attrs, int defStyleAttr) {
         super(context, attrs, defStyleAttr);
+        init();
     }
 
     @TargetApi(Build.VERSION_CODES.LOLLIPOP)
     public MDCheckBox(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
         super(context, attrs, defStyleAttr, defStyleRes);
+        init();
     }
 
-    private int color;
+    private void init() {
+        try {
+            Field btnDrawable = CompoundButton.class.getDeclaredField("mButtonDrawable");
+            btnDrawable.setAccessible(true);
+            AnimatedStateListDrawable stateDrawable = (AnimatedStateListDrawable) btnDrawable.get(this);
 
-    @Override
-    protected boolean verifyDrawable(Drawable who) {
-        boolean val = super.verifyDrawable(who);
-        if (who != null)
-            who.setColorFilter(color, PorterDuff.Mode.SRC_ATOP);
-        return val;
+            Field stateList = AnimatedStateListDrawable.class.getSuperclass().getDeclaredField("mStateListState");
+            stateList.setAccessible(true);
+            DrawableContainer.DrawableContainerState stateListState = (DrawableContainer.DrawableContainerState) stateList.get(stateDrawable);
+
+            Drawable[] mDrawables = stateListState.getChildren();
+
+            Field stateSets = stateListState.getClass().getSuperclass().getDeclaredField("mStateSets");
+            stateSets.setAccessible(true);
+            int[][] stateSetsValues = (int[][]) stateSets.get(stateListState);
+
+            int index = 0;
+            List<Drawable> checkDraws = new ArrayList<>();
+            for (int[] state : stateSetsValues) {
+                if (state == null || state.length == 0) continue;
+                for (int stateSub : state) {
+                    if (stateSub == android.R.attr.state_checked)
+                        checkDraws.add(mDrawables[index]);
+                }
+                index++;
+            }
+            mCheckedDrawables = checkDraws.toArray(new Drawable[checkDraws.size()]);
+
+            Log.v("TEMP", "TEMP");
+
+        } catch (Throwable t) {
+            Log.v("MDCompoundButtonColor", t.getLocalizedMessage());
+        }
     }
 
+    private int color;
+    private Drawable[] mCheckedDrawables;
+
     public void setColorFilter(int color) {
         this.color = color;
-//        invalidate();
+        if (mCheckedDrawables != null) {
+            for (Drawable d : mCheckedDrawables)
+                d.setColorFilter(color, PorterDuff.Mode.SRC_ATOP);
+        }
+    }
+
+    @Override
+    protected void drawableStateChanged() {
+        super.drawableStateChanged();
+        if (mCheckedDrawables != null) {
+            for (Drawable d : mCheckedDrawables)
+                d.setColorFilter(color, PorterDuff.Mode.SRC_ATOP);
+        }
+    }
+
+    @Override
+    public void setChecked(boolean checked) {
+        super.setChecked(checked);
+        if (mCheckedDrawables != null) {
+            for (Drawable d : mCheckedDrawables)
+                d.setColorFilter(color, PorterDuff.Mode.SRC_ATOP);
+        }
     }
-}
+}

+ 72 - 11
library/src/main/java/com/afollestad/materialdialogs/internal/MDRadioButton.java

@@ -3,45 +3,106 @@ package com.afollestad.materialdialogs.internal;
 import android.annotation.TargetApi;
 import android.content.Context;
 import android.graphics.PorterDuff;
+import android.graphics.drawable.AnimatedStateListDrawable;
 import android.graphics.drawable.Drawable;
+import android.graphics.drawable.DrawableContainer;
 import android.os.Build;
 import android.util.AttributeSet;
-import android.widget.CheckBox;
+import android.util.Log;
+import android.widget.CompoundButton;
+import android.widget.RadioButton;
+
+import java.lang.reflect.Field;
+import java.util.ArrayList;
+import java.util.List;
 
 /**
  * @author Aidan Follestad (afollestad)
  */
-public class MDRadioButton extends CheckBox {
+public class MDRadioButton extends RadioButton {
 
     public MDRadioButton(Context context) {
         super(context);
+        init();
     }
 
     public MDRadioButton(Context context, AttributeSet attrs) {
         super(context, attrs);
+        init();
     }
 
     public MDRadioButton(Context context, AttributeSet attrs, int defStyleAttr) {
         super(context, attrs, defStyleAttr);
+        init();
     }
 
     @TargetApi(Build.VERSION_CODES.LOLLIPOP)
     public MDRadioButton(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
         super(context, attrs, defStyleAttr, defStyleRes);
+        init();
     }
 
-    private int color;
+    private void init() {
+        try {
+            Field btnDrawable = CompoundButton.class.getDeclaredField("mButtonDrawable");
+            btnDrawable.setAccessible(true);
+            AnimatedStateListDrawable stateDrawable = (AnimatedStateListDrawable) btnDrawable.get(this);
 
-    @Override
-    protected boolean verifyDrawable(Drawable who) {
-        boolean val = super.verifyDrawable(who);
-        if (who != null)
-            who.setColorFilter(color, PorterDuff.Mode.SRC_ATOP);
-        return val;
+            Field stateList = AnimatedStateListDrawable.class.getSuperclass().getDeclaredField("mStateListState");
+            stateList.setAccessible(true);
+            DrawableContainer.DrawableContainerState stateListState = (DrawableContainer.DrawableContainerState) stateList.get(stateDrawable);
+
+            Drawable[] mDrawables = stateListState.getChildren();
+
+            Field stateSets = stateListState.getClass().getSuperclass().getDeclaredField("mStateSets");
+            stateSets.setAccessible(true);
+            int[][] stateSetsValues = (int[][]) stateSets.get(stateListState);
+
+            int index = 0;
+            List<Drawable> checkDraws = new ArrayList<>();
+            for (int[] state : stateSetsValues) {
+                if (state == null || state.length == 0) continue;
+                for (int stateSub : state) {
+                    if (stateSub == android.R.attr.state_checked)
+                        checkDraws.add(mDrawables[index]);
+                }
+                index++;
+            }
+            mCheckedDrawables = checkDraws.toArray(new Drawable[checkDraws.size()]);
+
+            Log.v("TEMP", "TEMP");
+
+        } catch (Throwable t) {
+            Log.v("MDCompoundButtonColor", t.getLocalizedMessage());
+        }
     }
 
+    private int color;
+    private Drawable[] mCheckedDrawables;
+
     public void setColorFilter(int color) {
         this.color = color;
-//        invalidate();
+        if (mCheckedDrawables != null) {
+            for (Drawable d : mCheckedDrawables)
+                d.setColorFilter(color, PorterDuff.Mode.SRC_ATOP);
+        }
+    }
+
+    @Override
+    protected void drawableStateChanged() {
+        super.drawableStateChanged();
+        if (mCheckedDrawables != null) {
+            for (Drawable d : mCheckedDrawables)
+                d.setColorFilter(color, PorterDuff.Mode.SRC_ATOP);
+        }
+    }
+
+    @Override
+    public void setChecked(boolean checked) {
+        super.setChecked(checked);
+        if (mCheckedDrawables != null) {
+            for (Drawable d : mCheckedDrawables)
+                d.setColorFilter(color, PorterDuff.Mode.SRC_ATOP);
+        }
     }
-}
+}