|
| 1 | + |
| 2 | + |
| 3 | +# Switch Segmented Control for android |
1 | 4 | [](https://jitpack.io/#classiczires/Android-Switch-Segmented-Control) |
| 5 | +[](https://android-arsenal.com/api?level=17) |
| 6 | + |
| 7 | +Android Switch Segmented Control is an implementation of iOS Segmented Control for use in Android applications. It runs on API 17+ (Android 4.2) |
| 8 | +# Android segmented control style |
| 9 | +<img src="ZiresSwitchSegmentedControl.png" width="50%"> |
| 10 | +<img src="androidswitchsegmentedcontrol.gif" width="32%" > |
| 11 | + |
| 12 | + |
| 13 | + |
| 14 | +## Installation |
| 15 | + |
| 16 | +### Gradle |
| 17 | +Add this to the root build.gradle at the end of repositories (**WARNING:** Make sure you add this under **allprojects** not under buildscript): |
| 18 | +```Gradle |
| 19 | +allprojects { |
| 20 | + repositories { |
| 21 | + ... |
| 22 | + maven { url 'https://jitpack.io' } |
| 23 | + } |
| 24 | +} |
| 25 | +``` |
| 26 | + |
| 27 | +Add the dependency to the project build.gradle: |
| 28 | +```Gradle |
| 29 | +dependencies { |
| 30 | + implementation 'com.github.classiczires:Android-Switch-Segmented-Control:1.0.2' |
| 31 | +} |
| 32 | +``` |
| 33 | + |
| 34 | +## Usage |
| 35 | + |
| 36 | + 1. First add a ZiresSwitchSegmentedControl to your xml layout as : |
| 37 | +```xml |
| 38 | + <com.zires.switchsegmentedcontrol.ZiresSwitchSegmentedControl |
| 39 | + android:id="@+id/zires_switch" |
| 40 | + android:layout_width="wrap_content" |
| 41 | + android:layout_height="wrap_content" |
| 42 | + app:activeBgColor="@color/green" |
| 43 | + app:activeTextColor="@android:color/white" |
| 44 | + app:backgroundColor="@android:color/white" |
| 45 | + app:borderColor="@color/green" |
| 46 | + app:inactiveTextColor="@android:color/darker_gray" |
| 47 | + app:switchFontFamily="serif-monospace" |
| 48 | + app:checked="true" |
| 49 | + app:cornerRadius="50dp" |
| 50 | + app:strokeWidth="2dp" |
| 51 | + app:textSize="48sp" |
| 52 | + app:textToggleLeft="OFF" |
| 53 | + app:textToggleRight="ON" /> |
| 54 | +``` |
| 55 | + |
| 56 | + 2. To handle switch state in your Activity/Fragment class set a ToggleSwitchChangeListener to it as below: |
| 57 | +```kotlin |
| 58 | + val switchSegmentedControl: ZiresSwitchSegmentedControl = findViewById(R.id.zires_switch) |
| 59 | + switchSegmentedControl.setOnToggleSwitchChangeListener(object : |
| 60 | + ZiresSwitchSegmentedControl.OnSwitchChangeListener { |
| 61 | + override fun onToggleSwitchChangeListener(isChecked: Boolean) { |
| 62 | + // your code |
| 63 | + } |
| 64 | + }) |
| 65 | + ``` |
| 66 | + |
| 67 | + 3. To check and uncheck use this method: |
| 68 | + ```kotlin |
| 69 | + switchSegmentedControl.setChecked(true) |
| 70 | + ``` |
| 71 | +# License |
| 72 | + Copyright 2021 Saeed Karimi |
| 73 | + |
| 74 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 75 | + you may not use this file except in compliance with the License. |
| 76 | + You may obtain a copy of the License at |
| 77 | + |
| 78 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 79 | + |
| 80 | + Unless required by applicable law or agreed to in writing, software |
| 81 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 82 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 83 | + See the License for the specific language governing permissions and |
| 84 | + limitations under the License. |
2 | 85 |
|
3 | | -# Android Switch Segmented Control |
4 | | -Segmented Control for android with two items and implemented with `androidx.constraintlayout.motion.widget.MotionLayout` |
5 | | -# Demo |
6 | | -<img src="androidswitchsegmentedcontrol.gif" width="32%"> |
|
0 commit comments