Skip to content

Commit

Permalink
Added functionality to set border
Browse files Browse the repository at this point in the history
  • Loading branch information
Akash authored and Akash committed Oct 21, 2018
1 parent da679d2 commit dde8d6f
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 4 deletions.
6 changes: 6 additions & 0 deletions app/.idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
android:id="@+id/revealSwitch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:showBorder="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
Expand Down
33 changes: 30 additions & 3 deletions revealswitch/src/main/java/com/akash/RevealSwitch.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import android.content.Context
import android.content.res.TypedArray
import android.graphics.Color
import android.graphics.PorterDuff
import android.graphics.drawable.GradientDrawable
import android.support.annotation.ColorInt
import android.support.annotation.IntRange
import android.util.AttributeSet
Expand All @@ -25,11 +26,14 @@ class RevealSwitch @JvmOverloads constructor(
private lateinit var clickable: View
private lateinit var revealSwitchContainer: FrameLayout
private var isEnable: Boolean = false
private lateinit var border: GradientDrawable
private var isborderEnabled: Boolean = false
private lateinit var typedArray: TypedArray

private var enabledTrackColor: Int = Color.parseColor("#444444")
private var disabledTrackColor: Int = Color.parseColor("#FFFFFF")
private var animDuration: Int = 500
private var borderColor: Int? = null

init {
View.inflate(context, R.layout.layout_revealswitch, this).apply {
Expand All @@ -39,6 +43,7 @@ class RevealSwitch @JvmOverloads constructor(
checkedThumb = findViewById<View>(R.id.checkedThumb)
clickable = findViewById(R.id.clickable)
revealSwitchContainer = findViewById(R.id.revealSwitch_Container)
border = clickable.background as GradientDrawable
getAttributeSet(context, attrs)

}
Expand All @@ -51,11 +56,17 @@ class RevealSwitch @JvmOverloads constructor(
disabledTrackColor = typedArray.getColor(R.styleable.RevealSwitch_setDisabledTrackColor,
Color.parseColor("#FFFFFF"))
isEnable = typedArray.getBoolean(R.styleable.RevealSwitch_setEnabled, false)
val duration = typedArray.getInteger(R.styleable.RevealSwitch_setAnimationDuration, 500)
if (duration > 1500 || duration < 500) {
animDuration = typedArray.getInteger(R.styleable.RevealSwitch_setAnimationDuration,
500)
isborderEnabled = typedArray.getBoolean(R.styleable.RevealSwitch_showBorder, false)
borderColor = typedArray.getColor(
R.styleable.RevealSwitch_borderColor,
0
)
if (animDuration > 1500 || animDuration < 500) {
throw IllegalArgumentException("duration must be between 500 to 1500")
} else {
setAnimationDuration(duration)
setAnimationDuration(animDuration)
}
setAnimationDuration(animDuration)
setEnable(isEnable)
Expand Down Expand Up @@ -102,6 +113,7 @@ class RevealSwitch @JvmOverloads constructor(
private fun revealEnableTrackAnimation() {
isEnable = true
unCheckedView.visibility = View.INVISIBLE
showBorderIfEnabled(disabledTrackColor, borderColor)
revealSwitchContainer.background.setColorFilter(disabledTrackColor, PorterDuff.Mode.SRC_ATOP)
checkedView.visibility = View.VISIBLE
val x: Int = unCheckedThumb.left + unCheckedThumb.width / 2
Expand Down Expand Up @@ -132,6 +144,7 @@ class RevealSwitch @JvmOverloads constructor(
private fun revealDisableTrackAnimation() {
isEnable = false
checkedView.visibility = View.INVISIBLE
showBorderIfEnabled(enabledTrackColor, borderColor)
revealSwitchContainer.background.setColorFilter(enabledTrackColor, PorterDuff.Mode.SRC_ATOP)
unCheckedView.visibility = View.VISIBLE
val x: Int = checkedThumb.right - checkedThumb.width / 2
Expand Down Expand Up @@ -164,8 +177,22 @@ class RevealSwitch @JvmOverloads constructor(
isEnable = isChecked
if (isEnable) {
unCheckedView.visibility = View.INVISIBLE
showBorderIfEnabled(disabledTrackColor, borderColor)
} else {
unCheckedView.visibility = View.VISIBLE
showBorderIfEnabled(enabledTrackColor, borderColor)
}
}

private fun showBorderIfEnabled(color: Int, borderColor: Int?) {
if (isborderEnabled) {
if (borderColor == 0) {
border.setStroke(4, color)
} else {
border.setStroke(4, borderColor!!)
}
} else {
border.setStroke(0, color)
}
}
}
6 changes: 6 additions & 0 deletions revealswitch/src/main/res/drawable/switch_border.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="50dp"/>
<stroke android:color="@android:color/black" android:width="1dp"/>
</shape>
3 changes: 2 additions & 1 deletion revealswitch/src/main/res/layout/layout_revealswitch.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@
<View
android:id="@+id/clickable"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
android:layout_height="match_parent"
android:background="@drawable/switch_border"/>


</FrameLayout>
2 changes: 2 additions & 0 deletions revealswitch/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,7 @@
<attr name="setEnabledTrackColor" format="color"/>
<attr name="setDisabledTrackColor" format="color"/>
<attr name="setAnimationDuration" format="integer"/>
<attr name="showBorder" format="boolean"/>
<attr name="borderColor" format="color"/>
</declare-styleable>
</resources>

0 comments on commit dde8d6f

Please sign in to comment.