Skip to content

Commit d1aca74

Browse files
committed
handle is checked
1 parent a176de5 commit d1aca74

File tree

5 files changed

+40
-20
lines changed

5 files changed

+40
-20
lines changed

ZiresSwitchSegmentedControl.png

35 KB
Loading
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,21 @@
11
package com.zires.androidswitchsegmentedcontrol
22

33
import android.os.Bundle
4+
import android.widget.Toast
45
import androidx.appcompat.app.AppCompatActivity
6+
import com.zires.switchsegmentedcontrol.ZiresSwitchSegmentedControl
57

68
class MainActivity : AppCompatActivity() {
79
override fun onCreate(savedInstanceState: Bundle?) {
810
super.onCreate(savedInstanceState)
911
setContentView(R.layout.activity_main)
12+
val switchSegmentedControl: ZiresSwitchSegmentedControl = findViewById(R.id.zires_switch)
13+
switchSegmentedControl.setOnToggleSwitchChangeListener(object :
14+
ZiresSwitchSegmentedControl.OnSwitchChangeListener {
15+
override fun onToggleSwitchChangeListener(isChecked: Boolean) {
16+
Toast.makeText(this@MainActivity, "Is checked: $isChecked", Toast.LENGTH_SHORT)
17+
.show()
18+
}
19+
})
1020
}
1121
}

app/src/main/res/layout/activity_main.xml

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,25 @@
44
xmlns:tools="http://schemas.android.com/tools"
55
android:layout_width="match_parent"
66
android:layout_height="match_parent"
7-
android:background="@color/gray"
87
tools:context=".MainActivity">
98

109

1110
<com.zires.switchsegmentedcontrol.ZiresSwitchSegmentedControl
11+
android:id="@+id/zires_switch"
1212
android:layout_width="wrap_content"
1313
android:layout_height="wrap_content"
1414
app:activeBgColor="@color/green"
1515
app:activeTextColor="@android:color/white"
1616
app:backgroundColor="@android:color/white"
1717
app:borderColor="@color/green"
18-
app:cornerRadius="15dp"
1918
app:inactiveTextColor="@android:color/darker_gray"
20-
app:strokeWidth="12dp"
2119
app:switchFontFamily="serif-monospace"
22-
app:textSize="24sp"
23-
app:textToggleLeft="TURN OFF"
24-
app:textToggleRight="TURN ON"
20+
app:checked="true"
21+
app:cornerRadius="50dp"
22+
app:strokeWidth="2dp"
23+
app:textSize="48sp"
24+
app:textToggleLeft="OFF"
25+
app:textToggleRight="ON"
2526

2627

2728
app:layout_constraintBottom_toBottomOf="parent"

switchsegmentedcontrol/src/main/java/com/zires/switchsegmentedcontrol/ZiresSwitchSegmentedControl.kt

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -106,11 +106,14 @@ open class ZiresSwitchSegmentedControl : LinearLayout {
106106
R.styleable.ZiresSwitchSegmentedControl_strokeWidth,
107107
Default.STROKE_WIDTH
108108
).toInt()
109-
110109
switchBackgroundColor = attributes.getColor(
111110
R.styleable.ZiresSwitchSegmentedControl_backgroundColor,
112111
ContextCompat.getColor(context, Default.BACKGROUND_COLOR)
113112
)
113+
val isChecked = attributes.getBoolean(
114+
R.styleable.ZiresSwitchSegmentedControl_checked,
115+
Default.CHECKED
116+
)
114117
val typeface = Typeface.create(switchFontFamily, Typeface.BOLD)
115118
switchFirstItem.text = rightToggleText
116119
switchSecondItem.text = leftToggleText
@@ -121,7 +124,7 @@ open class ZiresSwitchSegmentedControl : LinearLayout {
121124

122125
val selectedGd = GradientDrawable()
123126
selectedGd.setColor(activeBgColor)
124-
selectedGd.cornerRadius = cornerRadius
127+
selectedGd.cornerRadius = cornerRadius - (strokeWidth)
125128
selected.background = selectedGd
126129

127130
val motionLayoutGd = GradientDrawable()
@@ -150,40 +153,45 @@ open class ZiresSwitchSegmentedControl : LinearLayout {
150153
}
151154
}
152155

156+
setChecked(isChecked)
153157
} finally {
154158
attributes.recycle()
155159
}
156160
}
157161
}
158162

163+
fun setChecked(checked: Boolean) {
164+
if (checked) {
165+
transitionStart = false
166+
motionLayout.transitionToStart()
167+
} else {
168+
transitionStart = true
169+
motionLayout.transitionToEnd()
170+
}
171+
}
172+
173+
fun getIsChecked(): Boolean {
174+
return transitionStart
175+
}
176+
159177
private fun initOnClick() {
160178
transitionStart = true
161179
motionLayoutContainer.setOnClickListener {
162180
mSwitchChangeListener?.onToggleSwitchChangeListener(transitionStart)
163-
164-
if (transitionStart) {
165-
transitionStart = false
166-
motionLayout.transitionToEnd()
167-
} else {
168-
transitionStart = true
169-
motionLayout.transitionToStart()
170-
}
181+
setChecked(transitionStart)
171182
}
172183
}
173184

174185
fun setOnToggleSwitchChangeListener(listener: OnSwitchChangeListener) {
175186
mSwitchChangeListener = listener
176187
}
177188

178-
private fun dp2px(context: Context, dp: Float): Float {
179-
return dp * (context.resources.displayMetrics.densityDpi / 160f)
180-
}
181-
182189
interface OnSwitchChangeListener {
183190
fun onToggleSwitchChangeListener(isChecked: Boolean)
184191
}
185192

186193
protected object Default {
194+
const val CHECKED: Boolean = false
187195
val ACTIVE_BG_COLOR = R.color.green
188196
const val ACTIVE_TEXT_COLOR = android.R.color.white
189197
val INACTIVE_TEXT_COLOR = R.color.gray

switchsegmentedcontrol/src/main/res/values/attr.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,6 @@
1212
<attr name="strokeWidth" format="dimension" />
1313
<attr name="textSize" format="dimension" />
1414
<attr name="switchFontFamily" format="string" />
15+
<attr name="checked" format="boolean" />
1516
</declare-styleable>
1617
</resources>

0 commit comments

Comments
 (0)