Skip to content

Commit a883963

Browse files
committed
Add drawable left icon for the switch button text
1 parent 023f6eb commit a883963

File tree

13 files changed

+43
-5
lines changed

13 files changed

+43
-5
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<vector xmlns:android="http://schemas.android.com/apk/res/android"
2+
android:width="24dp"
3+
android:height="24dp"
4+
android:viewportWidth="24"
5+
android:viewportHeight="24"
6+
android:tint="#333333"
7+
android:alpha="0.6">
8+
<path
9+
android:fillColor="@android:color/white"
10+
android:pathData="M12,17c1.1,0 2,-0.9 2,-2s-0.9,-2 -2,-2 -2,0.9 -2,2 0.9,2 2,2zM18,8h-1L17,6c0,-2.76 -2.24,-5 -5,-5S7,3.24 7,6h1.9c0,-1.71 1.39,-3.1 3.1,-3.1 1.71,0 3.1,1.39 3.1,3.1v2L6,8c-1.1,0 -2,0.9 -2,2v10c0,1.1 0.9,2 2,2h12c1.1,0 2,-0.9 2,-2L20,10c0,-1.1 -0.9,-2 -2,-2zM18,20L6,20L6,10h12v10z"/>
11+
</vector>
447 Bytes
Loading
412 Bytes
Loading
322 Bytes
Loading
250 Bytes
Loading
562 Bytes
Loading
497 Bytes
Loading
856 Bytes
Loading
808 Bytes
Loading
1.16 KB
Loading

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@
2121
app:cornerRadius="50dp"
2222
app:strokeWidth="2dp"
2323
app:textSize="48sp"
24-
app:textToggleLeft="OFF"
25-
app:textToggleRight="ON"
26-
27-
24+
app:textToggleLeft="UnLock"
25+
app:switchOnIcon="@drawable/ic_lock"
26+
app:switchOffIcon="@drawable/ic_lock_open"
27+
app:textToggleRight="Lock"
2828
app:layout_constraintBottom_toBottomOf="parent"
2929
app:layout_constraintEnd_toEndOf="parent"
3030
app:layout_constraintStart_toStartOf="parent"

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

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,12 @@ open class ZiresSwitchSegmentedControl : LinearLayout {
7575
attributes.getString(R.styleable.ZiresSwitchSegmentedControl_textToggleLeft)
7676
val rightToggleText =
7777
attributes.getString(R.styleable.ZiresSwitchSegmentedControl_textToggleRight)
78+
79+
val switchOnIcon =
80+
attributes.getDrawable(R.styleable.ZiresSwitchSegmentedControl_switchOnIcon)
81+
val switchOffIcon =
82+
attributes.getDrawable(R.styleable.ZiresSwitchSegmentedControl_switchOffIcon)
83+
7884
activeBgColor = attributes.getColor(
7985
R.styleable.ZiresSwitchSegmentedControl_activeBgColor,
8086
ContextCompat.getColor(context, Default.ACTIVE_BG_COLOR)
@@ -115,7 +121,26 @@ open class ZiresSwitchSegmentedControl : LinearLayout {
115121
Default.CHECKED
116122
)
117123
val typeface = Typeface.create(switchFontFamily, Typeface.BOLD)
118-
switchFirstItem.text = rightToggleText
124+
125+
if (switchOnIcon != null) {
126+
switchFirstItem.text = rightToggleText
127+
switchFirstItem.setCompoundDrawablesRelativeWithIntrinsicBounds(
128+
switchOnIcon,
129+
null,
130+
null,
131+
null
132+
)
133+
}
134+
135+
if (switchOffIcon != null) {
136+
switchSecondItem.text = leftToggleText
137+
switchSecondItem.setCompoundDrawablesRelativeWithIntrinsicBounds(
138+
switchOffIcon,
139+
null,
140+
null,
141+
null
142+
)
143+
}
119144
switchSecondItem.text = leftToggleText
120145
switchFirstItem.typeface = typeface
121146
switchSecondItem.typeface = typeface

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,7 @@
1313
<attr name="textSize" format="dimension" />
1414
<attr name="switchFontFamily" format="string" />
1515
<attr name="checked" format="boolean" />
16+
<attr name="switchOnIcon" format="reference|color" />
17+
<attr name="switchOffIcon" format="reference|color" />
1618
</declare-styleable>
1719
</resources>

0 commit comments

Comments
 (0)