From d008167c4b640e5c9243c456414959478cdf047a Mon Sep 17 00:00:00 2001 From: Hussein Yassine Date: Mon, 5 Oct 2020 17:36:42 +0300 Subject: [PATCH] Modifying the labeled switch and adding enable text, as well as invert colors functionalities --- .../angads25/toggledemo/MainActivity.kt | 2 +- app/src/main/res/layout/activity_test.xml | 17 +- app/src/main/res/values/colors.xml | 5 + .../angads25/toggle/model/ToggleableView.java | 51 +++++ .../angads25/toggle/widget/LabeledSwitch.java | 183 ++++++++++-------- toggle/src/main/res/values/attrs.xml | 6 + 6 files changed, 182 insertions(+), 82 deletions(-) diff --git a/app/src/main/java/com/github/angads25/toggledemo/MainActivity.kt b/app/src/main/java/com/github/angads25/toggledemo/MainActivity.kt index 3a0948a..fc6daf9 100755 --- a/app/src/main/java/com/github/angads25/toggledemo/MainActivity.kt +++ b/app/src/main/java/com/github/angads25/toggledemo/MainActivity.kt @@ -33,7 +33,7 @@ class MainActivity : AppCompatActivity(), View.OnClickListener { override fun onClick(view: View) { when (view.id) { - R.id.switch_labeled -> startActivity(Intent(this@MainActivity, LabeledSwitchActivity::class.java)) + R.id.switch_labeled -> startActivity(Intent(this@MainActivity, TestActivity::class.java)) R.id.switch_day_night -> startActivity(Intent(this@MainActivity, DayNightActivity::class.java)) } } diff --git a/app/src/main/res/layout/activity_test.xml b/app/src/main/res/layout/activity_test.xml index c8b072d..441b156 100644 --- a/app/src/main/res/layout/activity_test.xml +++ b/app/src/main/res/layout/activity_test.xml @@ -15,14 +15,23 @@ ~ limitations under the License. --> - + android:layout_height="match_parent" + android:background="#752BE6" + xmlns:app="http://schemas.android.com/apk/res-auto"> + android:layout_margin="16dp" + android:textSize="10sp" + app:enableText="false" + app:invertColors="false" + app:on="false" + app:colorOff="@color/color_white" + app:colorOn="@color/edit_profile_bottomsheet_bg" + app:colorBorder="@color/color_white"/> + \ No newline at end of file diff --git a/app/src/main/res/values/colors.xml b/app/src/main/res/values/colors.xml index 3ab3e9c..33643c5 100755 --- a/app/src/main/res/values/colors.xml +++ b/app/src/main/res/values/colors.xml @@ -3,4 +3,9 @@ #3F51B5 #303F9F #FF4081 + + + #FFFFFF + #16283B + diff --git a/toggle/src/main/java/com/github/angads25/toggle/model/ToggleableView.java b/toggle/src/main/java/com/github/angads25/toggle/model/ToggleableView.java index 6b7047b..19cd10b 100755 --- a/toggle/src/main/java/com/github/angads25/toggle/model/ToggleableView.java +++ b/toggle/src/main/java/com/github/angads25/toggle/model/ToggleableView.java @@ -42,6 +42,22 @@ public class ToggleableView extends View { */ protected boolean isOn; + /** + * Field to determine whether text is enabled. + * + * @see #isTextEnabled() + * @see #setTextOn(boolean) + */ + protected boolean isTextEnabled; + + /** + * Field to determine whether text is enabled. + * + * @see #isColorInverted() + * @see #setColorInverted(boolean) + */ + protected boolean isColorInverted; + /** * Field to determine whether switch is enabled/disabled. * @@ -110,6 +126,41 @@ public void setOn(boolean on) { isOn = on; } + /** + *

Returns the boolean state of the text in the Switch.

+ * + * @return true if the switch is on, false if it is off. + */ + public boolean isTextEnabled() { + return isTextEnabled; + } + + /** + *

Changes the boolean state of this Switch.

+ * + * @param on true to turn switch on, false to turn it off. + */ + public void setTextOn(boolean on) { + isTextEnabled = on; + } + /** + *

Returns the boolean state of the text in the Switch.

+ * + * @return true if the switch is on, false if it is off. + */ + public boolean isColorInverted() { + return isColorInverted; + } + + /** + *

Changes the boolean state of this Switch.

+ * + * @param on true to turn switch on, false to turn it off. + */ + public void setColorInverted(boolean on) { + isColorInverted = on; + } + /** * Returns the enabled status for this switch. The interpretation of the * enabled state varies by subclass. diff --git a/toggle/src/main/java/com/github/angads25/toggle/widget/LabeledSwitch.java b/toggle/src/main/java/com/github/angads25/toggle/widget/LabeledSwitch.java index bc7daa9..9ad5e00 100755 --- a/toggle/src/main/java/com/github/angads25/toggle/widget/LabeledSwitch.java +++ b/toggle/src/main/java/com/github/angads25/toggle/widget/LabeledSwitch.java @@ -72,8 +72,9 @@ public class LabeledSwitch extends ToggleableView { /** * Simple constructor to use when creating a switch from code. + * * @param context The Context the switch is running in, through which it can - * access the current theme, resources, etc. + * access the current theme, resources, etc. */ public LabeledSwitch(Context context) { super(context); @@ -84,8 +85,8 @@ public LabeledSwitch(Context context) { * Constructor that is called when inflating a switch from XML. * * @param context The Context the switch is running in, through which it can - * access the current theme, resources, etc. - * @param attrs The attributes of the XML tag that is inflating the switch. + * access the current theme, resources, etc. + * @param attrs The attributes of the XML tag that is inflating the switch. */ public LabeledSwitch(Context context, AttributeSet attrs) { super(context, attrs); @@ -97,12 +98,12 @@ public LabeledSwitch(Context context, AttributeSet attrs) { * Perform inflation from XML and apply a class-specific base style from a * theme attribute. * - * @param context The Context the switch is running in, through which it can - * access the current theme, resources, etc. - * @param attrs The attributes of the XML tag that is inflating the switch. + * @param context The Context the switch is running in, through which it can + * access the current theme, resources, etc. + * @param attrs The attributes of the XML tag that is inflating the switch. * @param defStyleAttr An attribute in the current theme that contains a - * reference to a style resource that supplies default values for - * the switch. Can be 0 to not look for defaults. + * reference to a style resource that supplies default values for + * the switch. Can be 0 to not look for defaults. */ public LabeledSwitch(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); @@ -116,7 +117,7 @@ private void initView() { this.labelOff = "OFF"; this.enabled = true; - this.textSize = (int)(12f * getResources().getDisplayMetrics().scaledDensity); + this.textSize = (int) (12f * getResources().getDisplayMetrics().scaledDensity); if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) { colorBorder = colorOn = getResources().getColor(R.color.colorAccent, getContext().getTheme()); @@ -139,11 +140,15 @@ private void initView() { } private void initProperties(AttributeSet attrs) { - TypedArray tarr = getContext().getTheme().obtainStyledAttributes(attrs, R.styleable.Toggle,0,0); + TypedArray tarr = getContext().getTheme().obtainStyledAttributes(attrs, R.styleable.Toggle, 0, 0); final int N = tarr.getIndexCount(); for (int i = 0; i < N; ++i) { int attr = tarr.getIndex(i); - if (attr == R.styleable.Toggle_on) { + if (attr == R.styleable.Toggle_invertColors) { + isColorInverted = tarr.getBoolean(R.styleable.Toggle_invertColors, true); + } else if (attr == R.styleable.Toggle_enableText) { + isTextEnabled = tarr.getBoolean(R.styleable.Toggle_enableText, true); + } else if (attr == R.styleable.Toggle_on) { isOn = tarr.getBoolean(R.styleable.Toggle_on, false); } else if (attr == R.styleable.Toggle_colorOff) { colorOff = tarr.getColor(R.styleable.Toggle_colorOff, Color.parseColor("#FFFFFF")); @@ -170,21 +175,22 @@ private void initProperties(AttributeSet attrs) { } else if (attr == R.styleable.Toggle_textOn) { labelOn = tarr.getString(R.styleable.Toggle_textOn); } else if (attr == R.styleable.Toggle_android_textSize) { - int defaultTextSize = (int)(12f * getResources().getDisplayMetrics().scaledDensity); + int defaultTextSize = (int) (12f * getResources().getDisplayMetrics().scaledDensity); textSize = tarr.getDimensionPixelSize(R.styleable.Toggle_android_textSize, defaultTextSize); - } else if(attr == R.styleable.Toggle_android_enabled) { + } else if (attr == R.styleable.Toggle_android_enabled) { enabled = tarr.getBoolean(R.styleable.Toggle_android_enabled, false); } } } - @Override protected void onDraw(Canvas canvas) { + @Override + protected void onDraw(Canvas canvas) { super.onDraw(canvas); paint.setTextSize(textSize); // Drawing Switch background here { - if(isEnabled()) { + if (isEnabled()) { paint.setColor(colorBorder); } else { paint.setColor(colorDisabled); @@ -203,8 +209,12 @@ private void initProperties(AttributeSet attrs) { alpha = (alpha < 0 ? 0 : (alpha > 255 ? 255 : alpha)); int onColor; - if(isEnabled()) { - onColor = Color.argb(alpha, Color.red(colorOn), Color.green(colorOn), Color.blue(colorOn)); + if (isEnabled()) { + if (isColorInverted()) { + onColor = Color.argb(alpha, Color.red(colorOn), Color.green(colorOn), Color.blue(colorOn)); + } else { + onColor = Color.argb(alpha, Color.red(colorOff), Color.green(colorOff), Color.blue(colorOff)); + } } else { onColor = Color.argb(alpha, Color.red(colorDisabled), Color.green(colorDisabled), Color.blue(colorDisabled)); } @@ -225,62 +235,77 @@ private void initProperties(AttributeSet attrs) { } // Drawing Switch Labels here - String MAX_CHAR = "N"; - float textCenter = paint.measureText(MAX_CHAR) / 2; - if(isOn) { - int alpha = (int)((((width >>> 1) - thumbBounds.centerX()) / ((width >>> 1) - thumbOffCenterX)) * 255); - alpha = (alpha < 0 ? 0 : (alpha > 255 ? 255 : alpha)); - int onColor = Color.argb(alpha, Color.red(colorOn), Color.green(colorOn), Color.blue(colorOn)); - paint.setColor(onColor); - - float centerX = (width - padding - ((padding + (padding >>> 1)) + (thumbRadii << 1))) >>> 1; - canvas.drawText(labelOff, (padding + (padding >>> 1)) + (thumbRadii << 1) + centerX - (paint.measureText(labelOff) / 2), (height >>> 1) + textCenter, paint); - - alpha = (int)(((thumbBounds.centerX() - (width >>> 1)) / (thumbOnCenterX - (width >>> 1))) * 255); - alpha = (alpha < 0 ? 0 : (alpha > 255 ? 255 : alpha)); - int offColor = Color.argb(alpha, Color.red(colorOff), Color.green(colorOff), Color.blue(colorOff)); - paint.setColor(offColor); - - int maxSize = width - (padding << 1) - (thumbRadii << 1); - - centerX = (((padding >>> 1) + maxSize) - padding) >>> 1; - canvas.drawText(labelOn, padding + centerX - (paint.measureText(labelOn) / 2), (height >>> 1) + textCenter, paint); - } else { - int alpha = (int)(((thumbBounds.centerX() - (width >>> 1)) / (thumbOnCenterX - (width >>> 1))) * 255); - alpha = (alpha < 0 ? 0 : (alpha > 255 ? 255 : alpha)); - int offColor = Color.argb(alpha, Color.red(colorOff), Color.green(colorOff), Color.blue(colorOff)); - paint.setColor(offColor); - - int maxSize = width - (padding << 1) - (thumbRadii << 1); - float centerX = (((padding >>> 1) + maxSize) - padding) >>> 1; - canvas.drawText(labelOn, padding + centerX - (paint.measureText(labelOn) / 2), (height >>> 1) + textCenter, paint); - - alpha = (int)((((width >>> 1) - thumbBounds.centerX()) / ((width >>> 1) - thumbOffCenterX)) * 255); - alpha = (alpha < 0 ? 0 : (alpha > 255 ? 255 : alpha)); - int onColor; - if(isEnabled()) { - onColor = Color.argb(alpha, Color.red(colorOn), Color.green(colorOn), Color.blue(colorOn)); + if (isTextEnabled()) { + String MAX_CHAR = "N"; + float textCenter = paint.measureText(MAX_CHAR) / 2; + if (isOn) { + int alpha = (int) ((((width >>> 1) - thumbBounds.centerX()) / ((width >>> 1) - thumbOffCenterX)) * 255); + alpha = (alpha < 0 ? 0 : (alpha > 255 ? 255 : alpha)); + int onColor = Color.argb(alpha, Color.red(colorOn), Color.green(colorOn), Color.blue(colorOn)); + paint.setColor(onColor); + + float centerX = (width - padding - ((padding + (padding >>> 1)) + (thumbRadii << 1))) >>> 1; + canvas.drawText(labelOff, (padding + (padding >>> 1)) + (thumbRadii << 1) + centerX - (paint.measureText(labelOff) / 2), (height >>> 1) + textCenter, paint); + + alpha = (int) (((thumbBounds.centerX() - (width >>> 1)) / (thumbOnCenterX - (width >>> 1))) * 255); + alpha = (alpha < 0 ? 0 : (alpha > 255 ? 255 : alpha)); + int offColor = Color.argb(alpha, Color.red(colorOff), Color.green(colorOff), Color.blue(colorOff)); + paint.setColor(offColor); + + int maxSize = width - (padding << 1) - (thumbRadii << 1); + + centerX = (((padding >>> 1) + maxSize) - padding) >>> 1; + canvas.drawText(labelOn, padding + centerX - (paint.measureText(labelOn) / 2), (height >>> 1) + textCenter, paint); } else { - onColor = Color.argb(alpha, Color.red(colorDisabled), Color.green(colorDisabled), Color.blue(colorDisabled)); - } - paint.setColor(onColor); + int alpha = (int) (((thumbBounds.centerX() - (width >>> 1)) / (thumbOnCenterX - (width >>> 1))) * 255); + alpha = (alpha < 0 ? 0 : (alpha > 255 ? 255 : alpha)); + int offColor = Color.argb(alpha, Color.red(colorOff), Color.green(colorOff), Color.blue(colorOff)); + paint.setColor(offColor); + + int maxSize = width - (padding << 1) - (thumbRadii << 1); + float centerX = (((padding >>> 1) + maxSize) - padding) >>> 1; + canvas.drawText(labelOn, padding + centerX - (paint.measureText(labelOn) / 2), (height >>> 1) + textCenter, paint); + + alpha = (int) ((((width >>> 1) - thumbBounds.centerX()) / ((width >>> 1) - thumbOffCenterX)) * 255); + alpha = (alpha < 0 ? 0 : (alpha > 255 ? 255 : alpha)); + int onColor; + if (isEnabled()) { + onColor = Color.argb(alpha, Color.red(colorOn), Color.green(colorOn), Color.blue(colorOn)); + } else { + onColor = Color.argb(alpha, Color.red(colorDisabled), Color.green(colorDisabled), Color.blue(colorDisabled)); + } + paint.setColor(onColor); - centerX = (width - padding - ((padding + (padding >>> 1)) + (thumbRadii << 1))) >>> 1; - canvas.drawText(labelOff, (padding + (padding >>> 1)) + (thumbRadii << 1) + centerX - (paint.measureText(labelOff) / 2), (height >>> 1) + textCenter, paint); + centerX = (width - padding - ((padding + (padding >>> 1)) + (thumbRadii << 1))) >>> 1; + canvas.drawText(labelOff, (padding + (padding >>> 1)) + (thumbRadii << 1) + centerX - (paint.measureText(labelOff) / 2), (height >>> 1) + textCenter, paint); + } } // Drawing Switch Thumb here { int alpha = (int) (((thumbBounds.centerX() - thumbOffCenterX) / (thumbOnCenterX - thumbOffCenterX)) * 255); - alpha = (alpha < 0 ? 0 : (alpha > 255 ? 255 : alpha)); + if (isColorInverted()) { + alpha = (alpha < 0 ? 0 : (alpha > 255 ? 255 : alpha)); + } else { + alpha = 255; + } int offColor = Color.argb(alpha, Color.red(colorOff), Color.green(colorOff), Color.blue(colorOff)); - paint.setColor(offColor); + int onColor = Color.argb(alpha, Color.red(colorOn), Color.green(colorOn), Color.blue(colorOn)); + if (isColorInverted()) { + paint.setColor(offColor); + } else { + paint.setColor(onColor); + } canvas.drawCircle(thumbBounds.centerX(), thumbBounds.centerY(), thumbRadii, paint); alpha = (int) (((thumbOnCenterX - thumbBounds.centerX()) / (thumbOnCenterX - thumbOffCenterX)) * 255); alpha = (alpha < 0 ? 0 : (alpha > 255 ? 255 : alpha)); - int onColor; - if(isEnabled()) { + if (isColorInverted()) { + alpha = (alpha < 0 ? 0 : (alpha > 255 ? 255 : alpha)); + } else { + alpha = 255; + } + if (isEnabled()) { onColor = Color.argb(alpha, Color.red(colorOn), Color.green(colorOn), Color.blue(colorOn)); } else { onColor = Color.argb(alpha, Color.red(colorDisabled), Color.green(colorDisabled), Color.blue(colorDisabled)); @@ -290,7 +315,8 @@ private void initProperties(AttributeSet attrs) { } } - @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { + @Override + protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int desiredWidth = getResources().getDimensionPixelSize(R.dimen.labeled_default_width); int desiredHeight = getResources().getDimensionPixelSize(R.dimen.labeled_default_height); @@ -327,17 +353,17 @@ private void initProperties(AttributeSet attrs) { thumbBounds.set(padding, padding, padding + thumbRadii, height - padding); thumbOffCenterX = thumbBounds.centerX(); - if(isOn) { + if (isOn) { thumbBounds.set(width - padding - thumbRadii, padding, width - padding, height - padding); } else { thumbBounds.set(padding, padding, padding + thumbRadii, height - padding); } - leftBgArc.set(0,0, outerRadii << 1, height); - rightBgArc.set(width - (outerRadii << 1),0, width, height); + leftBgArc.set(0, 0, outerRadii << 1, height); + rightBgArc.set(width - (outerRadii << 1), 0, width, height); - leftFgArc.set(padding / 10,padding / 10, (outerRadii << 1)- (padding / 10), height - (padding / 10)); - rightFgArc.set(width - (outerRadii << 1) + (padding / 10),padding / 10, width - (padding / 10), height - (padding / 10)); + leftFgArc.set(padding / 10, padding / 10, (outerRadii << 1) - (padding / 10), height - (padding / 10)); + rightFgArc.set(width - (outerRadii << 1) + (padding / 10), padding / 10, width - (padding / 10), height - (padding / 10)); } /** @@ -346,9 +372,10 @@ private void initProperties(AttributeSet attrs) { * a sound, etc. * * @return True there was an assigned OnClickListener that was called, false - * otherwise is returned. + * otherwise is returned. */ - @Override public final boolean performClick() { + @Override + public final boolean performClick() { super.performClick(); if (isOn) { ValueAnimator switchColor = ValueAnimator.ofFloat(width - padding - thumbRadii, padding); @@ -371,8 +398,8 @@ private void initProperties(AttributeSet attrs) { switchColor.setDuration(250); switchColor.start(); } - isOn =! isOn; - if(onToggledListener != null) { + isOn = !isOn; + if (onToggledListener != null) { onToggledListener.onSwitched(this, isOn); } return true; @@ -384,8 +411,9 @@ private void initProperties(AttributeSet attrs) { * @param event The motion event. * @return True if the event was handled, false otherwise. */ - @Override public final boolean onTouchEvent(MotionEvent event) { - if(isEnabled()) { + @Override + public final boolean onTouchEvent(MotionEvent event) { + if (isEnabled()) { float x = event.getX(); switch (event.getAction()) { case MotionEvent.ACTION_DOWN: { @@ -431,7 +459,7 @@ private void initProperties(AttributeSet attrs) { switchColor.start(); isOn = false; } - if(onToggledListener != null) { + if (onToggledListener != null) { onToggledListener.onSwitched(this, isOn); } } @@ -549,9 +577,10 @@ public void setTypeface(Typeface typeface) { * * @param on true to turn switch on, false to turn it off. */ - @Override public void setOn(boolean on) { + @Override + public void setOn(boolean on) { super.setOn(on); - if(isOn) { + if (isOn) { thumbBounds.set(width - padding - thumbRadii, padding, width - padding, height - padding); } else { thumbBounds.set(padding, padding, padding + thumbRadii, height - padding); @@ -612,7 +641,7 @@ public int getTextSize() { * @param textSize text size for Switch on/off label. */ public void setTextSize(int textSize) { - this.textSize = (int)(textSize * getResources().getDisplayMetrics().scaledDensity); + this.textSize = (int) (textSize * getResources().getDisplayMetrics().scaledDensity); invalidate(); } } diff --git a/toggle/src/main/res/values/attrs.xml b/toggle/src/main/res/values/attrs.xml index 82612f6..9bfe2f9 100755 --- a/toggle/src/main/res/values/attrs.xml +++ b/toggle/src/main/res/values/attrs.xml @@ -30,6 +30,12 @@ + + + +