From 94c399995485c7d9296e98e4f047e4f1068d4e61 Mon Sep 17 00:00:00 2001 From: "sadegh.bakhshandeh" Date: Sun, 30 Jan 2022 13:43:26 +0330 Subject: [PATCH] fix: fix setChecked() method when set false programmatically --- .../com/sigma/niceswitchsample/MainActivity.java | 6 +++++- build.gradle | 2 +- gradle/wrapper/gradle-wrapper.properties | 2 +- .../main/java/com/sigma/niceswitch/NiceSwitch.java | 11 ++++++++++- .../java/com/sigma/niceswitch/NiceSwitchRound.java | 12 +++++++++++- 5 files changed, 28 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/com/sigma/niceswitchsample/MainActivity.java b/app/src/main/java/com/sigma/niceswitchsample/MainActivity.java index a903e75..d9c26ca 100644 --- a/app/src/main/java/com/sigma/niceswitchsample/MainActivity.java +++ b/app/src/main/java/com/sigma/niceswitchsample/MainActivity.java @@ -7,6 +7,7 @@ import android.widget.Toast; import com.sigma.niceswitch.NiceSwitch; +import com.sigma.niceswitch.NiceSwitchRound; import com.sigma.niceswitchsample.R; public class MainActivity extends AppCompatActivity { @@ -17,10 +18,13 @@ protected void onCreate(Bundle savedInstanceState) { setContentView(R.layout.activity_main); NiceSwitch niceSwitch = findViewById(R.id.niceSwitch); - + NiceSwitchRound niceSwitchRound = findViewById(R.id.niceSwitchRound); niceSwitch.setOnCheckedChangedListener(checked -> { Toast.makeText(this, checked + "", Toast.LENGTH_SHORT).show(); }); + + niceSwitch.setChecked(false); + niceSwitchRound.setChecked(false); } } diff --git a/build.gradle b/build.gradle index 68df50b..85c058c 100644 --- a/build.gradle +++ b/build.gradle @@ -7,7 +7,7 @@ buildscript { } dependencies { - classpath 'com.android.tools.build:gradle:3.3.1' + classpath 'com.android.tools.build:gradle:4.2.0' classpath 'com.github.dcendents:android-maven-gradle-plugin:2.1' diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 4f6f267..266bb1d 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.1-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-6.7.1-all.zip diff --git a/niceswitch/src/main/java/com/sigma/niceswitch/NiceSwitch.java b/niceswitch/src/main/java/com/sigma/niceswitch/NiceSwitch.java index 71a86fc..c64de6c 100644 --- a/niceswitch/src/main/java/com/sigma/niceswitch/NiceSwitch.java +++ b/niceswitch/src/main/java/com/sigma/niceswitch/NiceSwitch.java @@ -355,7 +355,16 @@ public void onAnimationRepeat(Animator animation) { public void setChecked(boolean checked) { if (this.checked != checked) { this.checked = checked; - animateSwitch(); + //animateSwitch(); + if (!this.checked) { + setIconProgress(1f); + } + + if (this.checked) { + setCurrentColor(onColor); + } else { + setCurrentColor(offColor); + } } } diff --git a/niceswitch/src/main/java/com/sigma/niceswitch/NiceSwitchRound.java b/niceswitch/src/main/java/com/sigma/niceswitch/NiceSwitchRound.java index 15a90cf..03ca780 100644 --- a/niceswitch/src/main/java/com/sigma/niceswitch/NiceSwitchRound.java +++ b/niceswitch/src/main/java/com/sigma/niceswitch/NiceSwitchRound.java @@ -312,7 +312,17 @@ public void onAnimationRepeat(Animator animation) { public void setChecked(boolean checked) { if (this.checked != checked) { this.checked = checked; - animateSwitch(); + //animateSwitch(); + + if (!this.checked) { + setIconProgress(1f); + } + + if (this.checked) { + setCurrentColor(onColor); + } else { + setCurrentColor(offColor); + } } }