Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added setEnable() function. #12

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ The library is a custom Switch widget inspired by this [dribbble shot](https://d
## Gradle
Add this into your dependencies block.
```
compile 'com.polyak:icon-switch:1.0.0'
implementation 'com.github.parminder93:IconSwitch:v1.0.5'
```
## Sample
Please see the [sample app](sample/src/main) for a library usage example.
Expand Down Expand Up @@ -41,6 +41,7 @@ To control the current state or get information about it, use:
iconSwitch.setChecked();
iconSwitch.getChecked();
iconSwitch.toggle();
iconSwitch.setEnabled();
```

#### Color
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ ext {
groupId = 'com.polyak'
uploadName = 'IconSwitch'
description = 'Custom switch-like widget.'
publishVersion = '1.0.0'
publishVersion = '1.0.1'
licences = ['Apache-2.0']
}
4 changes: 2 additions & 2 deletions iconswitch/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ android {
defaultConfig {
minSdkVersion 16
targetSdkVersion 25
versionCode 1
versionName "1.0"
versionCode 2
versionName "1.1"
}
}

Expand Down
73 changes: 67 additions & 6 deletions iconswitch/src/main/java/com/polyak/iconswitch/IconSwitch.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public class IconSwitch extends ViewGroup {
private int inactiveTintIconLeft, activeTintIconLeft;
private int inactiveTintIconRight, activeTintIconRight;
private int thumbColorLeft, thumbColorRight;
private int backgroundColor;

private PointF downPoint;
private boolean isClick;
Expand Down Expand Up @@ -123,7 +124,7 @@ private void init(AttributeSet attr) {
activeTintIconLeft = ta.getColor(R.styleable.IconSwitch_isw_active_tint_icon_left, colorDefActive);
inactiveTintIconRight = ta.getColor(R.styleable.IconSwitch_isw_inactive_tint_icon_right, colorDefInactive);
activeTintIconRight = ta.getColor(R.styleable.IconSwitch_isw_active_tint_icon_right, colorDefActive);
background.setColor(ta.getColor(R.styleable.IconSwitch_isw_background_color, colorDefBackground));
backgroundColor = ta.getColor(R.styleable.IconSwitch_isw_background_color, colorDefBackground);
thumbColorLeft = ta.getColor(R.styleable.IconSwitch_isw_thumb_color_left, colorDefThumb);
thumbColorRight = ta.getColor(R.styleable.IconSwitch_isw_thumb_color_right, colorDefThumb);
currentChecked = Checked.values()[ta.getInt(R.styleable.IconSwitch_isw_default_selection, 0)];
Expand Down Expand Up @@ -210,6 +211,9 @@ protected void onLayout(boolean changed, int l, int t, int r, int b) {

@Override
public boolean onTouchEvent(MotionEvent e) {
if(!this.isEnabled())
return false;

MotionEvent event = MotionEvent.obtain(e);
event.setLocation(e.getX() - translationX, e.getY() - translationY);
switch (event.getAction()) {
Expand All @@ -224,6 +228,7 @@ public boolean onTouchEvent(MotionEvent e) {
clearTouchInfo();
break;
case MotionEvent.ACTION_CANCEL:
cancelAction();
clearTouchInfo();
break;
}
Expand All @@ -232,6 +237,27 @@ public boolean onTouchEvent(MotionEvent e) {
return true;
}

public void setEnabled(boolean enabled) {
super.setEnabled(enabled);

int left = currentChecked == Checked.LEFT ? thumbStartLeft : thumbEndLeft;
if (thumbDragHelper.smoothSlideViewTo(thumb, left, thumb.getTop())) {
ViewCompat.postInvalidateOnAnimation(this);
}

ensureCorrectColors();
}

@ColorInt
private int getDisableColor(@ColorInt int color) {
return Color.argb(30, Color.red(color), Color.green(color), Color.blue(color));
}

@ColorInt
private int getEnableColor(@ColorInt int color) {
return Color.rgb(Color.red(color), Color.green(color), Color.blue(color));
}

private void onDown(MotionEvent e) {
velocityTracker = VelocityTracker.obtain();
velocityTracker.addMovement(e);
Expand Down Expand Up @@ -267,6 +293,12 @@ private void clearTouchInfo() {
}
}

private void cancelAction() {
if (thumbDragHelper.smoothSlideViewTo(thumb, currentChecked == Checked.LEFT ? thumbStartLeft : thumbEndLeft, thumb.getTop())) {
ViewCompat.postInvalidateOnAnimation(this);
}
}

private void toggleSwitch() {
currentChecked = currentChecked.toggle();
int newLeft = currentChecked == Checked.LEFT ? thumbStartLeft : thumbEndLeft;
Expand Down Expand Up @@ -315,9 +347,28 @@ public void setCheckedChangeListener(CheckedChangeListener listener) {
}

private void ensureCorrectColors() {
if(isEnabled()) {
thumbColorLeft = getEnableColor(thumbColorLeft);
thumbColorRight = getEnableColor(thumbColorRight);
backgroundColor = getEnableColor(backgroundColor);

leftIcon.setAlpha(1f);
rightIcon.setAlpha(1f);
} else {
thumbColorLeft = getDisableColor(thumbColorLeft);
thumbColorRight = getDisableColor(thumbColorRight);
backgroundColor = getDisableColor(backgroundColor);

leftIcon.setAlpha(0.3f);
rightIcon.setAlpha(0.3f);
}

leftIcon.setColorFilter(isLeftChecked() ? activeTintIconLeft : inactiveTintIconLeft);
rightIcon.setColorFilter(isLeftChecked() ? inactiveTintIconRight : activeTintIconRight);
thumb.setColor(isLeftChecked() ? thumbColorLeft : thumbColorRight);
background.setColor(backgroundColor);


}

private boolean isLeftChecked() {
Expand All @@ -331,15 +382,24 @@ private void notifyCheckedChanged() {
}

public void setChecked(Checked newChecked) {
if (currentChecked != newChecked) {
if (isEnabled() && currentChecked != newChecked) {
toggleSwitch();
notifyCheckedChanged();
}
}

public void setChecked(Checked newChecked, boolean ignoreEnable) {
if ((ignoreEnable || isEnabled()) && currentChecked != newChecked) {
toggleSwitch();
notifyCheckedChanged();
}
}

public void toggle() {
toggleSwitch();
notifyCheckedChanged();
if(isEnabled()) {
toggleSwitch();
notifyCheckedChanged();
}
}

public Checked getChecked() {
Expand Down Expand Up @@ -376,8 +436,9 @@ public void setActiveTintIconRight(@ColorInt int activeTintIconRight) {
ensureCorrectColors();
}

public void setBackgroundColor(@ColorInt int color) {
background.setColor(color);
public void setBackgroundColor(@ColorInt int backgroundColor) {
this.backgroundColor = backgroundColor;
ensureCorrectColors();
}

public ImageView getLeftIcon() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,9 @@ public boolean settleCapturedViewAt(int finalLeft, int finalTop) {
}

private boolean forceSettleCapturedViewAt(int finalLeft, int finalTop, int xvel, int yvel) {
if(mCapturedView == null)
return false;

final int startLeft = mCapturedView.getLeft();
final int startTop = mCapturedView.getTop();
final int dx = finalLeft - startLeft;
Expand Down
4 changes: 2 additions & 2 deletions sample/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ android {
applicationId "com.polyak.iconswitch.sample"
minSdkVersion 21
targetSdkVersion 25
versionCode 1
versionName "1.0"
versionCode 2
versionName "1.1"
}

buildTypes {
Expand Down
2 changes: 1 addition & 1 deletion sample/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

<activity
android:name=".MainActivity"
android:screenOrientation="portrait">
android:screenOrientation="fullSensor">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import android.animation.ValueAnimator;
import android.content.Intent;
import android.graphics.Point;
import android.graphics.drawable.Icon;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.ColorInt;
Expand Down Expand Up @@ -34,6 +35,7 @@ public class MainActivity extends AppCompatActivity implements OnMapReadyCallbac
private static final Uri URL_GITHUB_POLYAK = Uri.parse("https://github.com/polyak01");
private static final Uri URL_GITHUB_YAROLEGOVICH = Uri.parse("https://github.com/yarolegovich");
private static final Uri URL_DRIBBBLE_PROKHODA = Uri.parse("https://dribbble.com/prokhoda");
private static final Uri URL_GITHUB_PARMINDER93 = Uri.parse("https://github.com/parminder93");

private int[] toolbarColors;
private int[] statusBarColors;
Expand Down Expand Up @@ -77,6 +79,14 @@ protected void onCreate(Bundle savedInstanceState) {
findViewById(R.id.credit_polyak).setOnClickListener(this);
findViewById(R.id.credit_yarolegovich).setOnClickListener(this);
findViewById(R.id.credit_prokhoda).setOnClickListener(this);
findViewById(R.id.credit_parminder93).setOnClickListener(this);

((IconSwitch)findViewById(R.id.ics_enable)).setCheckedChangeListener(new IconSwitch.CheckedChangeListener() {
@Override
public void onCheckChanged(Checked current) {
iconSwitch.setEnabled(current == Checked.LEFT);
}
});
}

private void updateColors(boolean animated) {
Expand Down Expand Up @@ -159,6 +169,9 @@ public void onClick(View v) {
case R.id.credit_prokhoda:
open(URL_DRIBBBLE_PROKHODA);
break;
case R.id.credit_parminder93:
open(URL_GITHUB_PARMINDER93);
break;
}
}

Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
29 changes: 27 additions & 2 deletions sample/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,40 @@
android:layout_height="wrap_content"
app:mp_icon="@drawable/ic_github_circle_grey600_24dp"
app:mp_summary="@string/credit_summary_yarolegovich"
app:mp_title="@string/credit_title_yarolegovich" />
app:mp_title="@string/credit_title_yarolegovich" >

</com.yarolegovich.mp.MaterialRightIconPreference>

<com.yarolegovich.mp.MaterialRightIconPreference
android:id="@+id/credit_prokhoda"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:mp_icon="@drawable/ic_dribbble_grey600_24dp"
app:mp_summary="@string/credit_summary_prokhoda"
app:mp_title="@string/credit_title_prokhoda" />
app:mp_title="@string/credit_title_prokhoda" >

</com.yarolegovich.mp.MaterialRightIconPreference>

<com.yarolegovich.mp.MaterialRightIconPreference
android:id="@+id/credit_parminder93"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:mp_icon="@drawable/ic_github_circle_grey600_24dp"
app:mp_summary="@string/credit_summary_parminder93"
app:mp_title="@string/credit_title_parminder93" >

<com.polyak.iconswitch.IconSwitch
android:id="@+id/ics_enable"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="end|center_vertical"
app:isw_default_selection="left"
app:isw_icon_left="@drawable/ic_enable_white_18dp"
app:isw_icon_right="@drawable/ic_disable_white_18dp"
app:isw_inactive_tint_icon_left="@android:color/holo_green_light"
app:isw_thumb_color_left="@android:color/holo_green_light"
app:isw_thumb_color_right="@color/mapPrimary" />
</com.yarolegovich.mp.MaterialRightIconPreference>

</com.yarolegovich.mp.MaterialPreferenceCategory>

Expand Down
2 changes: 2 additions & 0 deletions sample/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@
<string name="credit_summary_yarolegovich">Contributor</string>
<string name="credit_title_polyak">Yaroslav Polyakov</string>
<string name="credit_summary_polyak">Developer</string>
<string name="credit_title_parminder93">Parminder Singh</string>
<string name="credit_summary_parminder93">Contributor</string>
</resources>